import { getText } from "../../../locales/index.js";
import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import IconFont from "../../iconfont/index.js";
import useEngineContext from "../../../hooks/useEngineContext.js";
import PisellQRScanner from "../../pisellQRScanner/index.js";
import React, { cloneElement, forwardRef, isValidElement, useImperativeHandle, useRef, useState } from "react";
import { Button, Input } from "antd";
import { CloseOutlined, SearchOutlined } from "@ant-design/icons";
//#region src/components/pisellLookup/components/LookupTrigger.tsx
/**
* LookupTrigger 组件
* 触发器层：支持多种触发器类型
*/
const LookupTriggerComponent = forwardRef((props, ref) => {
	var _context$appHelper;
	const { type = "input", trigger, triggerText, triggerIcon, buttonText = getText("pisell-lookup-button-text"), disabled = false, placeholder = getText("pisell-lookup-placeholder"), size = "middle", showPrefixIcon = false, prefixIcon, showClear = true, showConfirmButton = true, confirmButtonText = getText("pisell-lookup-confirm-button-text"), showScan = true, value, onChange, onClick, onFocus, onConfirm, onScan, onClear, onBlur, autoFocus = false, className, style } = props;
	const { getApp } = ((_context$appHelper = useEngineContext().appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	/**
	* Input 输入框的 ref
	*/
	const inputRef = useRef(null);
	/**
	* 暴露给外部的方法
	*/
	useImperativeHandle(ref, () => ({
		focus: () => {
			var _inputRef$current;
			(_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || _inputRef$current.focus();
		},
		select: () => {
			var _inputRef$current2;
			(_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 || _inputRef$current2.select();
		}
	}), []);
	/**
	* 扫码弹窗可见性状态
	*/
	const [scanVisible, setScanVisible] = useState(false);
	/**
	* 处理输入变化
	*/
	const handleChange = (e) => {
		onChange === null || onChange === void 0 || onChange(e.target.value);
	};
	/**
	* 处理清除
	*/
	const handleClear = () => {
		onChange === null || onChange === void 0 || onChange("");
		onClear === null || onClear === void 0 || onClear();
	};
	/**
	* @title: 获取扫码插件示例
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	*/
	const getAppScanBarcodePlugin = () => {
		if (getApp) {
			var _app$getPlugin;
			const app = getApp();
			const scanBarcode = app === null || app === void 0 || (_app$getPlugin = app.getPlugin("scan")) === null || _app$getPlugin === void 0 ? void 0 : _app$getPlugin.scanBarcode;
			if (scanBarcode) return scanBarcode;
		}
		return null;
	};
	/**
	* @title: 在APP环境下使用插件进行扫码
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	*/
	const appScan = () => {
		const app = getApp();
		try {
			getAppScanBarcodePlugin()({ hint: 17 }).then((res) => onScan === null || onScan === void 0 ? void 0 : onScan(res === null || res === void 0 ? void 0 : res.ScanResult)).catch((err) => {
				app.logger.addLog({
					type: "info",
					title: "扫码失败",
					metadata: { err }
				});
			});
		} catch (err) {
			app.logger.addLog({
				type: "error",
				title: "扫码代码执行异常",
				metadata: { err }
			});
		}
	};
	/**
	* 处理扫码按钮点击
	*/
	const handleScanClick = () => {
		if (getAppScanBarcodePlugin()) {
			appScan();
			return;
		}
		setScanVisible(true);
	};
	/**
	* 处理扫码成功
	*/
	const handleScanSuccess = (code) => {
		setScanVisible(false);
		onScan === null || onScan === void 0 || onScan(code);
	};
	/**
	* 处理扫码关闭
	*/
	const handleScanClose = () => {
		setScanVisible(false);
	};
	/**
	* 处理确认
	*/
	const handleConfirm = () => {
		onConfirm === null || onConfirm === void 0 || onConfirm();
	};
	/**
	* 处理按 Enter 键：传入当前输入值，避免受控模式下 state 未更新导致历史未保存
	*/
	const handleKeyPress = (e) => {
		if (e.key === "Enter") {
			var _ref, _e$currentTarget$valu, _e$currentTarget;
			const currentValue = (_ref = (_e$currentTarget$valu = (_e$currentTarget = e.currentTarget) === null || _e$currentTarget === void 0 ? void 0 : _e$currentTarget.value) !== null && _e$currentTarget$valu !== void 0 ? _e$currentTarget$valu : value) !== null && _ref !== void 0 ? _ref : "";
			onConfirm === null || onConfirm === void 0 || onConfirm(currentValue);
		}
	};
	/**
	* 渲染输入框触发器
	*/
	const renderInputTrigger = () => {
		const prefix = showPrefixIcon ? prefixIcon || /* @__PURE__ */ React.createElement(SearchOutlined, null) : void 0;
		const suffix = /* @__PURE__ */ React.createElement("div", { className: "pisell-lookup-input-suffix" }, showClear && value && /* @__PURE__ */ React.createElement("span", {
			className: "pisell-lookup-input-clear",
			onClick: handleClear
		}, /* @__PURE__ */ React.createElement(CloseOutlined, null), /* @__PURE__ */ React.createElement("span", { className: "pisell-lookup-input-clear-text" }, getText("pisell-lookup-clear"))), showScan && /* @__PURE__ */ React.createElement(IconFont, {
			className: "pisell-lookup-input-scan",
			type: "pisell2-scan",
			onClick: handleScanClick,
			style: { fontSize: size === "small" ? 18 : size === "large" ? 26 : 24 }
		}), showConfirmButton && /* @__PURE__ */ React.createElement(Button, {
			type: "primary",
			size,
			onClick: handleConfirm,
			className: "pisell-lookup-input-confirm"
		}, confirmButtonText));
		const inputEl = /* @__PURE__ */ React.createElement(Input, {
			ref: inputRef,
			value,
			onChange: handleChange,
			onFocus,
			onBlur,
			onPressEnter: handleKeyPress,
			placeholder,
			size,
			disabled,
			prefix,
			suffix,
			autoFocus,
			className: `pisell-lookup-trigger-input ${className || ""}`.trim(),
			style
		});
		if (!showConfirmButton) return /* @__PURE__ */ React.createElement("div", { className: "pisell-lookup-trigger-input pisell-lookup-trigger-input-no-confirm" }, inputEl);
		return inputEl;
	};
	/**
	* 渲染按钮触发器
	*/
	const renderButtonTrigger = () => {
		return /* @__PURE__ */ React.createElement(Button, {
			type: "primary",
			icon: /* @__PURE__ */ React.createElement(SearchOutlined, null),
			onClick,
			disabled,
			size,
			className: `pisell-lookup-trigger-button ${className || ""}`,
			style
		}, buttonText);
	};
	/**
	* 渲染图标触发器
	*/
	const renderIconTrigger = () => {
		const icon = triggerIcon || /* @__PURE__ */ React.createElement(SearchOutlined, { style: { fontSize: 20 } });
		return /* @__PURE__ */ React.createElement("div", {
			className: `pisell-lookup-trigger-icon ${className || ""}`,
			onClick: disabled ? void 0 : onClick,
			style: _objectSpread2({
				cursor: disabled ? "not-allowed" : "pointer",
				opacity: disabled ? .5 : 1
			}, style)
		}, icon);
	};
	/**
	* 渲染文本触发器
	*/
	const renderTextTrigger = () => {
		const text = triggerText || "搜索";
		return /* @__PURE__ */ React.createElement("span", {
			className: `pisell-lookup-trigger-text ${className || ""}`,
			onClick: disabled ? void 0 : onClick,
			style: _objectSpread2({
				cursor: disabled ? "not-allowed" : "pointer",
				opacity: disabled ? .5 : 1
			}, style)
		}, text);
	};
	/**
	* 渲染自定义触发器
	*/
	const renderCustomTrigger = () => {
		if (!trigger) return null;
		if (isValidElement(trigger)) return cloneElement(trigger, {
			onClick: disabled ? void 0 : onClick,
			style: _objectSpread2({
				cursor: disabled ? "not-allowed" : "pointer",
				opacity: disabled ? .5 : 1
			}, trigger.props.style || {})
		});
		return /* @__PURE__ */ React.createElement("div", {
			className: `pisell-lookup-trigger-custom ${className || ""}`,
			onClick: disabled ? void 0 : onClick,
			style: _objectSpread2({
				cursor: disabled ? "not-allowed" : "pointer",
				opacity: disabled ? .5 : 1
			}, style)
		}, trigger);
	};
	/**
	* 渲染触发器
	*/
	const renderTrigger = () => {
		if (trigger) return renderCustomTrigger();
		switch (type) {
			case "input": return renderInputTrigger();
			case "button": return renderButtonTrigger();
			case "icon": return renderIconTrigger();
			case "text": return renderTextTrigger();
			default: return renderInputTrigger();
		}
	};
	return /* @__PURE__ */ React.createElement(React.Fragment, null, renderTrigger(), showScan && /* @__PURE__ */ React.createElement(PisellQRScanner, {
		visible: scanVisible,
		onClose: handleScanClose,
		onScan: handleScanSuccess
	}));
});
LookupTriggerComponent.displayName = "LookupTrigger";
const LookupTrigger = LookupTriggerComponent;
//#endregion
export { LookupTrigger };
