#!/bin/sh

# vite-plus hook start
vite_plus_run_staged_files() {
  if [ -x "./node_modules/.bin/vp" ]; then
    "./node_modules/.bin/vp" staged
    return
  fi

  if command -v vp >/dev/null 2>&1; then
    vp staged
    return
  fi

  printf '%s\n' "vite-plus: command not found; skipping staged checks."
}

if ! vite_plus_run_staged_files; then
  printf '%s\n' "Vite+ staged checks failed. Run vp staged to inspect." >&2
  exit 1
fi
# vite-plus hook end

# react-doctor hook start
react_doctor_scan_staged_files() {
  if [ -x "./node_modules/.bin/react-doctor" ]; then
    "./node_modules/.bin/react-doctor" --staged --blocking warning
    return
  fi

  if command -v react-doctor >/dev/null 2>&1; then
    react-doctor --staged --blocking warning
    return
  fi

  if command -v pnpm >/dev/null 2>&1; then
    pnpm dlx react-doctor@latest --staged --blocking warning
    return
  fi

  if command -v npx >/dev/null 2>&1; then
    npx --yes react-doctor@latest --staged --blocking warning
    return
  fi

  printf '%s\n' "react-doctor: command not found; skipping staged scan."
}

react_doctor_output=$(mktemp "${TMPDIR:-/tmp}/react-doctor-hook.XXXXXX")
if react_doctor_scan_staged_files > "$react_doctor_output" 2>&1; then
  rm -f "$react_doctor_output"
else
  rm -f "$react_doctor_output"
  printf '%s\n' "React Doctor found staged regressions." "Run react-doctor --staged --blocking warning to inspect." "Want them fixed? Ask your agent to run that command and resolve the findings." >&2
fi
# react-doctor hook end
