# Detect package manager
if [ -f "package-lock.json" ]; then
  PACKAGE_MANAGER="npm"
  RUNNER="npm run"
  EXECUTOR="npx"
elif [ -f "yarn.lock" ]; then
  PACKAGE_MANAGER="yarn"
  RUNNER="yarn"
  EXECUTOR="yarn"
elif [ -f "bun.lockb" ]; then
  PACKAGE_MANAGER="bun"
  RUNNER="bun run"
  EXECUTOR="bunx"
else
  # Default to npm if no lock file is found
  PACKAGE_MANAGER="npm"
  RUNNER="npm run"
  EXECUTOR="npx"
fi

echo "📦 Using package manager: $PACKAGE_MANAGER"

# Run type check on entire project (can't be done incrementally)
echo "🔍 Running type check..."
$RUNNER typecheck
if [ $? -ne 0 ]; then
  echo "❌ Type check failed. Please fix TypeScript errors before committing."
  exit 1
fi

# Run lint-staged for incremental lint and format checks
echo "🚀 Running lint-staged..."
$EXECUTOR lint-staged --config package.json