require("../../../_virtual/_rolldown/runtime.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
let react = require("react");
//#region src/components/eftposPay/huifu/hooks.ts
/**
* 获取扫码设备连接状态
*/
const useScannerDevice = () => {
	var _context$appHelper2;
	const [scannerStatus, setScannerStatus] = (0, react.useState)(false);
	const { interaction } = ((_context$appHelper2 = require_useEngineContext.default().appHelper) === null || _context$appHelper2 === void 0 ? void 0 : _context$appHelper2.utils) || {};
	(0, react.useEffect)(() => {
		var _interaction$utils2, _interaction$utils2$p, _interaction$utils3, _interaction$utils3$m;
		interaction === null || interaction === void 0 || (_interaction$utils2 = interaction.utils) === null || _interaction$utils2 === void 0 || (_interaction$utils2$p = _interaction$utils2.postMessageToApp) === null || _interaction$utils2$p === void 0 || _interaction$utils2$p.call(_interaction$utils2, {
			module: "global",
			key: "get_peripherals_status",
			data: {}
		});
		interaction === null || interaction === void 0 || (_interaction$utils3 = interaction.utils) === null || _interaction$utils3 === void 0 || (_interaction$utils3$m = _interaction$utils3.mountFunction) === null || _interaction$utils3$m === void 0 || _interaction$utils3$m.call(_interaction$utils3, "global", "peripheralsStatus", (strPeripherals) => {
			try {
				const peripherals = JSON.parse(strPeripherals);
				setScannerStatus((peripherals === null || peripherals === void 0 ? void 0 : peripherals.scanner) == "connected");
			} catch (err) {
				console.error(err);
			}
		});
	}, []);
	return { scannerStatus };
};
/**
* 摄像头激活和状态获取
*/
const useCameraDevice = () => {
	var _context$appHelper3, _interaction$utils5, _interaction$utils5$i;
	const { interaction, isTerminal } = ((_context$appHelper3 = require_useEngineContext.default().appHelper) === null || _context$appHelper3 === void 0 ? void 0 : _context$appHelper3.utils) || {};
	return {
		activeCamera: (0, react.useCallback)(() => {
			var _interaction$utils4, _interaction$utils4$p;
			interaction === null || interaction === void 0 || (_interaction$utils4 = interaction.utils) === null || _interaction$utils4 === void 0 || (_interaction$utils4$p = _interaction$utils4.postMessageToApp) === null || _interaction$utils4$p === void 0 || _interaction$utils4$p.call(_interaction$utils4, {
				module: "global",
				key: "active_native_scanner",
				data: { source: "eftposPay_huifu" }
			});
		}, []),
		cameraStatus: isTerminal && isTerminal() || (interaction === null || interaction === void 0 || (_interaction$utils5 = interaction.utils) === null || _interaction$utils5 === void 0 || (_interaction$utils5$i = _interaction$utils5.isAppEnv) === null || _interaction$utils5$i === void 0 ? void 0 : _interaction$utils5$i.call(_interaction$utils5))
	};
};
/**
* 获取外部设备扫描结果
*/
const useScanListener = () => {
	var _context$appHelper4, _interaction$utils6, _interaction$utils6$i;
	const { interaction, isTerminal } = ((_context$appHelper4 = require_useEngineContext.default().appHelper) === null || _context$appHelper4 === void 0 ? void 0 : _context$appHelper4.utils) || {};
	const [scanResult, setScanResult] = (0, react.useState)({
		type: "scanner",
		value: ""
	});
	const isNativeEnv = isTerminal && isTerminal() || (interaction === null || interaction === void 0 || (_interaction$utils6 = interaction.utils) === null || _interaction$utils6 === void 0 || (_interaction$utils6$i = _interaction$utils6.isAppEnv) === null || _interaction$utils6$i === void 0 ? void 0 : _interaction$utils6$i.call(_interaction$utils6));
	(0, react.useEffect)(() => {
		var _interaction$utils7, _interaction$utils7$m;
		interaction === null || interaction === void 0 || (_interaction$utils7 = interaction.utils) === null || _interaction$utils7 === void 0 || (_interaction$utils7$m = _interaction$utils7.mountFunction) === null || _interaction$utils7$m === void 0 || _interaction$utils7$m.call(_interaction$utils7, "global", "peripheralsResult", (strVal) => {
			try {
				setScanResult(JSON.parse(strVal));
			} catch (err) {
				console.error(err);
				setScanResult({
					type: "scanner",
					value: ""
				});
			}
		});
		let inputBuffer = "";
		let lastInputTime = 0;
		let inputTimer = null;
		const handleKeyDown = (e) => {
			if (e.key === "Shift" || e.key === "Control" || e.key === "Alt" || e.key === "Meta" || e.key === "Tab" || e.key === "CapsLock" || e.key === "Escape" || e.key.startsWith("Arrow") || e.ctrlKey || e.metaKey) return;
			const currentTime = Date.now();
			const timeDiff = currentTime - lastInputTime;
			if (e.key === "Enter") {
				const trimmedCode = inputBuffer.trim();
				if (trimmedCode.length >= 10) setScanResult({
					type: "scanner",
					value: trimmedCode
				});
				inputBuffer = "";
				lastInputTime = 0;
				if (inputTimer) {
					clearTimeout(inputTimer);
					inputTimer = null;
				}
				return;
			}
			if (timeDiff < 30 && inputBuffer.length > 0) inputBuffer += e.key;
			else inputBuffer = e.key;
			lastInputTime = currentTime;
			if (inputTimer) clearTimeout(inputTimer);
			inputTimer = setTimeout(() => {
				const trimmedCode = inputBuffer.trim();
				if (trimmedCode.length >= 10) setScanResult({
					type: "scanner",
					value: trimmedCode
				});
				inputBuffer = "";
			}, 100);
		};
		if (!isNativeEnv) document.addEventListener("keydown", handleKeyDown);
		return () => {
			if (!isNativeEnv) document.removeEventListener("keydown", handleKeyDown);
			if (inputTimer) clearTimeout(inputTimer);
		};
	}, []);
	return { scanResult };
};
//#endregion
exports.useCameraDevice = useCameraDevice;
exports.useScanListener = useScanListener;
exports.useScannerDevice = useScannerDevice;
