import { _objectWithoutProperties } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import PisellCard from "../pisellCard/index.js";
import IconFont from "../iconfont/index.js";
import usePisellConfig from "../pisell-config-provider/hooks/usePisellConfig.js";
import { _asyncToGenerator } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import Button from "../button/index.js";
import { entryModeErrorTextIcon, entryModeTextIcon, passiveModeType } from "./constants.js";
import Scan from "./Scan/index.js";
import PisellEntryInput from "./Input/index.js";
import { hasCamera } from "./utils.js";
import useTerminalPeripherals from "./hooks/useTerminalPeripherals.js";
import { useNativeScanner } from "./hooks/useNativeScanner.js";
import React, { useEffect, useMemo, useState } from "react";
import classNames from "classnames";
import "./index.less";
//#region src/components/pisellInformationEntry/index.tsx
const _excluded = ["icon", "children"];
const CardButton = (props) => {
	const { icon, children } = props, others = _objectWithoutProperties(props, _excluded);
	return /* @__PURE__ */ React.createElement(PisellCard, others, /* @__PURE__ */ React.createElement("div", null, icon), /* @__PURE__ */ React.createElement("div", null, children));
};
const PisellInformationEntry = (props) => {
	const { platform: propsPlatform, title, entryModes = [], searchDescribe, onChange, onChangeType } = props;
	const { platform } = usePisellConfig();
	const [currentType, setCurrentType] = useState(null);
	const { peripheralsStatus } = useTerminalPeripherals(onChange);
	const { activeNativeScanner, isTerminal } = useNativeScanner(onChange);
	const [availableEntryModes, setAvailableEntryModes] = useState(entryModes);
	useEffect(() => {
		document.body.id = "body";
	}, []);
	useEffect(() => {
		(function() {
			var _ref = _asyncToGenerator(function* () {
				let available = entryModes;
				if (!(yield 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 (passiveModeType.includes(type)) return;
		if (isTerminal && "scanCode" === type) {
			setCurrentType(null);
			activeNativeScanner();
			return;
		}
		setCurrentType(type);
	};
	useEffect(() => {
		onChangeType && onChangeType(currentType);
	}, [currentType]);
	const { isHasInput, isHasNfc, isHasScanner, isHasScanCode } = 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 = useMemo(() => {
		if (!currentType) return true;
		if (availableEntryModes.length === 1 && passiveModeType.includes(availableEntryModes[0].type)) return true;
		return false;
	}, [currentType, availableEntryModes]);
	return /* @__PURE__ */ React.createElement("div", { className: "pisell-information-entry-wrap" }, /* @__PURE__ */ React.createElement("div", { className: "pisell-information-entry-title" }, /* @__PURE__ */ React.createElement("span", null, title), currentType && isHasScanCode && isHasInput && /* @__PURE__ */ React.createElement("div", { className: "pisell-information-entry-title-action-wrap" }, isHasScanCode && /* @__PURE__ */ React.createElement(Button, {
		size: "large",
		type: currentType === "scanCode" ? "primary" : "default",
		icon: /* @__PURE__ */ React.createElement(IconFont, { type: entryModeTextIcon["scanCode"].icon }),
		onClick: () => handleEntryClick("scanCode")
	}), isHasInput && /* @__PURE__ */ React.createElement(Button, {
		size: "large",
		type: currentType === "input" ? "primary" : "default",
		icon: /* @__PURE__ */ React.createElement(IconFont, { type: entryModeTextIcon["input"].icon }),
		onClick: () => handleEntryClick("input")
	}))), isShowEntryModes && /* @__PURE__ */ React.createElement("div", { className: "pisell-information-entry-modes" }, availableEntryModes.map((item) => {
		const detail = entryModeTextIcon[item.type];
		let icon = detail.icon;
		if (["scanner", "nfc"].includes(item.type)) {
			const disconnect = entryModeErrorTextIcon[item.type];
			icon = peripheralsStatus[item.type] === "connected" ? detail.icon : disconnect.icon;
		}
		return /* @__PURE__ */ React.createElement(CardButton, {
			bordered: !passiveModeType.includes(item.type),
			icon: /* @__PURE__ */ React.createElement(IconFont, {
				type: icon,
				className: "pisell-card-button-icon"
			}),
			className: classNames("pisell-information-entry-item", { ["pisell-entry-ready"]: passiveModeType.includes(item.type) }),
			onClick: () => handleEntryClick(item.type)
		}, item.name || detail.text);
	})), /* @__PURE__ */ React.createElement("div", null, currentType === "scanCode" && !isTerminal && /* @__PURE__ */ React.createElement(Scan, {
		onChange: handleChange,
		onCancel: () => setCurrentType(null)
	}), currentType === "input" && /* @__PURE__ */ React.createElement(PisellEntryInput, {
		onChange: handleChange,
		searchDescribe
	})));
};
//#endregion
export { PisellInformationEntry as default };
