"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 useHistorySearch_exports = {};
__export(useHistorySearch_exports, {
  useHistorySearch: () => useHistorySearch
});
module.exports = __toCommonJS(useHistorySearch_exports);
var import_react = require("react");
function findMatches(history, query) {
  if (!query) return [];
  const lowerQuery = query.toLowerCase();
  return history.filter((item) => item.toLowerCase().includes(lowerQuery)).reverse();
}
function useHistorySearch({
  ui,
  input,
  buffer,
  setUi,
  setInput
}) {
  const { historySearch } = ui;
  const { history } = input;
  const matches = (0, import_react.useMemo)(
    () => findMatches(history, historySearch.query),
    [history, historySearch.query]
  );
  const hasMatch = matches.length > 0;
  const currentMatch = hasMatch ? matches[Math.min(historySearch.matchIndex, matches.length - 1)] || null : historySearch.lastMatch || null;
  const enter = (0, import_react.useCallback)(() => {
    const currentText = buffer.text;
    setUi((prev) => ({
      ...prev,
      historySearch: {
        isActive: true,
        query: "",
        matchIndex: 0,
        originalInput: currentText,
        lastMatch: ""
      }
    }));
  }, [buffer, setUi]);
  const exit = (0, import_react.useCallback)(() => {
    setUi((prev) => ({
      ...prev,
      historySearch: {
        isActive: false,
        query: "",
        matchIndex: 0,
        originalInput: "",
        lastMatch: ""
      }
    }));
  }, [setUi]);
  const cancel = (0, import_react.useCallback)(() => {
    const originalInput = historySearch.originalInput;
    buffer.setText(originalInput);
    setInput((prev) => ({ ...prev, value: originalInput }));
    exit();
  }, [historySearch.originalInput, buffer, setInput, exit]);
  const applyMatch = (0, import_react.useCallback)(
    (match) => {
      if (match) {
        buffer.setText(match);
        setInput((prev) => ({ ...prev, value: match }));
      }
    },
    [buffer, setInput]
  );
  const updateQuery = (0, import_react.useCallback)(
    (newQuery) => {
      const newMatches = findMatches(history, newQuery);
      const newHasMatch = newMatches.length > 0;
      const newMatch = newHasMatch ? newMatches[0] : null;
      setUi((prev) => ({
        ...prev,
        historySearch: {
          ...prev.historySearch,
          query: newQuery,
          matchIndex: 0,
          // Update lastMatch only if we have a new match
          lastMatch: newMatch || prev.historySearch.lastMatch
        }
      }));
      if (newMatch) {
        applyMatch(newMatch);
      }
    },
    [history, setUi, applyMatch]
  );
  const appendToQuery = (0, import_react.useCallback)(
    (char) => {
      updateQuery(historySearch.query + char);
    },
    [historySearch.query, updateQuery]
  );
  const backspace = (0, import_react.useCallback)(() => {
    if (historySearch.query.length > 0) {
      updateQuery(historySearch.query.slice(0, -1));
    }
  }, [historySearch.query, updateQuery]);
  const cycleNext = (0, import_react.useCallback)(() => {
    if (matches.length === 0) return;
    const newIndex = Math.min(historySearch.matchIndex + 1, matches.length - 1);
    const newMatch = matches[newIndex];
    setUi((prev) => ({
      ...prev,
      historySearch: {
        ...prev.historySearch,
        matchIndex: newIndex,
        lastMatch: newMatch || prev.historySearch.lastMatch
      }
    }));
    if (newMatch) {
      applyMatch(newMatch);
    }
  }, [matches, historySearch.matchIndex, setUi, applyMatch]);
  const cyclePrev = (0, import_react.useCallback)(() => {
    if (matches.length === 0) return;
    const newIndex = Math.max(0, historySearch.matchIndex - 1);
    const newMatch = matches[newIndex];
    setUi((prev) => ({
      ...prev,
      historySearch: {
        ...prev.historySearch,
        matchIndex: newIndex,
        lastMatch: newMatch || prev.historySearch.lastMatch
      }
    }));
    if (newMatch) {
      applyMatch(newMatch);
    }
  }, [matches, historySearch.matchIndex, setUi, applyMatch]);
  const accept = (0, import_react.useCallback)(() => {
    exit();
  }, [exit]);
  const handleKey = (0, import_react.useCallback)(
    (inputStr, key) => {
      if (!historySearch.isActive) {
        if (key.ctrl && inputStr === "r") {
          enter();
          return true;
        }
        return false;
      }
      if (key.ctrl && inputStr === "e") {
        cyclePrev();
        return true;
      }
      if (key.ctrl && inputStr === "r") {
        cycleNext();
        return true;
      }
      if (key.return) {
        accept();
        return true;
      }
      if (key.escape) {
        cancel();
        return true;
      }
      if (key.backspace || key.delete) {
        backspace();
        return true;
      }
      if (inputStr && !key.ctrl && !key.meta) {
        appendToQuery(inputStr);
        return true;
      }
      return true;
    },
    [
      historySearch.isActive,
      enter,
      cycleNext,
      cyclePrev,
      accept,
      cancel,
      backspace,
      appendToQuery
    ]
  );
  return {
    isActive: historySearch.isActive,
    query: historySearch.query,
    currentMatch,
    hasMatch,
    originalInput: historySearch.originalInput,
    enter,
    exit,
    cancel,
    updateQuery,
    appendToQuery,
    backspace,
    cycleNext,
    cyclePrev,
    accept,
    handleKey
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useHistorySearch
});
