#!/usr/bin/env bash
set -euo pipefail

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
go_file="$repo_root/golang/nats-server/server/jetstream_api.go"

if [[ -f "$go_file" ]]; then
  {
    rg -n -F '$JS.API' "$go_file" \
      | awk -F: '{print $3}' \
      | sed -E 's/.*"(\$JS\.API[^\"]+)".*/\1/' \
      | awk '/^\$JS\.API/ && $0 !~ /\.>$/'

    # Some Go constants are coarse patterns (e.g. "$JS.API.STREAM.>").
    # Add explicit subject families used by parity tests/docs.
    cat <<'EOF'
$JS.API.INFO
$JS.API.SERVER.REMOVE
$JS.API.ACCOUNT.PURGE.*
$JS.API.ACCOUNT.STREAM.MOVE.*
$JS.API.ACCOUNT.STREAM.MOVE.CANCEL.*
$JS.API.STREAM.UPDATE.*
$JS.API.STREAM.DELETE.*
$JS.API.STREAM.PURGE.*
$JS.API.STREAM.PEER.REMOVE.*
$JS.API.STREAM.NAMES
$JS.API.STREAM.LIST
$JS.API.STREAM.MSG.GET.*
$JS.API.STREAM.MSG.DELETE.*
$JS.API.STREAM.SNAPSHOT.*
$JS.API.STREAM.RESTORE.*
$JS.API.CONSUMER.NAMES.*
$JS.API.CONSUMER.LIST.*
$JS.API.CONSUMER.DELETE.*.*
$JS.API.CONSUMER.PAUSE.*.*
$JS.API.CONSUMER.RESET.*.*
$JS.API.CONSUMER.UNPIN.*.*
$JS.API.CONSUMER.MSG.NEXT.*.*
$JS.API.CONSUMER.LEADER.STEPDOWN.*.*
$JS.API.DIRECT.GET.*
$JS.API.STREAM.LEADER.STEPDOWN.*
$JS.API.META.LEADER.STEPDOWN
EOF
  } | sort -u
  exit 0
fi

# Fallback subject inventory when Go reference sources are not vendored in this repo.
cat <<'EOF'
$JS.API.INFO
$JS.API.SERVER.REMOVE
$JS.API.ACCOUNT.PURGE.*
$JS.API.ACCOUNT.STREAM.MOVE.*
$JS.API.ACCOUNT.STREAM.MOVE.CANCEL.*
$JS.API.STREAM.CREATE.*
$JS.API.STREAM.UPDATE.*
$JS.API.STREAM.DELETE.*
$JS.API.STREAM.PURGE.*
$JS.API.STREAM.INFO.*
$JS.API.STREAM.PEER.REMOVE.*
$JS.API.STREAM.NAMES
$JS.API.STREAM.LIST
$JS.API.STREAM.MSG.GET.*
$JS.API.STREAM.MSG.DELETE.*
$JS.API.STREAM.SNAPSHOT.*
$JS.API.STREAM.RESTORE.*
$JS.API.CONSUMER.CREATE.*.*
$JS.API.CONSUMER.INFO.*.*
$JS.API.CONSUMER.NAMES.*
$JS.API.CONSUMER.LIST.*
$JS.API.CONSUMER.DELETE.*.*
$JS.API.CONSUMER.PAUSE.*.*
$JS.API.CONSUMER.RESET.*.*
$JS.API.CONSUMER.UNPIN.*.*
$JS.API.CONSUMER.MSG.NEXT.*.*
$JS.API.CONSUMER.LEADER.STEPDOWN.*.*
$JS.API.DIRECT.GET.*
$JS.API.STREAM.LEADER.STEPDOWN.*
$JS.API.META.LEADER.STEPDOWN
EOF
