const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useEngineContext = require("../../hooks/useEngineContext.js");
const require_serve = require("./serve.js");
const require_index = require("./index.js");
let react = require("react");
react = require_runtime.__toESM(react);
//#region src/components/eftposPay/EFTPos.tsx
const isDev = location.host.indexOf("-dev.cn");
const EFTPos = (props, ref) => {
	var _context$appHelper;
	const [visible, setVisible] = (0, react.useState)(false);
	const [footer, setFooter] = (0, react.useState)(null);
	const [type, setType] = (0, react.useState)("payo");
	const [terminal, setTerminal] = (0, react.useState)({ network: true });
	const [apiParams, setApiParams] = (0, react.useState)({});
	const [source, setSource] = (0, react.useState)("normal");
	const [action, setAction] = (0, react.useState)("");
	const [mode, setMode] = (0, react.useState)("auto");
	const [key, setKey] = (0, react.useState)(0);
	const [tyroUrl] = (0, react.useState)(isDev === -1 ? "https://iclient.tyro.com" : "https://iclientsimulator.test.tyro.com");
	const [tyroVersion] = (0, react.useState)("iclient-v1.js");
	const isComplete = (0, react.useRef)(false);
	const { lowcodeRef, store, storage, interaction, isTerminal, sendWarningLog } = ((_context$appHelper = require_useEngineContext.default().appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	(0, react.useMemo)(() => {
		return isTerminal();
	}, [isTerminal]);
	const install = () => {
		if (sendWarningLog) sendWarningLog({
			title: "页面eftpos进入",
			content: [{
				key: "time",
				value: Date.now()
			}]
		});
		isComplete.current = false;
		if (lowcodeRef) lowcodeRef.onReady({});
		if (interaction) {
			var _interaction$utils, _interaction$utils$po, _interaction$utils2, _interaction$utils3, _interaction$utils4;
			interaction === null || interaction === void 0 || (_interaction$utils = interaction.utils) === null || _interaction$utils === void 0 || (_interaction$utils$po = _interaction$utils.postMessageToApp) === null || _interaction$utils$po === void 0 || _interaction$utils$po.call(_interaction$utils, {
				module: "pos",
				key: "page_load"
			});
			interaction === null || interaction === void 0 || (_interaction$utils2 = interaction.utils) === null || _interaction$utils2 === void 0 || _interaction$utils2.mountFunction("pos", "onPay", (params) => {
				sendWarningLog && sendWarningLog({
					title: "进入eftpos",
					content: [{
						key: "eftposPay",
						value: params
					}, {
						key: "time",
						value: Date.now()
					}]
				});
				try {
					var _storage$get, _store$getState;
					console.log("onPay params =", params, storage === null || storage === void 0 || (_storage$get = storage.get) === null || _storage$get === void 0 ? void 0 : _storage$get.call(storage, "channel"));
					const json = JSON.parse(params);
					json.card_reader_type = json.code.split("_")[1].toLocaleLowerCase();
					json.type = json.card_reader_type;
					json.symbol = (store === null || store === void 0 || (_store$getState = store.getState) === null || _store$getState === void 0 || (_store$getState = _store$getState.call(store)) === null || _store$getState === void 0 || (_store$getState = _store$getState.global) === null || _store$getState === void 0 || (_store$getState = _store$getState.globalConfig) === null || _store$getState === void 0 ? void 0 : _store$getState.currency_symbol) || "$";
					if (!json.client) json.client = "merchant";
					if (!json.name) json.name = json.code;
					update({
						type: json.card_reader_type,
						terminal: { network: true },
						apiParams: json,
						mode: json.mode || "pay",
						key: Date.now()
					});
					console.log("json=====>", json);
				} catch (error) {
					console.log(typeof params);
					console.log("error onPay", params);
					console.error(error);
				}
			});
			interaction === null || interaction === void 0 || (_interaction$utils3 = interaction.utils) === null || _interaction$utils3 === void 0 || _interaction$utils3.mountFunction("pos", "onPayBackup", (params) => {
				try {
					const json = JSON.parse(decodeURIComponent(atob(decodeURIComponent(params))));
					console.log("json = ", json);
					update({
						apiParams: json,
						terminal: { network: true },
						key: Date.now()
					}, "restore");
				} catch (error) {
					console.log(typeof params);
					console.log("error onPayBackup", JSON.stringify(params));
					console.error(error);
				}
			});
			interaction === null || interaction === void 0 || (_interaction$utils4 = interaction.utils) === null || _interaction$utils4 === void 0 || _interaction$utils4.mountFunction("pos", "onPayNetWork", (params) => {
				try {
					updateTerminal(JSON.parse(params));
				} catch (error) {
					console.error("onPayNetWork ", params);
					console.error(error);
				}
			});
		}
	};
	const uninstall = () => {
		if (sendWarningLog) sendWarningLog({
			title: "页面eftpos离开",
			content: [{
				key: "time",
				value: Date.now()
			}]
		});
	};
	(0, react.useEffect)(() => {
		install();
		return uninstall;
	}, []);
	const onChangeStatus = (status, params, other) => {
		console.log("---------------------------");
		console.log("status, params = ", status, params, params === null || params === void 0 ? void 0 : params.uniquePaymentNumber, other);
		console.log("---------------------------");
		if (status === "success" || status === "fail") isComplete.current = true;
		if (props === null || props === void 0 ? void 0 : props.onPayResult) {
			props.onPayResult({
				status,
				params
			});
			if (status === "success" || status === "fail") {
				setVisible(false);
				isComplete.current = false;
			}
			return;
		}
		if (lowcodeRef) {
			lowcodeRef.onChange({
				status,
				params
			});
			if (status === "success" || status === "fail") {
				setVisible(false);
				isComplete.current = false;
			}
			return;
		}
		if (interaction) {
			var _interaction$utils13, _interaction$utils13$;
			if (status === "page") {
				console.log(" 页面 = ", params);
				if (params === "amount" || params === "deviceList" || params === "fail") {
					var _interaction$utils5, _interaction$utils5$p;
					interaction === null || interaction === void 0 || (_interaction$utils5 = interaction.utils) === null || _interaction$utils5 === void 0 || (_interaction$utils5$p = _interaction$utils5.postMessageToApp) === null || _interaction$utils5$p === void 0 || _interaction$utils5$p.call(_interaction$utils5, {
						module: "pos",
						key: "other",
						data: { key: "show_close_button" }
					});
				}
				if (params === "pay") {
					var _interaction$utils6, _interaction$utils6$p;
					interaction === null || interaction === void 0 || (_interaction$utils6 = interaction.utils) === null || _interaction$utils6 === void 0 || (_interaction$utils6$p = _interaction$utils6.postMessageToApp) === null || _interaction$utils6$p === void 0 || _interaction$utils6$p.call(_interaction$utils6, {
						module: "pos",
						key: "other",
						data: { key: "hide_close_button" }
					});
				}
				return;
			}
			if (status === "print") {
				if (other === "print_on_timeout") {
					var _interaction$utils7, _interaction$utils7$p;
					interaction === null || interaction === void 0 || (_interaction$utils7 = interaction.utils) === null || _interaction$utils7 === void 0 || (_interaction$utils7$p = _interaction$utils7.postMessageToApp) === null || _interaction$utils7$p === void 0 || _interaction$utils7$p.call(_interaction$utils7, {
						module: "pos",
						key: "print_on_timeout"
					});
				} else if (other === "back_home") {
					var _interaction$utils8, _interaction$utils8$p;
					interaction === null || interaction === void 0 || (_interaction$utils8 = interaction.utils) === null || _interaction$utils8 === void 0 || (_interaction$utils8$p = _interaction$utils8.postMessageToApp) === null || _interaction$utils8$p === void 0 || _interaction$utils8$p.call(_interaction$utils8, {
						module: "pos",
						key: "back_home"
					});
				} else {
					const list = Array.isArray(params) ? params.filter((item) => typeof item === "string").filter(Boolean) : [];
					if (list.length > 0) {
						var _interaction$utils9, _interaction$utils9$p;
						interaction === null || interaction === void 0 || (_interaction$utils9 = interaction.utils) === null || _interaction$utils9 === void 0 || (_interaction$utils9$p = _interaction$utils9.postMessageToApp) === null || _interaction$utils9$p === void 0 || _interaction$utils9$p.call(_interaction$utils9, {
							module: "pos",
							key: "print",
							data: list
						});
					}
				}
				return;
			}
			if (status === "mark_tx_processed") {
				var _interaction$utils10, _interaction$utils10$;
				interaction === null || interaction === void 0 || (_interaction$utils10 = interaction.utils) === null || _interaction$utils10 === void 0 || (_interaction$utils10$ = _interaction$utils10.postMessageToApp) === null || _interaction$utils10$ === void 0 || _interaction$utils10$.call(_interaction$utils10, {
					module: "pos",
					key: "mark_tx_processed",
					data: { params: { uniquePaymentNumberList: params } }
				});
				return;
			}
			if (typeof params === "object" && params && "uniquePaymentNumber" in params && params.uniquePaymentNumber) {
				var _interaction$utils11, _interaction$utils11$;
				interaction === null || interaction === void 0 || (_interaction$utils11 = interaction.utils) === null || _interaction$utils11 === void 0 || (_interaction$utils11$ = _interaction$utils11.postMessageToApp) === null || _interaction$utils11$ === void 0 || _interaction$utils11$.call(_interaction$utils11, {
					module: "pos",
					key: "mark_tx_processed",
					data: { params: { uniquePaymentNumberList: [params.uniquePaymentNumber] } }
				});
			}
			if (status === "success" && typeof params === "object" && params && "receipt" in params && Array.isArray(params.receipt) && params.receipt.length > 0) {
				const receiptList = params.receipt.filter((item) => typeof item === "string").filter(Boolean);
				if (receiptList.length > 0) {
					var _interaction$utils12, _interaction$utils12$;
					interaction === null || interaction === void 0 || (_interaction$utils12 = interaction.utils) === null || _interaction$utils12 === void 0 || (_interaction$utils12$ = _interaction$utils12.postMessageToApp) === null || _interaction$utils12$ === void 0 || _interaction$utils12$.call(_interaction$utils12, {
						module: "pos",
						key: "print",
						data: receiptList
					});
				}
			}
			sendWarningLog({
				title: "离开eftpos",
				content: [{
					key: "eftposPay",
					value: JSON.stringify(params)
				}, {
					key: "time",
					value: Date.now()
				}]
			});
			interaction === null || interaction === void 0 || (_interaction$utils13 = interaction.utils) === null || _interaction$utils13 === void 0 || (_interaction$utils13$ = _interaction$utils13.postMessageToApp) === null || _interaction$utils13$ === void 0 || _interaction$utils13$.call(_interaction$utils13, {
				module: "pos",
				key: "payChange",
				data: {
					status: status === "success" ? 1 : 0,
					params
				}
			});
			setVisible(false);
			isComplete.current = false;
		}
	};
	const update = (params, source) => {
		setVisible(true);
		setSource(source || "");
		if (params.type) setType(params.type);
		if (params.terminal !== void 0) setTerminal(params.terminal);
		if (params.apiParams !== void 0) setApiParams(params.apiParams);
		if (params.mode !== void 0) setMode(params.mode);
		if (params.key !== void 0) setKey(params.key);
		if (params.source !== void 0) setSource(params.source);
	};
	const updateNetwork = (terminal) => {
		setTerminal(terminal);
	};
	const updateTerminal = (terminal) => {
		setTerminal(terminal);
	};
	const getApi = () => {
		return {
			pay(params, config) {
				return require_serve.payData(params, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, config || {}), {}, { customResponse: true })).then((res) => {
					console.log("====> 支付结果", res);
					if (sendWarningLog) sendWarningLog({
						title: "支付结果",
						content: [{
							key: "res",
							value: JSON.stringify(res)
						}, {
							key: "params",
							value: JSON.stringify(params)
						}]
					});
					if (res && (res.code === "200" || res.code === 200)) return res;
					return Promise.reject(res);
				}).catch((error) => {
					console.log("====> 支付失败", error);
					if (sendWarningLog) sendWarningLog({
						title: "api.pay支付失败",
						content: [{
							key: "params",
							value: JSON.stringify(params)
						}, {
							key: "error",
							value: JSON.stringify(error)
						}]
					});
					return error;
				});
			},
			check(params, config) {
				return require_serve.verifyData(params, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, config || {}), {}, { customResponse: true })).then((res) => {
					console.log("--------- verifyData  ----", res);
					if (res && (res.code === "200" || res.code === 200)) return res;
					return Promise.reject(res);
				});
			},
			getDeviceList(params, config) {
				if (sendWarningLog) sendWarningLog({
					title: "getDeviceList",
					content: [{
						key: "params",
						value: JSON.stringify(params)
					}]
				});
				return require_serve.getDeviceList(params, config).then((res) => {
					var _props$onEftposLoad2;
					console.log("getDeviceList res", res);
					if (res && (res.code === "200" || res.code === 200)) {
						var _props$onEftposLoad;
						props === null || props === void 0 || (_props$onEftposLoad = props.onEftposLoad) === null || _props$onEftposLoad === void 0 || _props$onEftposLoad.call(props, true);
						return res.data;
					}
					props === null || props === void 0 || (_props$onEftposLoad2 = props.onEftposLoad) === null || _props$onEftposLoad2 === void 0 || _props$onEftposLoad2.call(props, false);
					return Promise.reject(res);
				}).catch((err) => {
					var _props$onEftposLoad3;
					console.log("getDeviceList_error>>>>>>", err);
					props === null || props === void 0 || (_props$onEftposLoad3 = props.onEftposLoad) === null || _props$onEftposLoad3 === void 0 || _props$onEftposLoad3.call(props, false);
					return Promise.reject(err);
				});
			},
			update(params, config) {
				return require_serve.payButton(params, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, config || {}), {}, { customResponse: true })).then((res) => {
					console.log("--------- payButton  ----", res);
					if (res && (res.code === "200" || res.code === 200)) return res;
					return Promise.reject(res);
				});
			},
			refund(params, config) {
				return require_serve.refundData(params, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, config || {}), {}, { customResponse: true })).then((res) => {
					console.log("--------- refundData  ----", res);
					if (res && (res.code === "200" || res.code === 200)) return res;
					return Promise.reject(res);
				});
			},
			getTransaction(params, config) {
				console.log("params", params, config);
				return require_serve.getTransaction(params, config).then((res) => {
					var _props$onEftposLoad5;
					if (res && (res.code === "200" || res.code === 200)) {
						var _props$onEftposLoad4;
						props === null || props === void 0 || (_props$onEftposLoad4 = props.onEftposLoad) === null || _props$onEftposLoad4 === void 0 || _props$onEftposLoad4.call(props, true);
						return res;
					}
					props === null || props === void 0 || (_props$onEftposLoad5 = props.onEftposLoad) === null || _props$onEftposLoad5 === void 0 || _props$onEftposLoad5.call(props, false);
					return Promise.reject(res);
				}).catch((err) => {
					var _props$onEftposLoad6;
					console.log("getTransaction_error>>>>>>", err);
					props === null || props === void 0 || (_props$onEftposLoad6 = props.onEftposLoad) === null || _props$onEftposLoad6 === void 0 || _props$onEftposLoad6.call(props, false);
					return Promise.reject(err);
				});
			},
			addTransaction(params, config) {
				console.log("params", params, config);
				return require_serve.addTransaction(params, config).then((res) => {
					if (res && (res.code === "200" || res.code === 200)) return res;
					return Promise.reject(res);
				});
			},
			editTransaction(id, params, config) {
				console.log("editTransaction id = ", id);
				return new Promise((resolve) => {
					resolve(id);
				}).then(() => require_serve.editTransaction(id, params, config)).then((res) => {
					if (res && (res.code === "200" || res.code === 200)) return res;
					return Promise.reject(res);
				}, (err) => {
					console.log(err);
					return Promise.reject(err);
				});
			},
			linklyPayQuery(params, config) {
				console.log("linklyPayQuery>>>>>>>params", params, config);
				return require_serve.linklyPayQuery(params, config).then((res) => {
					console.log("linklyPayQuery>>>>>>then", res);
					if (res && (res.code === "200" || res.code === 200)) return res;
					return Promise.reject(res);
				}).catch((err) => {
					console.log("linklyPayQuery>>>>>>error", err);
					return Promise.reject(err);
				});
			},
			linklyRefundQuery(params, config) {
				return require_serve.linklyRefundQuery(params, config).then((res) => {
					if (res && (res.code === "200" || res.code === 200)) return res;
					return Promise.reject(res);
				}).catch((err) => {
					return Promise.reject(err);
				});
			}
		};
	};
	const onChangeParams = (params) => {
		if (isComplete.current) return;
		if (lowcodeRef) {
			lowcodeRef.onChange({
				status: "cache",
				params
			});
			return;
		}
		if (interaction) {
			var _interaction$utils14, _interaction$utils14$;
			const json = JSON.stringify(params);
			const base64JSon = encodeURIComponent(btoa(encodeURIComponent(json)));
			interaction === null || interaction === void 0 || (_interaction$utils14 = interaction.utils) === null || _interaction$utils14 === void 0 || (_interaction$utils14$ = _interaction$utils14.postMessageToApp) === null || _interaction$utils14$ === void 0 || _interaction$utils14$.call(_interaction$utils14, {
				module: "pos",
				key: "paymentInfo",
				data: base64JSon
			});
		}
	};
	(0, react.useImperativeHandle)(ref, () => ({
		updateNetwork,
		update
	}));
	return visible ? /* @__PURE__ */ react.default.createElement(require_index.default, {
		key,
		type,
		tyroUrl,
		tyroVersion,
		source,
		mode,
		terminal,
		onChangeStatus,
		onChangeParams,
		getApi,
		params: apiParams,
		styles: props === null || props === void 0 ? void 0 : props.styles,
		isWebPos: props === null || props === void 0 ? void 0 : props.isWebPos
	}) : null;
};
var EFTPos_default = (0, react.forwardRef)(EFTPos);
//#endregion
exports.default = EFTPos_default;
