function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { useCallback, useEffect, useState } from 'react';
import { addHistoryItem, clearHistory, loadHistory, removeHistoryItem } from "../utils";

/**
 * 搜索历史管理 Hook
 */

/**
 * 搜索历史管理 Hook
 * 支持自定义存储逻辑
 */
export var useSearchHistory = function useSearchHistory(options) {
  var historyKey = options.historyKey,
    maxHistoryCount = options.maxHistoryCount,
    onLoadHistory = options.onLoadHistory,
    onSaveHistory = options.onSaveHistory;
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    history = _useState2[0],
    setHistory = _useState2[1];

  /**
   * 加载历史记录
   */
  var reloadHistory = useCallback(function () {
    try {
      if (onLoadHistory) {
        // 使用自定义加载函数
        var loadedHistory = onLoadHistory();
        setHistory(Array.isArray(loadedHistory) ? loadedHistory : []);
      } else {
        // 使用默认的 localStorage 加载
        var _loadedHistory = loadHistory(historyKey);
        setHistory(Array.isArray(_loadedHistory) ? _loadedHistory : []);
      }
    } catch (error) {
      console.error('Failed to reload history:', error);
      setHistory([]);
    }
  }, [historyKey, onLoadHistory]);

  /**
   * 初始化：加载历史记录
   */
  useEffect(function () {
    reloadHistory();
  }, [reloadHistory]);

  /**
   * 添加历史记录
   */
  var addHistory = useCallback(function (keyword) {
    // 忽略空关键词
    if (!keyword || !keyword.trim()) {
      return;
    }
    var trimmedKeyword = keyword.trim();
    if (onSaveHistory) {
      // 使用自定义保存函数
      // 去重并添加到开头
      var safeHistory = Array.isArray(history) ? history : [];
      var filteredHistory = safeHistory.filter(function (h) {
        return h !== trimmedKeyword;
      });
      var newHistory = [trimmedKeyword].concat(_toConsumableArray(filteredHistory)).slice(0, maxHistoryCount);
      setHistory(newHistory);
      onSaveHistory(newHistory);
    } else {
      // 使用默认的 localStorage 保存
      var _newHistory = addHistoryItem(historyKey, trimmedKeyword, maxHistoryCount);
      setHistory(_newHistory);
    }
  }, [history, historyKey, maxHistoryCount, onSaveHistory]);

  /**
   * 删除历史记录
   */
  var removeHistory = useCallback(function (keyword) {
    if (onSaveHistory) {
      // 使用自定义保存函数
      var safeHistory = Array.isArray(history) ? history : [];
      var newHistory = safeHistory.filter(function (h) {
        return h !== keyword;
      });
      setHistory(newHistory);
      onSaveHistory(newHistory);
    } else {
      // 使用默认的 localStorage 删除
      var _newHistory2 = removeHistoryItem(historyKey, keyword);
      setHistory(_newHistory2);
    }
  }, [history, historyKey, onSaveHistory]);

  /**
   * 清除全部历史记录
   */
  var clearAllHistory = useCallback(function () {
    if (onSaveHistory) {
      // 使用自定义保存函数
      setHistory([]);
      onSaveHistory([]);
    } else {
      // 使用默认的 localStorage 清除
      clearHistory(historyKey);
      setHistory([]);
    }
  }, [historyKey, onSaveHistory]);
  return {
    history: history,
    addHistory: addHistory,
    removeHistory: removeHistory,
    clearAllHistory: clearAllHistory,
    reloadHistory: reloadHistory
  };
};