#!/usr/bin/env bash
#
# Verify MCG-DS-REFERENCE.md is in sync with the component source.
# Regenerate with `npm run ds:docs` if this fails.
#
# Installed automatically by `npm install` via the "prepare" script,
# which points core.hooksPath at this directory.
#
# Skip with `git commit --no-verify` only if you really mean it.

set -e

npm run ds:check

# Only run when a tracked .tsx in the DS dir or the generator itself changed.
changed=$(git diff --cached --name-only --diff-filter=ACMR)
if ! echo "$changed" | grep -qE '(^|/)src/app/components/ds/.*\.tsx$|scripts/generate-ds-reference\.mjs$|scripts/ds-categories\.json$'; then
  exit 0
fi

if ! node scripts/generate-ds-reference.mjs --check; then
  echo ""
  echo "✖ MCG-DS-REFERENCE.md is out of date with component source."
  echo "  Run: npm run ds:docs"
  echo "  Then: git add MCG-DS-REFERENCE.md && git commit --amend --no-edit"
  exit 1
fi
