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

# Pre-commit hook: run version bump checks when WIT or extension sources change.
# Install: git config core.hooksPath .githooks

# Only run the check if relevant files are staged
STAGED=$(git diff --cached --name-only)

# The WIT branch is depth-agnostic on purpose. `wit/` lives inside the
# `ironclaw_wasm` crate, and WS7 moved that crate into its family directory
# (`crates/lanes/ironclaw_wasm/`, PROPOSAL §5). A flat path literal here fails
# *silently* — the hook keeps exiting 0 and version bumps stop being checked —
# so the branch matches the crate at any depth under `crates/`, exactly as
# `platform-and-compat.yml`'s `has_direct_wasm_abi_risk` filter already does.
# Pinned by `scripts/ci/test_pre_commit_staged_paths.py` against the real
# inventory, so a rename (which the regex cannot follow) still fails loudly.
NEEDS_CHECK=false
if echo "$STAGED" | grep -qE '^crates/([^/]+/)*ironclaw_wasm/wit/|^channels-src/|^tools-src/'; then
    NEEDS_CHECK=true
fi

if $NEEDS_CHECK; then
    echo "pre-commit: checking version bumps..."
    if ! ./scripts/check-version-bumps.sh; then
        echo ""
        echo "Commit blocked: version bump check failed."
        echo "Bump versions in the relevant registry JSON and/or WIT package declaration."
        echo "To bypass: git commit --no-verify"
        exit 1
    fi
fi

# Reborn WebUI i18n parity (crates/ironclaw_webui/frontend/src/i18n/*.ts) is
# enforced by the frontend's own tooling (`pnpm lint`/`pnpm test` in the
# `webui-v2-js-lint` CI job), not by this shell hook — the v1 gateway's
# `.js` language packs were deleted under Tier B.
