require("../../../_virtual/_rolldown/runtime.js");
let react = require("react");
//#region src/components/pisellLookup/hooks/useDebounceSearch.ts
/**
* 防抖搜索 Hook
* 用于实时搜索模式的防抖处理
*/
const useDebounceSearch = (options) => {
	const { keyword, debounceTime, onSearch, enabled = true, lastSearchKeyword } = options;
	const [debouncedKeyword, setDebouncedKeyword] = (0, react.useState)(keyword);
	const timerRef = (0, react.useRef)(null);
	/**
	* 防抖处理
	*/
	(0, react.useEffect)(() => {
		if (!enabled) {
			setDebouncedKeyword(keyword);
			return;
		}
		if (timerRef.current) clearTimeout(timerRef.current);
		timerRef.current = setTimeout(() => {
			setDebouncedKeyword(keyword);
		}, debounceTime);
		return () => {
			if (timerRef.current) clearTimeout(timerRef.current);
		};
	}, [
		keyword,
		debounceTime,
		enabled
	]);
	/**
	* 触发搜索回调
	*/
	(0, react.useEffect)(() => {
		if (enabled && onSearch && debouncedKeyword !== void 0) {
			if (lastSearchKeyword !== void 0 && debouncedKeyword === lastSearchKeyword) return;
			try {
				onSearch(debouncedKeyword);
			} catch (error) {
				console.error("Failed to trigger search:", error);
			}
		}
	}, [
		debouncedKeyword,
		onSearch,
		enabled,
		lastSearchKeyword
	]);
	return { debouncedKeyword };
};
//#endregion
exports.useDebounceSearch = useDebounceSearch;
