const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_index = require("../../locales/index.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_PisellLookup = require("../pisellLookup/PisellLookup.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
//#region src/components/pisellFind/index.tsx
const _excluded = [
	"triggerType",
	"buttonText",
	"placeholder",
	"showConfirmButton",
	"showHistory",
	"historyKey",
	"enableScanner",
	"onScannerData",
	"onSearch"
];
/**
* 原生扫码事件名，对齐宿主端约定。
*/
const SCAN_RESULT_EVENT = "nativeScanResult";
/**
* PisellFind 组件
* 基于 PisellLookup 的组件内查找优化版本
*
* 核心特点：
* - 默认使用 button 触发器
* - 默认不显示确认按钮（输入即搜索）
* - 默认开启搜索历史
* - 默认不展示结果区（结果在页面其他位置展示）
* - 通过 `enableScanner` 接入扫码枪：使用宿主 `app.pubsub.subscribe(SCAN_RESULT_EVENT, ...)`，
*   每次扫码直接覆盖输入框内容并触发 onSearch / onScannerData；切回 false 时自动 unsubscribe。
*
* @example
* ```tsx
* <PisellFind
*   triggerType="input"
*   enableScanner={isActive}
*   onScannerData={(payload) => console.log('scan:', payload.data)}
*   onSearch={(keyword) => filterData(keyword)}
* />
* ```
*/
const PisellFind = (0, react.forwardRef)((props, ref) => {
	var _engineContext$appHel;
	const { triggerType = "button", buttonText = (() => {
		const key = "pisell2.recordBoard.toolbar.find";
		const u = _pisell_utils.locales.getText(key);
		return u && u !== key ? u : require_index.getText(key) || require_index.getText("pisell-find-button-text");
	})(), placeholder = (() => {
		const key = "pisell2.recordBoard.toolbar.findPlaceholder";
		const u = _pisell_utils.locales.getText(key);
		return u && u !== key ? u : require_index.getText(key) || require_index.getText("pisell-find-placeholder");
	})(), showConfirmButton = false, showHistory = true, historyKey = "pisell_find_history", enableScanner = false, onScannerData, onSearch } = props, restProps = require_objectWithoutProperties._objectWithoutProperties(props, _excluded);
	const lookupRef = (0, react.useRef)(null);
	/** 透传给监听回调的最新引用，避免回调闭包 stale */
	const onScannerDataRef = (0, react.useRef)(onScannerData);
	const onSearchRef = (0, react.useRef)(onSearch);
	(0, react.useEffect)(() => {
		onScannerDataRef.current = onScannerData;
	}, [onScannerData]);
	(0, react.useEffect)(() => {
		onSearchRef.current = onSearch;
	}, [onSearch]);
	(0, react.useImperativeHandle)(ref, () => ({
		open: () => {
			var _lookupRef$current;
			return (_lookupRef$current = lookupRef.current) === null || _lookupRef$current === void 0 ? void 0 : _lookupRef$current.open();
		},
		close: () => {
			var _lookupRef$current2;
			return (_lookupRef$current2 = lookupRef.current) === null || _lookupRef$current2 === void 0 ? void 0 : _lookupRef$current2.close();
		},
		getKeyword: () => {
			var _lookupRef$current$ge, _lookupRef$current3;
			return (_lookupRef$current$ge = (_lookupRef$current3 = lookupRef.current) === null || _lookupRef$current3 === void 0 ? void 0 : _lookupRef$current3.getKeyword()) !== null && _lookupRef$current$ge !== void 0 ? _lookupRef$current$ge : "";
		},
		setKeyword: (v) => {
			var _lookupRef$current4;
			return (_lookupRef$current4 = lookupRef.current) === null || _lookupRef$current4 === void 0 ? void 0 : _lookupRef$current4.setKeyword(v);
		}
	}), []);
	/** 通过 useEngineContext 拿到宿主 app（含 pubsub.subscribe / unsubscribe） */
	const engineContext = require_useEngineContext.default();
	/**
	* 用 ref 稳定 getApp 引用：低代码环境下 engineContext 每次渲染可能返回新引用，
	* 若把 getApp 直接放进 effect 依赖会导致频繁 unsubscribe/subscribe（甚至漏掉扫码事件）。
	* effect 依赖只保留 enableScanner，运行时通过 ref 拿最新的 getApp。
	*/
	const getAppRef = (0, react.useRef)(void 0);
	getAppRef.current = typeof (engineContext === null || engineContext === void 0 || (_engineContext$appHel = engineContext.appHelper) === null || _engineContext$appHel === void 0 || (_engineContext$appHel = _engineContext$appHel.utils) === null || _engineContext$appHel === void 0 ? void 0 : _engineContext$appHel.getApp) === "function" ? engineContext.appHelper.utils.getApp : void 0;
	(0, react.useEffect)(() => {
		if (!enableScanner) return void 0;
		const getApp = getAppRef.current;
		if (typeof getApp !== "function") return void 0;
		const app = getApp();
		const pubsub = app === null || app === void 0 ? void 0 : app.pubsub;
		if (!pubsub || typeof pubsub.subscribe !== "function") return;
		/**
		* 监听回调：每次扫码直接覆盖输入框。
		* ScanResultPayload: { code, data }，取 code 作为输入框值与搜索关键词。
		* setKeyword 在受控模式下会触发外部 onChange；onSearch 需要显式触发以驱动后续搜索流程
		* （受控模式下宿主一般只通过 onSearch 拿到新关键词，不会在 onChange 里再触发一次 onSearch，
		*  因此这里不会产生重复请求。如需自定义合流，可绑定 onScannerData 自行处理）。
		*
		* 非法 payload（无 code 或 code 为空串）直接忽略，避免把输入框/搜索条件意外清空。
		*/
		const onScan = (payload) => {
			var _lookupRef$current5, _onSearchRef$current, _onScannerDataRef$cur;
			const code = payload === null || payload === void 0 ? void 0 : payload.code;
			if (typeof code !== "string" || code.length === 0) return;
			(_lookupRef$current5 = lookupRef.current) === null || _lookupRef$current5 === void 0 || _lookupRef$current5.setKeyword(code);
			(_onSearchRef$current = onSearchRef.current) === null || _onSearchRef$current === void 0 || _onSearchRef$current.call(onSearchRef, code);
			(_onScannerDataRef$cur = onScannerDataRef.current) === null || _onScannerDataRef$cur === void 0 || _onScannerDataRef$cur.call(onScannerDataRef, payload);
		};
		try {
			pubsub.subscribe(SCAN_RESULT_EVENT, onScan);
		} catch (e) {
			console.warn("[PisellFind] subscribe scanner failed:", e);
			return;
		}
		return () => {
			try {
				var _pubsub$unsubscribe;
				(_pubsub$unsubscribe = pubsub.unsubscribe) === null || _pubsub$unsubscribe === void 0 || _pubsub$unsubscribe.call(pubsub, "nativeScanResult", onScan);
			} catch (e) {
				console.warn("[PisellFind] unsubscribe scanner failed:", e);
			}
		};
	}, [enableScanner]);
	return /* @__PURE__ */ react.default.createElement(require_PisellLookup.PisellLookup, require_objectSpread2._objectSpread2({
		ref: lookupRef,
		triggerType,
		buttonText,
		placeholder,
		showConfirmButton,
		showHistory,
		historyKey,
		onSearch
	}, restProps));
});
PisellFind.displayName = "PisellFind";
//#endregion
exports.PisellFind = PisellFind;
