"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var BaseAutocomplete_exports = {};
__export(BaseAutocomplete_exports, {
  BaseAutocomplete: () => BaseAutocomplete
});
module.exports = __toCommonJS(BaseAutocomplete_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_ink = require("ink");
function BaseAutocomplete({
  items,
  query,
  isVisible,
  isLoading = false,
  onSelect,
  onLoadIntoInput,
  onClose,
  filterFn,
  scoreFn,
  formatItem,
  formatLoadText,
  title,
  maxVisibleItems = 10,
  loadingMessage = "Loading...",
  emptyMessage = "No matches found",
  borderColor = "cyan"
}) {
  const [selectedIndex, setSelectedIndex] = (0, import_react.useState)(0);
  const [scrollOffset, setScrollOffset] = (0, import_react.useState)(0);
  const selectedIndexRef = (0, import_react.useRef)(0);
  (0, import_react.useEffect)(() => {
    selectedIndexRef.current = selectedIndex;
  }, [selectedIndex]);
  const filteredItems = (0, import_react.useMemo)(() => {
    if (!query) return items;
    return items.filter((item) => filterFn(item, query)).sort((a, b) => {
      const scoreA = scoreFn(a, query);
      const scoreB = scoreFn(b, query);
      return scoreB - scoreA;
    });
  }, [items, query, filterFn, scoreFn]);
  (0, import_react.useEffect)(() => {
    setSelectedIndex(0);
    setScrollOffset(0);
  }, [filteredItems.length]);
  (0, import_react.useEffect)(() => {
    if (selectedIndex < scrollOffset) {
      setScrollOffset(selectedIndex);
    } else if (selectedIndex >= scrollOffset + maxVisibleItems) {
      setScrollOffset(Math.max(0, selectedIndex - maxVisibleItems + 1));
    }
  }, [selectedIndex, scrollOffset, maxVisibleItems]);
  const visibleItems = (0, import_react.useMemo)(() => {
    return filteredItems.slice(scrollOffset, scrollOffset + maxVisibleItems);
  }, [filteredItems, scrollOffset, maxVisibleItems]);
  (0, import_ink.useInput)(
    (input, key) => {
      if (!isVisible) return;
      const itemsLength = filteredItems.length;
      if (itemsLength === 0) return;
      if (key.upArrow) {
        setSelectedIndex((prev) => (prev - 1 + itemsLength) % itemsLength);
      }
      if (key.downArrow) {
        setSelectedIndex((prev) => (prev + 1) % itemsLength);
      }
      if (key.escape) {
        onClose();
      }
      if (key.tab && onLoadIntoInput && formatLoadText && itemsLength > 0) {
        const item = filteredItems[selectedIndexRef.current];
        if (item) {
          onLoadIntoInput(formatLoadText(item));
        }
        return;
      }
      if (key.return && itemsLength > 0) {
        const item = filteredItems[selectedIndexRef.current];
        if (item) {
          onSelect(item);
        }
      }
    },
    { isActive: isVisible }
  );
  if (!isVisible) return null;
  if (isLoading) {
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { borderStyle: "single", borderColor: "gray", paddingX: 1, paddingY: 1, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: loadingMessage }) });
  }
  if (filteredItems.length === 0) {
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { borderStyle: "single", borderColor: "gray", paddingX: 1, paddingY: 1, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: emptyMessage }) });
  }
  const hasMoreAbove = scrollOffset > 0;
  const hasMoreBelow = scrollOffset + maxVisibleItems < filteredItems.length;
  const totalItems = filteredItems.length;
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
    import_ink.Box,
    {
      borderStyle: "single",
      borderColor,
      flexDirection: "column",
      height: Math.min(maxVisibleItems + 3, totalItems + 3),
      children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { paddingX: 1, paddingY: 0, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
          title,
          " (",
          selectedIndex + 1,
          "/",
          totalItems,
          ") - \u2191\u2193 navigate",
          onLoadIntoInput && ", Tab load",
          ", Enter select, Esc close"
        ] }) }),
        hasMoreAbove && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { paddingX: 1, paddingY: 0, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
          "... \u2191 (",
          scrollOffset,
          " more above)"
        ] }) }),
        visibleItems.map((item, visibleIndex) => {
          const actualIndex = scrollOffset + visibleIndex;
          const isSelected = actualIndex === selectedIndex;
          return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { paddingX: 1, paddingY: 0, children: formatItem(item, isSelected) }, actualIndex);
        }),
        hasMoreBelow && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { paddingX: 1, paddingY: 0, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
          "... \u2193 (",
          totalItems - scrollOffset - maxVisibleItems,
          " more below)"
        ] }) })
      ]
    }
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BaseAutocomplete
});
