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 _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 _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 _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; }
/**
 * localStorage 工具函数
 * 用于管理搜索历史的存储
 */

/**
 * 从 localStorage 加载历史记录
 * @param key 存储的 key
 * @returns 历史记录数组
 */
export var loadHistory = function loadHistory(key) {
  try {
    var data = localStorage.getItem(key);
    if (!data) return [];
    return JSON.parse(data);
  } catch (error) {
    console.error('Failed to load history from localStorage:', error);
    return [];
  }
};

/**
 * 保存历史记录到 localStorage
 * @param key 存储的 key
 * @param history 历史记录数组
 * @param maxCount 最大数量限制
 */
export var saveHistory = function saveHistory(key, history, maxCount) {
  try {
    // 限制数量
    var limitedHistory = history.slice(0, maxCount);
    localStorage.setItem(key, JSON.stringify(limitedHistory));
  } catch (error) {
    console.error('Failed to save history to localStorage:', error);
  }
};

/**
 * 清除历史记录
 * @param key 存储的 key
 */
export var clearHistory = function clearHistory(key) {
  try {
    localStorage.removeItem(key);
  } catch (error) {
    console.error('Failed to clear history from localStorage:', error);
  }
};

/**
 * 添加历史记录项
 * @param key 存储的 key
 * @param item 新的历史记录项
 * @param maxCount 最大数量限制
 * @returns 更新后的历史记录数组
 */
export var addHistoryItem = function addHistoryItem(key, item, maxCount) {
  try {
    // 加载现有历史
    var history = loadHistory(key);

    // 去重：如果已存在，先移除
    var filteredHistory = history.filter(function (h) {
      return h !== item;
    });

    // 添加到开头
    var newHistory = [item].concat(_toConsumableArray(filteredHistory));

    // 保存
    saveHistory(key, newHistory, maxCount);
    return newHistory.slice(0, maxCount);
  } catch (error) {
    console.error('Failed to add history item:', error);
    return [];
  }
};

/**
 * 删除历史记录项
 * @param key 存储的 key
 * @param item 要删除的历史记录项
 * @returns 更新后的历史记录数组
 */
export var removeHistoryItem = function removeHistoryItem(key, item) {
  try {
    var history = loadHistory(key);
    var newHistory = history.filter(function (h) {
      return h !== item;
    });
    localStorage.setItem(key, JSON.stringify(newHistory));
    return newHistory;
  } catch (error) {
    console.error('Failed to remove history item:', error);
    return [];
  }
};

/**
 * 检查 localStorage 是否可用
 * @returns 是否可用
 */
export var isLocalStorageAvailable = function isLocalStorageAvailable() {
  try {
    var testKey = '__pisell_lookup_test__';
    localStorage.setItem(testKey, 'test');
    localStorage.removeItem(testKey);
    return true;
  } catch (_unused) {
    return false;
  }
};