#!/usr/bin/env bash
# Regenerate @nech/api from the checked-in OpenAPI snapshot(s), then bundle.
# Fully generated — no hand-written client code to drift. Re-run after any API
# change (first refresh the snapshot: ../refresh_openapi.py).
#
#   ./codegen.sh
#
# Adding a domain (geo, iris): vendor its openapi.json next to ../openapi.json
# and add a generate block below + an exports entry in package.json.
set -euo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
GEN="npx --yes @openapitools/openapi-generator-cli generate"
PROPS="supportsES6=true,typescriptThreePlus=true"

gen() {  # gen <domain> <spec-path>
  local domain="$1" spec="$2"
  echo "→ generating $domain from $spec"
  $GEN -i "$spec" -g typescript-fetch -o "$HERE/src/$domain" \
    --additional-properties="$PROPS"
  rm -rf "$HERE/src/$domain/docs" "$HERE/src/$domain/.openapi-generator" \
         "$HERE/src/$domain/.openapi-generator-ignore"
}

gen audio "$HERE/../openapi.json"
# gen geo  "$HERE/../openapi.geo.json"     # when geo's spec is vendored
# gen iris "$HERE/../openapi.iris.json"    # when iris's spec is vendored

cd "$HERE"
npm run build
echo "✓ @nech/api regenerated + bundled"
