const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectWithoutProperties = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_index = require("../pisellCard/index.js");
const require_index$1 = require("../iconfont/index.js");
const require_usePisellConfig = require("../pisell-config-provider/hooks/usePisellConfig.js");
const require_asyncToGenerator = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_index$2 = require("../button/index.js");
const require_constants = require("./constants.js");
const require_index$3 = require("./Scan/index.js");
const require_index$4 = require("./Input/index.js");
const require_utils = require("./utils.js");
const require_useTerminalPeripherals = require("./hooks/useTerminalPeripherals.js");
const require_useNativeScanner = require("./hooks/useNativeScanner.js");
let react = require("react");
react = require_runtime.__toESM(react);
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./index.less");
//#region src/components/pisellInformationEntry/index.tsx
const _excluded = ["icon", "children"];
const CardButton = (props) => {
	const { icon, children } = props, others = require_objectWithoutProperties._objectWithoutProperties(props, _excluded);
	return /* @__PURE__ */ react.default.createElement(require_index.default, others, /* @__PURE__ */ react.default.createElement("div", null, icon), /* @__PURE__ */ react.default.createElement("div", null, children));
};
const PisellInformationEntry = (props) => {
	const { platform: propsPlatform, title, entryModes = [], searchDescribe, onChange, onChangeType } = props;
	const { platform } = require_usePisellConfig.default();
	const [currentType, setCurrentType] = (0, react.useState)(null);
	const { peripheralsStatus } = require_useTerminalPeripherals.default(onChange);
	const { activeNativeScanner, isTerminal } = require_useNativeScanner.useNativeScanner(onChange);
	const [availableEntryModes, setAvailableEntryModes] = (0, react.useState)(entryModes);
	(0, react.useEffect)(() => {
		document.body.id = "body";
	}, []);
	(0, react.useEffect)(() => {
		(function() {
			var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
				let available = entryModes;
				if (!(yield require_utils.hasCamera())) available = entryModes.filter((item) => item.type !== "scanCode");
				available = available.filter((item) => {
					if (item.type === "scanner") return peripheralsStatus.scanner !== "none";
					if (item.type === "nfc") return peripheralsStatus.nfc !== "none";
					return true;
				});
				if (available.length === 1) setCurrentType(available[0].type);
				setAvailableEntryModes(available);
			});
			return function getEntryModes() {
				return _ref.apply(this, arguments);
			};
		})()();
	}, [entryModes, peripheralsStatus]);
	const handleChange = (val) => {
		onChange === null || onChange === void 0 || onChange(val);
	};
	const handleEntryClick = (type) => {
		if (require_constants.passiveModeType.includes(type)) return;
		if (isTerminal && "scanCode" === type) {
			setCurrentType(null);
			activeNativeScanner();
			return;
		}
		setCurrentType(type);
	};
	(0, react.useEffect)(() => {
		onChangeType && onChangeType(currentType);
	}, [currentType]);
	const { isHasInput, isHasNfc, isHasScanner, isHasScanCode } = (0, react.useMemo)(() => {
		let isHasScanCode = false;
		let isHasInput = false;
		let isHasScanner = false;
		let isHasNfc = false;
		availableEntryModes.forEach((item) => {
			if (item.type === "scanCode") isHasScanCode = true;
			if (item.type === "input") isHasInput = true;
			if (item.type === "scanner") isHasScanner = true;
			if (item.type === "nfc") isHasNfc = true;
		});
		return {
			isHasScanCode,
			isHasInput,
			isHasScanner,
			isHasNfc
		};
	}, [availableEntryModes]);
	const isShowEntryModes = (0, react.useMemo)(() => {
		if (!currentType) return true;
		if (availableEntryModes.length === 1 && require_constants.passiveModeType.includes(availableEntryModes[0].type)) return true;
		return false;
	}, [currentType, availableEntryModes]);
	return /* @__PURE__ */ react.default.createElement("div", { className: "pisell-information-entry-wrap" }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-information-entry-title" }, /* @__PURE__ */ react.default.createElement("span", null, title), currentType && isHasScanCode && isHasInput && /* @__PURE__ */ react.default.createElement("div", { className: "pisell-information-entry-title-action-wrap" }, isHasScanCode && /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		size: "large",
		type: currentType === "scanCode" ? "primary" : "default",
		icon: /* @__PURE__ */ react.default.createElement(require_index$1.default, { type: require_constants.entryModeTextIcon["scanCode"].icon }),
		onClick: () => handleEntryClick("scanCode")
	}), isHasInput && /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		size: "large",
		type: currentType === "input" ? "primary" : "default",
		icon: /* @__PURE__ */ react.default.createElement(require_index$1.default, { type: require_constants.entryModeTextIcon["input"].icon }),
		onClick: () => handleEntryClick("input")
	}))), isShowEntryModes && /* @__PURE__ */ react.default.createElement("div", { className: "pisell-information-entry-modes" }, availableEntryModes.map((item) => {
		const detail = require_constants.entryModeTextIcon[item.type];
		let icon = detail.icon;
		if (["scanner", "nfc"].includes(item.type)) {
			const disconnect = require_constants.entryModeErrorTextIcon[item.type];
			icon = peripheralsStatus[item.type] === "connected" ? detail.icon : disconnect.icon;
		}
		return /* @__PURE__ */ react.default.createElement(CardButton, {
			bordered: !require_constants.passiveModeType.includes(item.type),
			icon: /* @__PURE__ */ react.default.createElement(require_index$1.default, {
				type: icon,
				className: "pisell-card-button-icon"
			}),
			className: (0, classnames.default)("pisell-information-entry-item", { ["pisell-entry-ready"]: require_constants.passiveModeType.includes(item.type) }),
			onClick: () => handleEntryClick(item.type)
		}, item.name || detail.text);
	})), /* @__PURE__ */ react.default.createElement("div", null, currentType === "scanCode" && !isTerminal && /* @__PURE__ */ react.default.createElement(require_index$3.default, {
		onChange: handleChange,
		onCancel: () => setCurrentType(null)
	}), currentType === "input" && /* @__PURE__ */ react.default.createElement(require_index$4.default, {
		onChange: handleChange,
		searchDescribe
	})));
};
//#endregion
exports.default = PisellInformationEntry;
