const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_locales = require("../../utils/locales.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_index = require("../booking/components/drawer/index.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_index$1 = require("../../utils/index.js");
const require_const = require("./const.js");
const require_table = require("./table.js");
const require_device = require("./device.js");
const require_pos = require("./pos.js");
const require_hooks = require("./hooks.js");
const require_log = require("./log.js");
const require_locales$1 = require("./locales.js");
const require_apiKey = require("./icon/apiKey.js");
const require_device$1 = require("./icon/device.js");
const require_hooks$1 = require("./PairModal/hooks.js");
const require_index$2 = require("./PairModal/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let src_components_eftpos_assets_mx51SCI_svg = require("./assets/mx51SCI.svg");
src_components_eftpos_assets_mx51SCI_svg = require_runtime.__toESM(src_components_eftpos_assets_mx51SCI_svg);
require("./index.less");
//#region src/components/eftpos/index.tsx
_pisell_utils.locales.init(require_locales$1.default, require_locales.getLocale());
const PosTabs = (0, react.forwardRef)(({ Table, PosForm, style, onChange }, ref) => {
	const [active, setActive] = (0, react.useState)("device");
	/**
	* @title: 切换tab
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:18
	*/
	const onClick = (event) => {
		const target = event.currentTarget;
		if (!target) return;
		const { key } = target.dataset;
		if (key) {
			setActive(key);
			onChange(key);
		}
	};
	(0, react.useImperativeHandle)(ref, () => ({ setActive }));
	const items = [{
		key: "device",
		label: (active) => /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__pos_tabs-tab-label" }, /* @__PURE__ */ react.default.createElement("div", { className: `pisell-lowcode__pos_circle ${active ? "active" : ""}` }, /* @__PURE__ */ react.default.createElement(require_apiKey.default, { color: active ? "#fff" : "#98A2B3" })), /* @__PURE__ */ react.default.createElement("span", { className: `pisell-lowcode__pos_tabs-tab-label-tlt ${active ? "active" : ""}` }, _pisell_utils.locales.getText("page.eftpos.text.device"))),
		children: Table
	}, {
		key: "setting",
		label: (active) => /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__pos_tabs-tab-label" }, /* @__PURE__ */ react.default.createElement("div", { className: `pisell-lowcode__pos_circle ${active ? "active" : ""}` }, /* @__PURE__ */ react.default.createElement(require_device$1.default, { color: active ? "#fff" : "#98A2B3" })), /* @__PURE__ */ react.default.createElement("span", { className: `pisell-lowcode__pos_tabs-tab-label-tlt ${active ? "active" : ""}` }, _pisell_utils.locales.getText("page.eftpos.text.setting"))),
		children: PosForm
	}];
	return /* @__PURE__ */ react.default.createElement("div", {
		className: "pisell-lowcode__pos_tabs",
		style
	}, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__pos_tabs-nav" }, items.map((item) => {
		return /* @__PURE__ */ react.default.createElement("div", {
			key: item.key,
			style: { display: "contents" },
			"data-key": item.key,
			onClick
		}, item.label(active === item.key));
	})), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__pos_tab-content" }, items.map((item) => {
		return /* @__PURE__ */ react.default.createElement("div", {
			className: `pisell-lowcode__pos_tabs-tabpane ${active === item.key ? "active" : ""}`,
			key: item.key
		}, item.children);
	})));
});
var eftpos_default = (props) => {
	const { name, zIndex, type = require_const.Eftpos.Payo, tableSize, getList, getField, getComm, onDel, onEdit, getPosComm, savePos, config, tyroUrl, tyroVersion, useTyroLog } = props;
	const utils = require_useEngineContext.default().appHelper.utils || {};
	require_index$1.request.setRequest(utils === null || utils === void 0 ? void 0 : utils.request);
	const [page, setPage] = (0, react.useState)("list");
	const [messageApi, contextHolder] = antd.message.useMessage({ prefixCls: "pisell-lowcode__pos_message" });
	const [loading, setLoading] = (0, react.useState)(false);
	const [status, setStatus] = (0, react.useState)(require_const.DeviceEnum.Add);
	const tableRef = (0, react.useRef)();
	const deviceIdRef = (0, react.useRef)("");
	const [deviceItemList, setDeviceCommList] = (0, react.useState)([]);
	const [posItemList, setPosCommList] = (0, react.useState)([]);
	const [deviceItem, setDeviceItem] = (0, react.useState)();
	const [pairStatus, setPairStatus] = (0, react.useState)("");
	const [pairMessage, setPairMessage] = (0, react.useState)("");
	const [systemConfigStatus, setSystemConfigStatus] = (0, react.useState)(true);
	const posTabRef = (0, react.useRef)(null);
	const [posLoading, setPosLoading] = (0, react.useState)(false);
	const pairModalHook = require_hooks$1.usePairModal();
	const pairRequest = require_hooks.usePairRequest();
	const checkPairRequest = require_hooks.useCheckPairRequest();
	/**
	* @title: 删除设备
	* @param {string} id 设备id
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:20
	*/
	const onDelDevice = (id) => {
		return onDel(id);
	};
	/**
	* @title: 编辑设备
	* @param {any} device 设备信息
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:20
	*/
	const onEditDevice = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (device) {
			if (device) {
				deviceIdRef.current = device.id;
				const list = yield require_const.getCommByFields({
					field: getField(type, require_const.DeviceEnum.Edit),
					value: device,
					getComm
				});
				setStatus(require_const.DeviceEnum.Edit);
				setDeviceCommList(list);
				setPage("edit");
				return;
			}
			setStatus(require_const.DeviceEnum.Add);
			const list = yield require_const.getCommByFields({
				field: getField(type),
				getComm
			});
			console.log(list);
			setDeviceCommList(list);
			setPage("edit");
		});
		return function onEditDevice(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	/**
	* @title: 重连设备
	* @description: 编辑第三方设备信息
	* @param {any} device
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:21
	*/
	const onRepair = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (device) {
			console.log("onRepair device = ", device);
			if (type === require_const.Eftpos.MX51) try {
				setLoading(true);
				const checkPairResponse = yield checkPairRequest.run({
					type,
					pair_response: device === null || device === void 0 ? void 0 : device.response
				});
				console.log("checkPairResponse = ", checkPairResponse);
				if ((checkPairResponse === null || checkPairResponse === void 0 ? void 0 : checkPairResponse.code) == 200) {
					messageApi.open({
						type: "warning",
						style: { zIndex: (zIndex || 9999) + 1 },
						content: _pisell_utils.locales.getText("page.eftpos.text.repair.message.warning")
					});
					return;
				}
			} catch (error) {
				console.error("onRepair error =>", error);
			} finally {
				setLoading(false);
			}
			const list = yield require_const.getCommByFields({
				field: getField(type, require_const.DeviceEnum.Repair),
				value: device,
				getComm
			});
			deviceIdRef.current = device.id;
			setDeviceCommList(list);
			setStatus(require_const.DeviceEnum.Repair);
			setPage("edit");
		});
		return function onRepair(_x2) {
			return _ref2.apply(this, arguments);
		};
	}();
	const pair = require_hooks.usePairTerminalo(type, {
		url: tyroUrl,
		version: tyroVersion
	});
	const onLog = (device) => {
		console.log(device);
		setDeviceItem(device);
		setPage("log");
	};
	/**
	* @title: 保存设备信息
	* @description: tyro 在新增与重连时候需在前端进行重连
	* @param {any} device 设备信息
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:21
	*/
	const onSave = function() {
		var _ref4 = require_asyncToGenerator._asyncToGenerator(function* (device, pair_response) {
			console.log("device = ", device);
			console.log("pair_response = ", pair_response);
			const update = function() {
				var _ref3 = require_asyncToGenerator._asyncToGenerator(function* () {
					try {
						yield onEdit(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, device), {}, {
							type,
							pair_response
						}), status || "add");
						if (type === require_const.Eftpos.MX51 && (status === require_const.DeviceEnum.Add || status === require_const.DeviceEnum.Repair)) {
							var _device$connection;
							messageApi.open({
								type: "success",
								style: { zIndex: (zIndex || 9999) + 1 },
								content: /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__pos_message-custom-content_mx51" }, /* @__PURE__ */ react.default.createElement("span", null, "Terminal paired successfully."), /* @__PURE__ */ react.default.createElement("span", null, /* @__PURE__ */ react.default.createElement("span", null, "Pairing Name: ", (device === null || device === void 0 || (_device$connection = device.connection) === null || _device$connection === void 0 ? void 0 : _device$connection.pairing_nickname) || "-"), /* @__PURE__ */ react.default.createElement("span", null, "Pairing ID: ", (pair_response === null || pair_response === void 0 ? void 0 : pair_response.pairing_id) || "-")))
							});
						}
					} catch (error) {
						console.error("onSave error =>", error);
						messageApi.open({
							type: "error",
							style: { zIndex: (zIndex || 9999) + 1 },
							content: (error === null || error === void 0 ? void 0 : error.message) || _pisell_utils.locales.getText("page.eftpos.text.pair.error")
						});
						return;
					} finally {
						setLoading(false);
					}
					yield tableRef.current.loadData();
					setPage("list");
				});
				return function update() {
					return _ref3.apply(this, arguments);
				};
			}();
			setLoading(true);
			if (type === "tyro" && status !== "edit") {
				pair(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, config), device.connection), (response) => {
					console.log("response =", response);
					setPairStatus(response.status);
					setPairMessage(response.message);
					if (response.status === "inProgress") {
						messageApi.open({
							type: "info",
							style: { zIndex: (zIndex || 9999) + 1 },
							content: response.message
						});
						return;
					}
					if (response.status === "success") {
						messageApi.open({
							type: "success",
							style: { zIndex: (zIndex || 9999) + 1 },
							content: response.message
						});
						device.connection.integrationKey = response.integrationKey;
						device.status = "paired";
						update();
						return;
					}
					messageApi.open({
						type: "warning",
						style: { zIndex: (zIndex || 9999) + 1 },
						content: response.message
					});
					device.status = "unpaired";
					update();
				});
				return;
			}
			update();
		});
		return function onSave(_x3, _x4) {
			return _ref4.apply(this, arguments);
		};
	}();
	const onClickByTip = () => {
		var _posTabRef$current;
		(_posTabRef$current = posTabRef.current) === null || _posTabRef$current === void 0 || _posTabRef$current.setActive("setting");
	};
	const getPos = (0, react.useCallback)(require_asyncToGenerator._asyncToGenerator(function* () {
		if (typeof getPosComm === "function") {
			setPosLoading(true);
			const list = (yield getPosComm(type).catch(() => {
				setPosLoading(false);
			})).map((item) => {
				return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { __sourceName: item.name });
			});
			setPosLoading(false);
			const obj = require_const.getDefaultValue(list);
			setSystemConfigStatus(Object.values(obj).every(Boolean));
			setPosCommList(list);
		}
	}), [getPosComm, type]);
	/**
	* @title: 保存厂商设置
	* @param {any} pos 厂商信息
	* @return {*}
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:22
	*/
	const onSavePos = (pos) => {
		if (typeof savePos === "function") {
			setPosLoading(true);
			savePos(type, pos).then(() => {
				getPos();
			}).catch(() => {
				setPosLoading(false);
			});
		}
	};
	(0, react.useEffect)(() => {
		setPairStatus("");
		setPairMessage("");
	}, [page]);
	/**
	* @title: 回退列表页
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:23
	*/
	const onBack = () => {
		setPage("list");
	};
	const onChangeTabs = (key) => {
		setPage("list");
	};
	/**
	* @title: 设置厂商配置字段，type 变化时重新拉取
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:23
	*/
	(0, react.useEffect)(() => {
		if (type) getPos();
	}, [type]);
	/**
	* @title: 响应外界type变化将page重置为列表页
	* @Author: hongbing.wang
	* @Date: 2024-08-20 11:23
	*/
	(0, react.useEffect)(() => {
		setPage("list");
	}, [type]);
	/**
	* @title 保存配对信息
	* @description MX51设备配对的第一步不是直接保存，而是要先发起配对请求，等刷卡机上配对后，再保存
	*/
	const onSavePair = function() {
		var _ref5 = require_asyncToGenerator._asyncToGenerator(function* (device) {
			if ([require_const.Eftpos.MX51].includes(type) && (status === require_const.DeviceEnum.Add || status === require_const.DeviceEnum.Repair)) try {
				setLoading(true);
				const response = yield pairRequest.run(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (device === null || device === void 0 ? void 0 : device.connection) || {}), {}, { type }));
				if ((response === null || response === void 0 ? void 0 : response.code) == 200) pairModalHook.open({
					data: {
						type,
						device,
						pairResponse: response === null || response === void 0 ? void 0 : response.data
					},
					onCancel: () => {
						pairModalHook.close();
					},
					onConfirm: () => {
						pairModalHook.close();
						onSave(device, response === null || response === void 0 ? void 0 : response.data);
					}
				});
			} catch (error) {
				console.error("onSavePair error =>", error);
				messageApi.open({
					type: "error",
					style: { zIndex: (zIndex || 9999) + 1 },
					content: (error === null || error === void 0 ? void 0 : error.message) || _pisell_utils.locales.getText("page.eftpos.text.pair.error")
				});
			} finally {
				setLoading(false);
			}
			else onSave(device);
		});
		return function onSavePair(_x5) {
			return _ref5.apply(this, arguments);
		};
	}();
	if (require_const.TYPES_WITH_MERCHANT_SETTINGS.has(type)) return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(PosTabs, {
		ref: posTabRef,
		Table: /* @__PURE__ */ react.default.createElement(require_table.default, {
			ref: tableRef,
			eftposName: name,
			type,
			useTyroLog,
			tableSize,
			getList,
			onDel: onDelDevice,
			onRepair,
			onEdit: onEditDevice,
			onLog,
			key: `postable${type}`,
			systemConfigStatus,
			onClickByTip
		}),
		PosForm: /* @__PURE__ */ react.default.createElement(antd.Spin, {
			tip: "Loading...",
			spinning: posLoading,
			wrapperClassName: "pisell-lowcode__pos_setting_box"
		}, /* @__PURE__ */ react.default.createElement(require_pos.default, {
			eftposName: name,
			onSave: onSavePos,
			formItemList: posItemList
		})),
		onChange: onChangeTabs
	}), /* @__PURE__ */ react.default.createElement(require_index.default, {
		open: page === "edit",
		onClose: onBack,
		title: _pisell_utils.locales.getText("page.eftpos.text.device.add")(status, name),
		titleNoWrap: true,
		zIndex: zIndex || 9999,
		destroyOnClose: true,
		className: "pisell-lowcode__pos_drawer"
	}, /* @__PURE__ */ react.default.createElement(antd.Spin, {
		tip: "Loading...",
		spinning: loading,
		wrapperClassName: "pisell-lowcode__pos_box"
	}, /* @__PURE__ */ react.default.createElement(require_device.default, {
		formItemList: deviceItemList,
		onSave,
		key: `posdevice${type}`
	}, pairStatus ? /* @__PURE__ */ react.default.createElement(antd.Form.Item, { label: "Status" }, /* @__PURE__ */ react.default.createElement(antd.Input, {
		value: pairStatus,
		disabled: true
	})) : null, pairMessage ? /* @__PURE__ */ react.default.createElement(antd.Form.Item, { label: "Message" }, /* @__PURE__ */ react.default.createElement(antd.Input, {
		value: pairMessage,
		disabled: true
	})) : null))), /* @__PURE__ */ react.default.createElement(require_index.default, {
		open: page === "log",
		onClose: onBack,
		title: _pisell_utils.locales.getText("page.eftpos.text.log"),
		titleNoWrap: true,
		zIndex: zIndex || 9999,
		destroyOnClose: true,
		className: "pisell-lowcode__pos_drawer pisell-lowcode__pos_drawer_log"
	}, /* @__PURE__ */ react.default.createElement(require_log.default, {
		type,
		tyroUrl,
		tyroVersion,
		device: deviceItem
	})), contextHolder);
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(require_table.default, {
		eftposName: name,
		ref: tableRef,
		type,
		useTyroLog,
		tableSize,
		getList,
		onDel: onDelDevice,
		onRepair,
		onLog,
		onEdit: onEditDevice,
		key: `table${type}`,
		systemConfigStatus,
		onClickByTip
	}), /* @__PURE__ */ react.default.createElement(require_index.default, {
		open: page === "edit",
		onClose: onBack,
		title: type === require_const.Eftpos.MX51 ? /* @__PURE__ */ react.default.createElement("span", { style: {
			display: "inline-flex",
			alignItems: "center",
			gap: "4px"
		} }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Image, {
			src: src_components_eftpos_assets_mx51SCI_svg.default,
			preview: false,
			width: 24
		}), /* @__PURE__ */ react.default.createElement("span", null, "Simple Cloud Integration")) : _pisell_utils.locales.getText("page.eftpos.text.device.add")(status, name),
		titleNoWrap: true,
		zIndex: zIndex || 9999,
		destroyOnClose: true,
		className: "pisell-lowcode__pos_drawer"
	}, /* @__PURE__ */ react.default.createElement(antd.Spin, {
		tip: "Loading...",
		spinning: loading,
		wrapperClassName: "pisell-lowcode__pos_box"
	}, /* @__PURE__ */ react.default.createElement(require_device.default, {
		formItemList: deviceItemList,
		onSave: onSavePair,
		key: `device${type}`,
		type,
		status
	}, pairStatus ? /* @__PURE__ */ react.default.createElement(antd.Form.Item, { label: "Status" }, /* @__PURE__ */ react.default.createElement(antd.Input, {
		value: pairStatus,
		disabled: true
	})) : null, pairMessage ? /* @__PURE__ */ react.default.createElement(antd.Form.Item, { label: "Message" }, /* @__PURE__ */ react.default.createElement(antd.Input, {
		value: pairMessage,
		disabled: true
	})) : null))), /* @__PURE__ */ react.default.createElement(require_index.default, {
		open: page === "log",
		onClose: onBack,
		title: _pisell_utils.locales.getText("page.eftpos.text.log"),
		titleNoWrap: true,
		zIndex: zIndex || 9999,
		destroyOnClose: true,
		className: "pisell-lowcode__pos_drawer pisell-lowcode__pos_drawer_log"
	}, /* @__PURE__ */ react.default.createElement(require_log.default, {
		type,
		tyroUrl,
		tyroVersion,
		device: deviceItem
	})), contextHolder, pairModalHook.visible && /* @__PURE__ */ react.default.createElement(require_index$2.default, pairModalHook.modalProps));
};
//#endregion
exports.default = eftpos_default;
