import { useCallback, useState } from "react";
//#region src/components/pisellLookup/hooks/useSearchState.ts
/**
* 搜索状态管理 Hook
* 支持受控和非受控模式
*/
const useSearchState = (options) => {
	const { value, defaultValue = "", visible: visibleProp, defaultVisible = false, onChange, onVisibleChange, onOpen, onClose } = options;
	const isControlledKeyword = value !== void 0;
	const isControlledVisible = visibleProp !== void 0;
	const [internalKeyword, setInternalKeyword] = useState(defaultValue);
	const [internalVisible, setInternalVisible] = useState(defaultVisible);
	const keyword = isControlledKeyword ? value : internalKeyword;
	const visible = isControlledVisible ? visibleProp : internalVisible;
	/**
	* 设置关键词
	*/
	const setKeyword = useCallback((newValue) => {
		if (!isControlledKeyword) setInternalKeyword(newValue);
		onChange === null || onChange === void 0 || onChange(newValue);
	}, [isControlledKeyword, onChange]);
	/**
	* 设置可见性
	*/
	const setVisible = useCallback((newVisible) => {
		const oldVisible = visible;
		if (!isControlledVisible) setInternalVisible(newVisible);
		onVisibleChange === null || onVisibleChange === void 0 || onVisibleChange(newVisible);
		if (newVisible && !oldVisible) onOpen === null || onOpen === void 0 || onOpen();
		else if (!newVisible && oldVisible) onClose === null || onClose === void 0 || onClose();
	}, [
		isControlledVisible,
		visible,
		onVisibleChange,
		onOpen,
		onClose
	]);
	return {
		keyword,
		setKeyword,
		visible,
		setVisible,
		open: useCallback(() => {
			setVisible(true);
		}, [setVisible]),
		close: useCallback(() => {
			setVisible(false);
		}, [setVisible]),
		toggle: useCallback(() => {
			setVisible(!visible);
		}, [setVisible, visible]),
		isControlledKeyword,
		isControlledVisible
	};
};
//#endregion
export { useSearchState };
