import { jsx, jsxs } from "react/jsx-runtime";
import { useMemo, useState } from "react";
import { Box, Text, useInput, useStdout } from "ink";
import { semantic } from "../theme/palette.js";
import { useShellWidth } from "./width.js";
import { fuzzyScore } from "./palette-index.js";
const FREQUENTLY_USED = [
  "WIGOLO_LLM_API_KEY",
  "agents",
  "verify",
  "doctor",
  "export"
];
const MAX_RESULTS = 8;
const TINY_LABEL_CHROME_COLS = 14;
function truncateLabel(label, maxLen) {
  if (label.length <= maxLen) return label;
  return label.slice(0, maxLen - 1) + "\u2026";
}
function CommandPalette({ entries, onPick, onClose }) {
  const shellWidth = useShellWidth();
  const { stdout } = useStdout();
  const isTiny = shellWidth === "tiny";
  const overlayWidth = isTiny ? "100%" : 50;
  const maxLabelChars = isTiny ? Math.max(10, stdout.columns - TINY_LABEL_CHROME_COLS) : 44;
  const [query, setQuery] = useState("");
  const [cursor, setCursor] = useState(0);
  const filtered = useMemo(() => {
    if (!query) {
      const defaults = FREQUENTLY_USED.map((id) => entries.find((e) => e.id === id)).filter((e) => e !== void 0);
      return defaults.slice(0, MAX_RESULTS);
    }
    return entries.map((e) => ({ entry: e, score: fuzzyScore(query, e.label) + fuzzyScore(query, e.keywords.join(" ")) })).filter((x) => x.score > 0).sort((a, b) => b.score - a.score).map((x) => x.entry).slice(0, MAX_RESULTS);
  }, [query, entries]);
  const clampedCursor = Math.min(cursor, Math.max(0, filtered.length - 1));
  useInput((input, key) => {
    if (key.escape) {
      onClose();
      return;
    }
    if (key.return) {
      const picked = filtered[clampedCursor];
      if (picked) {
        onPick(picked);
      }
      return;
    }
    if (key.upArrow) {
      setCursor((c) => Math.max(0, c - 1));
      return;
    }
    if (key.downArrow) {
      setCursor((c) => Math.min(filtered.length - 1, c + 1));
      return;
    }
    if (key.backspace || key.delete) {
      setQuery((q) => q.slice(0, -1));
      setCursor(0);
      return;
    }
    if (input && !key.ctrl && !key.meta) {
      setQuery((q) => q + input);
      setCursor(0);
    }
  }, { isActive: true });
  return /* @__PURE__ */ jsxs(
    Box,
    {
      borderStyle: "round",
      borderColor: semantic.accent,
      flexDirection: "column",
      paddingX: 1,
      width: overlayWidth,
      children: [
        /* @__PURE__ */ jsx(Text, { color: semantic.accent, bold: true, children: "Jump to\u2026" }),
        /* @__PURE__ */ jsxs(Box, { children: [
          /* @__PURE__ */ jsx(Text, { color: semantic.textDim, children: "> " }),
          /* @__PURE__ */ jsx(Text, { children: query || " " })
        ] }),
        /* @__PURE__ */ jsxs(Box, { flexDirection: "column", marginTop: 1, children: [
          filtered.length === 0 && /* @__PURE__ */ jsx(Text, { color: semantic.textMuted, children: "No results" }),
          filtered.map((entry, i) => {
            const isSel = i === clampedCursor;
            const kindColor = entry.kind === "action" ? semantic.accentAlt : entry.kind === "field" ? semantic.textDim : semantic.ok;
            const displayLabel = isTiny ? truncateLabel(entry.label, maxLabelChars) : entry.label;
            return /* @__PURE__ */ jsxs(Box, { justifyContent: "space-between", children: [
              /* @__PURE__ */ jsxs(Text, { color: isSel ? semantic.text : semantic.textDim, bold: isSel, children: [
                isSel ? "\u25B8 " : "  ",
                displayLabel
              ] }),
              /* @__PURE__ */ jsx(Text, { color: kindColor, children: entry.kind })
            ] }, entry.id);
          })
        ] }),
        /* @__PURE__ */ jsx(Text, { color: semantic.textMuted, children: "\u2191\u2193 navigate \xB7 \u23CE jump \xB7 esc close" })
      ]
    }
  );
}
export {
  CommandPalette
};
//# sourceMappingURL=CommandPalette.js.map