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);

// src/components/pisellLookup/hooks/useSearchHistory.ts
var useSearchHistory_exports = {};
__export(useSearchHistory_exports, {
  useSearchHistory: () => useSearchHistory
});
module.exports = __toCommonJS(useSearchHistory_exports);
var import_react = require("react");
var import_utils = require("../utils");
var useSearchHistory = (options) => {
  const { historyKey, maxHistoryCount, onLoadHistory, onSaveHistory } = options;
  const [history, setHistory] = (0, import_react.useState)([]);
  const reloadHistory = (0, import_react.useCallback)(() => {
    try {
      if (onLoadHistory) {
        const loadedHistory = onLoadHistory();
        setHistory(Array.isArray(loadedHistory) ? loadedHistory : []);
      } else {
        const loadedHistory = (0, import_utils.loadHistory)(historyKey);
        setHistory(Array.isArray(loadedHistory) ? loadedHistory : []);
      }
    } catch (error) {
      console.error("Failed to reload history:", error);
      setHistory([]);
    }
  }, [historyKey, onLoadHistory]);
  (0, import_react.useEffect)(() => {
    reloadHistory();
  }, [reloadHistory]);
  const addHistory = (0, import_react.useCallback)(
    (keyword) => {
      if (!keyword || !keyword.trim()) {
        return;
      }
      const trimmedKeyword = keyword.trim();
      if (onSaveHistory) {
        const safeHistory = Array.isArray(history) ? history : [];
        const filteredHistory = safeHistory.filter((h) => h !== trimmedKeyword);
        const newHistory = [trimmedKeyword, ...filteredHistory].slice(
          0,
          maxHistoryCount
        );
        setHistory(newHistory);
        onSaveHistory(newHistory);
      } else {
        const newHistory = (0, import_utils.addHistoryItem)(
          historyKey,
          trimmedKeyword,
          maxHistoryCount
        );
        setHistory(newHistory);
      }
    },
    [history, historyKey, maxHistoryCount, onSaveHistory]
  );
  const removeHistory = (0, import_react.useCallback)(
    (keyword) => {
      if (onSaveHistory) {
        const safeHistory = Array.isArray(history) ? history : [];
        const newHistory = safeHistory.filter((h) => h !== keyword);
        setHistory(newHistory);
        onSaveHistory(newHistory);
      } else {
        const newHistory = (0, import_utils.removeHistoryItem)(historyKey, keyword);
        setHistory(newHistory);
      }
    },
    [history, historyKey, onSaveHistory]
  );
  const clearAllHistory = (0, import_react.useCallback)(() => {
    if (onSaveHistory) {
      setHistory([]);
      onSaveHistory([]);
    } else {
      (0, import_utils.clearHistory)(historyKey);
      setHistory([]);
    }
  }, [historyKey, onSaveHistory]);
  return {
    history,
    addHistory,
    removeHistory,
    clearAllHistory,
    reloadHistory
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useSearchHistory
});
