require("../../../_virtual/_rolldown/runtime.js");
const require_storage = require("../utils/storage.js");
let react = require("react");
//#region src/components/pisellLookup/hooks/useSearchHistory.ts
/**
* 搜索历史管理 Hook
* 支持自定义存储逻辑
*/
const useSearchHistory = (options) => {
	const { historyKey, maxHistoryCount, onLoadHistory, onSaveHistory } = options;
	const [history, setHistory] = (0, react.useState)([]);
	/**
	* 加载历史记录
	*/
	const reloadHistory = (0, react.useCallback)(() => {
		try {
			if (onLoadHistory) {
				const loadedHistory = onLoadHistory();
				setHistory(Array.isArray(loadedHistory) ? loadedHistory : []);
			} else {
				const loadedHistory = require_storage.loadHistory(historyKey);
				setHistory(Array.isArray(loadedHistory) ? loadedHistory : []);
			}
		} catch (error) {
			console.error("Failed to reload history:", error);
			setHistory([]);
		}
	}, [historyKey, onLoadHistory]);
	/**
	* 初始化：加载历史记录
	*/
	(0, react.useEffect)(() => {
		reloadHistory();
	}, [reloadHistory]);
	return {
		history,
		addHistory: (0, react.useCallback)((keyword) => {
			if (!keyword || !keyword.trim()) return;
			const trimmedKeyword = keyword.trim();
			if (onSaveHistory) {
				const newHistory = [trimmedKeyword, ...(Array.isArray(history) ? history : []).filter((h) => h !== trimmedKeyword)].slice(0, maxHistoryCount);
				setHistory(newHistory);
				onSaveHistory(newHistory);
			} else setHistory(require_storage.addHistoryItem(historyKey, trimmedKeyword, maxHistoryCount));
		}, [
			history,
			historyKey,
			maxHistoryCount,
			onSaveHistory
		]),
		removeHistory: (0, react.useCallback)((keyword) => {
			if (onSaveHistory) {
				const newHistory = (Array.isArray(history) ? history : []).filter((h) => h !== keyword);
				setHistory(newHistory);
				onSaveHistory(newHistory);
			} else setHistory(require_storage.removeHistoryItem(historyKey, keyword));
		}, [
			history,
			historyKey,
			onSaveHistory
		]),
		clearAllHistory: (0, react.useCallback)(() => {
			if (onSaveHistory) {
				setHistory([]);
				onSaveHistory([]);
			} else {
				require_storage.clearHistory(historyKey);
				setHistory([]);
			}
		}, [historyKey, onSaveHistory]),
		reloadHistory
	};
};
//#endregion
exports.useSearchHistory = useSearchHistory;
