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

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

cd "$REPO_ROOT/kraiken-lib"

if [[ ! -d node_modules || ! -x node_modules/.bin/tsc ]]; then
  if ! npm install --silent; then
    echo "Warning: npm install failed; continuing with existing node_modules" >&2
  fi
fi

# Ensure tax rate data mirrors onchain Stake.sol before compiling
node "$SCRIPT_DIR/sync-tax-rates.mjs"

./node_modules/.bin/tsc

echo "kraiken-lib built"
