const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_const = require("../const.js");
const require_helper = require("../helper.js");
const require_index = require("../store/index.js");
const require_hooks = require("../hooks.js");
const require_payLog = require("../utils/payLog.js");
const require_step = require("../component/step/step.js");
const require_hooks$1 = require("./hooks.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let decimal_js = require("decimal.js");
//#region src/components/eftposPay/tyro/index.tsx
const Minute = 1e3 * 60;
var tyro_default = ({ api, onChange, children, url, version }) => {
	const { eftpos, custom, mode, status: payStatus, client } = require_index.useContextData();
	const dispatch = require_index.useDispatch();
	const [status, setStatus] = (0, react.useState)(-1);
	const [isManually, setManually] = (0, react.useState)(false);
	const isCancelRef = (0, react.useRef)(false);
	const { deviceRef, payRef, numRef, modeRef, orderIdRef, amountRef } = require_hooks.useStoreRef();
	const merchantReceiptRef = (0, react.useRef)({});
	const [TyroRef, load] = require_hooks$1.useTyro({
		url,
		version
	});
	const requestParams = (0, react.useRef)(custom === null || custom === void 0 ? void 0 : custom.requestParams);
	const listRef = (0, react.useRef)((custom === null || custom === void 0 ? void 0 : custom.list) || []);
	const iClientRef = (0, react.useRef)();
	const requestIdRef = (0, react.useRef)((custom === null || custom === void 0 ? void 0 : custom.requestId) || "");
	const unloadRef = (0, react.useRef)(false);
	const clearRef = (0, react.useRef)();
	const isPrint = (key) => {
		var _deviceRef$current, _deviceRef$current2;
		if ((_deviceRef$current = deviceRef.current) === null || _deviceRef$current === void 0 || (_deviceRef$current = _deviceRef$current.setting) === null || _deviceRef$current === void 0 ? void 0 : _deviceRef$current.receipts) return false;
		const receiptList = (_deviceRef$current2 = deviceRef.current) === null || _deviceRef$current2 === void 0 || (_deviceRef$current2 = _deviceRef$current2.setting) === null || _deviceRef$current2 === void 0 ? void 0 : _deviceRef$current2.receiptList;
		if (!Array.isArray(receiptList)) return true;
		if (Array.isArray(receiptList) && receiptList.some((item) => item === key)) return true;
		return false;
	};
	(0, react.useEffect)(() => {
		listRef.current = (custom === null || custom === void 0 ? void 0 : custom.list) || [];
	}, [custom === null || custom === void 0 ? void 0 : custom.list]);
	(0, react.useEffect)(() => {
		requestIdRef.current = (custom === null || custom === void 0 ? void 0 : custom.requestId) || "";
	}, [custom === null || custom === void 0 ? void 0 : custom.requestId]);
	(0, react.useEffect)(() => {
		requestParams.current = custom === null || custom === void 0 ? void 0 : custom.requestParams;
	}, [custom === null || custom === void 0 ? void 0 : custom.requestParams]);
	/**
	* @title: 获取设备信息
	* @description: 获取设备信息
	* @param {function} cb
	* @Author: hongbing.wang
	* @Date: 2024-08-19 11:32
	*/
	const getTerminalInfo = (cb) => {
		const iClient = iClientRef.current;
		if (!iClient) return;
		setStatus(0);
		const { connection } = deviceRef.current;
		const { integrationKey, tid, mid } = connection;
		console.log(integrationKey, tid, mid);
		iClient.terminalInfo(({ status, message, terminalInfo }) => {
			require_payLog.default.log("Tyro：终端信息回调", {
				status,
				terminalInfo,
				message
			});
			if (status === "success" && (terminalInfo === null || terminalInfo === void 0 ? void 0 : terminalInfo.available)) cb();
		}, {
			mid,
			tid,
			integrationKey
		});
	};
	/**
	* @title: 退款
	* @description: 退款
	* @Author: hongbing.wang
	* @Date: 2024-08-19 11:32
	*/
	const refundOrder = () => {
		var _deviceRef$current3, _deviceRef$current4, _requestParams$curren;
		const iClient = iClientRef.current;
		if (!iClient) return;
		if (isCancelRef.current) return;
		const { connection } = deviceRef.current;
		const { integrationKey, tid, mid } = connection;
		numRef.current = require_helper.getUuid();
		dispatch(require_index.updateState({ number: numRef.current }));
		requestParams.current = {
			amount: new decimal_js.Decimal(amountRef.current || 0).mul(100).toFixed(0, decimal_js.Decimal.ROUND_HALF_UP),
			integratedReceipt: !((_deviceRef$current3 = deviceRef.current) === null || _deviceRef$current3 === void 0 || (_deviceRef$current3 = _deviceRef$current3.setting) === null || _deviceRef$current3 === void 0 ? void 0 : _deviceRef$current3.receipts),
			integrationKey,
			transactionId: numRef.current,
			mid,
			tid,
			device: JSON.stringify(deviceRef.current),
			isPrintMerchant: isPrint("merchant"),
			isPrintCustomer: isPrint("customer"),
			browserVersion: require_helper.getBrowserVersion()
		};
		dispatch(require_index.updateCustom({ requestParams: requestParams.current }));
		setStatus(Date.now());
		merchantReceiptRef.current = {};
		api.addTransaction({
			order_id: orderIdRef.current,
			number: numRef.current,
			card_reader_id: (_deviceRef$current4 = deviceRef.current) === null || _deviceRef$current4 === void 0 ? void 0 : _deviceRef$current4.id,
			card_reader_type: eftpos,
			amount: requestParams.current.amount,
			action: modeRef.current === require_const.ModeEnum.FullPay ? require_const.ModeEnum.Pay : modeRef.current,
			status: "pending",
			request: requestParams.current,
			response: {},
			metadata: { receipt: [] },
			_config: { retry: {
				count: 10,
				delay: 500
			} }
		}).then((res) => {
			var _res$data, _res$data2;
			requestIdRef.current = res === null || res === void 0 || (_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.id;
			dispatch(require_index.updateCustom({ requestId: res === null || res === void 0 || (_res$data2 = res.data) === null || _res$data2 === void 0 ? void 0 : _res$data2.id }));
		});
		require_payLog.default.setContext({ transactionNumber: numRef.current });
		require_payLog.default.log("Tyro：发起退款", {
			number: numRef === null || numRef === void 0 ? void 0 : numRef.current,
			amount: (_requestParams$curren = requestParams.current) === null || _requestParams$curren === void 0 ? void 0 : _requestParams$curren.amount
		});
		iClient.initiateRefund(requestParams.current, {
			receiptCallback: receiptCallbackImpl,
			transactionCompleteCallback: transactionCompleteCallbackImpl,
			questionCallback: questionCallbackImpl,
			statusMessageCallback: statusMessageCallbackImpl
		});
	};
	/**
	* @title: 支付
	* @description:
	* @return {*}
	* @Author: hongbing.wang
	* @Date: 2024-08-19 11:33
	*/
	const payOrder = () => {
		var _deviceRef$current5, _payRef$current, _deviceRef$current6, _deviceRef$current7, _deviceRef$current8;
		const iClient = iClientRef.current;
		if (!iClient) return;
		if (isCancelRef.current) return;
		const { connection } = deviceRef.current;
		const { integrationKey, tid, mid } = connection;
		numRef.current = require_helper.getUuid();
		dispatch(require_index.updateState({ number: numRef.current }));
		console.log("deviceRef.current?.settings?.useOriginSurcharge = ", (_deviceRef$current5 = deviceRef.current) === null || _deviceRef$current5 === void 0 || (_deviceRef$current5 = _deviceRef$current5.setting) === null || _deviceRef$current5 === void 0 ? void 0 : _deviceRef$current5.useOriginSurcharge);
		requestParams.current = {
			amount: new decimal_js.Decimal((_payRef$current = payRef.current) === null || _payRef$current === void 0 ? void 0 : _payRef$current.total).mul(100).toFixed(0, decimal_js.Decimal.ROUND_HALF_UP),
			enableSurcharge: ((_deviceRef$current6 = deviceRef.current) === null || _deviceRef$current6 === void 0 || (_deviceRef$current6 = _deviceRef$current6.setting) === null || _deviceRef$current6 === void 0 ? void 0 : _deviceRef$current6.useOriginSurcharge) || false,
			integratedReceipt: !((_deviceRef$current7 = deviceRef.current) === null || _deviceRef$current7 === void 0 || (_deviceRef$current7 = _deviceRef$current7.setting) === null || _deviceRef$current7 === void 0 ? void 0 : _deviceRef$current7.receipts),
			integrationKey,
			transactionId: numRef.current,
			mid,
			tid,
			device: JSON.stringify(deviceRef.current),
			isPrintMerchant: isPrint("merchant"),
			isPrintCustomer: isPrint("customer"),
			browserVersion: require_helper.getBrowserVersion()
		};
		dispatch(require_index.updateCustom({ requestParams: requestParams.current }));
		setStatus(Date.now());
		merchantReceiptRef.current = {};
		api.addTransaction({
			order_id: orderIdRef.current,
			number: numRef.current,
			card_reader_id: (_deviceRef$current8 = deviceRef.current) === null || _deviceRef$current8 === void 0 ? void 0 : _deviceRef$current8.id,
			card_reader_type: eftpos,
			amount: requestParams.current.amount,
			action: modeRef.current === require_const.ModeEnum.FullPay ? require_const.ModeEnum.Pay : modeRef.current,
			status: "pending",
			request: requestParams.current,
			response: {},
			metadata: { receipt: [] },
			_config: { retry: {
				count: 10,
				delay: 500
			} }
		}).then((res) => {
			var _res$data3, _res$data4;
			requestIdRef.current = res === null || res === void 0 || (_res$data3 = res.data) === null || _res$data3 === void 0 ? void 0 : _res$data3.id;
			dispatch(require_index.updateCustom({ requestId: res === null || res === void 0 || (_res$data4 = res.data) === null || _res$data4 === void 0 ? void 0 : _res$data4.id }));
		});
		require_payLog.default.setContext({ transactionNumber: numRef.current });
		require_payLog.default.log("Tyro：发起支付", {
			number: numRef.current,
			amount: requestParams.current.amount
		});
		iClient.initiatePurchase(requestParams.current, {
			receiptCallback: receiptCallbackImpl,
			transactionCompleteCallback: transactionCompleteCallbackImpl,
			questionCallback: questionCallbackImpl,
			statusMessageCallback: statusMessageCallbackImpl
		});
	};
	/**
	* @title: 恢复上一次支付
	* @Author: hongbing.wang
	* @Date: 2024-08-19 11:33
	*/
	const continueLastOrder = () => {
		const iClient = iClientRef.current;
		if (!iClient) return;
		if (isCancelRef.current) return;
		setStatus(Date.now());
		merchantReceiptRef.current = {};
		require_payLog.default.log("Tyro：恢复上次交易");
		iClient.continueLastTransaction({
			receiptCallback: receiptCallbackImpl,
			transactionCompleteCallback: transactionCompleteCallbackImpl,
			questionCallback: questionCallbackImpl,
			statusMessageCallback: statusMessageCallbackImpl
		});
	};
	/**
	* @title: 获取商家小票回调信息
	* @description: 获取小票回调信息
	* @param {{signatureRequired:boolean,merchantReceipt:string}} signatureRequired: 是否打印小票, merchantReceipt:小票内容
	* @Author: hongbing.wang
	* @Date: 2024-08-19 11:33
	*/
	const receiptCallbackImpl = ({ signatureRequired, merchantReceipt }) => {
		require_payLog.default.log("Tyro：小票回调", {
			signatureRequired,
			hasReceipt: !!merchantReceipt
		});
		const receipt = [merchantReceipt].filter((item) => typeof item === "string").filter(Boolean);
		merchantReceiptRef.current = {
			signatureRequired,
			merchantReceipt
		};
		if (signatureRequired) {
			onChange("print", receipt);
			return;
		}
		if (isPrint("merchant")) onChange("print", receipt);
	};
	/**
	* @title: 支付状态回调函数
	* @description:
	* @param {string} message
	* @return {*}
	* @Author: hongbing.wang
	* @Date: 2024-08-19 11:34
	*/
	const statusMessageCallbackImpl = (message) => {
		var _listRef$current;
		require_payLog.default.log("Tyro：状态消息", { message });
		const translateMsg = _pisell_utils.locales.getText("page.eftpos-pay.text.tyro")(message);
		const msg = Array.isArray(translateMsg) ? translateMsg.reduce((accumulator, currentValue) => accumulator + " " + currentValue, "") : translateMsg;
		const lastitem = listRef.current[listRef.current.length - 1];
		const key = _pisell_utils.locales.getText("page.eftpos-pay.text.tyro")("Connection lost. Attempting to recover");
		if (msg === key && lastitem && lastitem.pedding === key) return;
		console.log("message = ", message);
		console.log("msg = ", msg);
		dispatch(require_index.updateCustom({ list: [...listRef.current || [], {
			status: "pendding",
			pedding: msg,
			resolve: msg,
			reject: msg,
			index: ((_listRef$current = listRef.current) === null || _listRef$current === void 0 ? void 0 : _listRef$current.length) || 0
		}] }));
	};
	/**
	* @title: 支付提示下操作信息
	* @Author: hongbing.wang
	* @Date: 2024-08-19 11:34
	*/
	const questionCallbackImpl = (question, answerCallback) => {
		require_payLog.default.log("Tyro：交互问题回调", { question });
		setStatus(-1);
		setManually(false);
		const msg = _pisell_utils.locales.getText("page.eftpos-pay.text.tyro")(question.text);
		dispatch(require_index.updateCustom({ btnList: question.options.map((item) => {
			return /* @__PURE__ */ react.default.createElement(antd.Button, {
				key: item,
				type: item === "YES" || item === "RETRY" ? "primary" : "default",
				className: "pisell-lowcode__eft-pay-step-item-btn",
				onClick: () => {
					dispatch(require_index.updateCustom({ btnList: [] }));
					setManually(false);
					dispatch(require_index.updateState({
						status: "loading",
						title: _pisell_utils.locales.getText("page.eftpos-pay.text.loading")(modeRef.current),
						subTitle: ""
					}));
					setStatus(Date.now());
					answerCallback(item);
				}
			}, _pisell_utils.locales.getText("page.eftpos-pay.text.tyro-btn")(item));
		}) }));
		const status = question.isError ? "fail" : "warn";
		dispatch(require_index.updateState(Array.isArray(msg) ? {
			status,
			title: msg[0],
			subTitle: msg[1]
		} : {
			status,
			title: msg
		}));
	};
	/**
	* @title: 支付完成-支付状态回调函数
	* @param {any} response
	* @Author: hongbing.wang
	* @Date: 2024-08-19 11:35
	*/
	const transactionCompleteCallbackImpl = (response) => {
		var _deviceRef$current9;
		setStatus(-1);
		setManually(false);
		require_payLog.default.log("Tyro：交易完成回调", { response });
		const receipt = [response === null || response === void 0 ? void 0 : response.customerReceipt].filter((item) => typeof item === "string").filter(Boolean);
		api.editTransaction(requestIdRef.current, {
			order_id: orderIdRef.current,
			number: numRef.current,
			card_reader_id: (_deviceRef$current9 = deviceRef.current) === null || _deviceRef$current9 === void 0 ? void 0 : _deviceRef$current9.id,
			card_reader_type: eftpos,
			amount: requestParams.current.amount,
			action: modeRef.current === require_const.ModeEnum.FullPay ? require_const.ModeEnum.Pay : modeRef.current,
			request: requestParams.current,
			status: response.result === "APPROVED" ? "succeed" : "failed",
			response: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, response), {}, { merchantReceipt: merchantReceiptRef.current }),
			metadata: { receipt }
		});
		dispatch(require_index.updateCustom({ btnList: [] }));
		if (response.result === "APPROVED") {
			dispatch(require_index.updateState({
				status: "success",
				title: _pisell_utils.locales.getText("page.eftpos-pay.text.successful")(mode),
				subTitle: ""
			}));
			dispatch(require_index.updateCustom({
				card_reader_surcharge: response.surchargeAmount || 0,
				receipt
			}));
			clearRef.current = setTimeout(() => {
				onChange("success", require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, require_helper.getPayParams({
					mode: modeRef.current,
					amount: amountRef.current,
					order_id: orderIdRef.current,
					number: numRef.current,
					device: deviceRef.current,
					pay: payRef.current,
					custom: {
						card_reader_surcharge: response.surchargeAmount || 0,
						receipt
					}
				}, "success")), {}, { receipt: isPrint("customer") ? receipt : [] }));
			}, 1e3);
			return;
		} else {
			var _deviceRef$current10;
			if ((((_deviceRef$current10 = deviceRef.current) === null || _deviceRef$current10 === void 0 ? void 0 : _deviceRef$current10.setting.receipts) ? [] : receipt).length > 0) {
				if (isPrint("customer")) onChange("print", receipt);
			}
		}
		if (response.result === "UNKNOWN") {
			dispatch(require_index.updateCustom({ errorStatus: "unknow" }));
			dispatch(require_index.updateComponent("UnknowFail"));
			return;
		}
		dispatch(require_index.updateState({ status: "fail" }));
		dispatch(require_index.updateCustom({ failCustom: {
			isPosNetworkError: false,
			status: "fail",
			fail: response.result,
			warn: null,
			title: _pisell_utils.locales.getText("page.eftpos-pay.text.failed")(modeRef.current)
		} }));
		dispatch(require_index.updateComponent("FailCustom"));
	};
	(0, react.useEffect)(() => {
		const { config } = deviceRef.current;
		const { apiKey, posProductData } = config;
		if (load && TyroRef.current) {
			const IClient = TyroRef.current.IClient;
			if (IClient) {
				iClientRef.current = new IClient(apiKey, posProductData);
				require_payLog.default.log("Tyro：SDK 初始化完成");
				if (isCancelRef.current) return;
				if (!numRef.current) {
					if (modeRef.current === require_const.ModeEnum.Pay || modeRef.current === require_const.ModeEnum.FullPay) {
						require_payLog.default.log("Tyro：支付交易，查询终端信息");
						getTerminalInfo(payOrder);
					} else if (modeRef.current === require_const.ModeEnum.Refund) {
						require_payLog.default.log("Tyro：退款交易，查询终端信息");
						getTerminalInfo(refundOrder);
					}
				} else {
					require_payLog.default.log("Tyro：恢复上次交易，查询终端信息");
					getTerminalInfo(continueLastOrder);
				}
			}
		}
	}, [load]);
	(0, react.useEffect)(() => {
		let clear;
		if (status === -1) return () => {
			if (clear) clearTimeout(clear);
		};
		if (status === 0) {
			clear = setTimeout(() => {
				require_payLog.default.log("Tyro：连接超时");
				dispatch(require_index.updateState({ status: "fail" }));
				dispatch(require_index.updateCustom({ failCustom: {
					status: "fail",
					fail: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.pay.fail.timeout"),
					title: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail.timeout"),
					warn: null
				} }));
				dispatch(require_index.updateComponent("FailCustom"));
			}, Minute);
			return () => {
				if (clear) clearTimeout(clear);
			};
		}
		clear = setTimeout(() => {
			if (!iClientRef.current) return;
			console.log("倒计时到了");
			require_payLog.default.log(`Tyro：${Minute}倒计时结束`);
			if (client === require_const.ClientEnum.Merchant) setManually(true);
			else {
				dispatch(require_index.updateState({ status: "fail" }));
				dispatch(require_index.updateComponent("Prompt"));
			}
		}, Minute);
		return () => {
			if (clear) clearTimeout(clear);
		};
	}, [status]);
	const onManually = () => {
		require_payLog.default.log("Tyro：点击手动标记");
		dispatch(require_index.updateState({ manual: {
			title: _pisell_utils.locales.getText("page.eftpos-pay.text.request.title")(mode),
			text: _pisell_utils.locales.getText("page.eftpos-pay.text.request.text")(mode)
		} }));
		dispatch(require_index.updateComponent("Manual", true));
	};
	/**
	* @title: 取消支付
	* @description:
	* @return {*}
	* @Author: hongbing.wang
	* @Date: 2024-09-13 13:42
	*/
	const onCancel = () => {
		const iClient = iClientRef.current;
		if (!iClient) return;
		if (status > 0) {
			require_payLog.default.log("Tyro：取消当前交易");
			iClient.cancelCurrentTransaction();
			return;
		}
		if (isCancelRef.current) return;
		require_payLog.default.log("Tyro：取消（交易未开始）");
		isCancelRef.current = true;
		dispatch(require_index.updateState({ status: "fail" }));
		dispatch(require_index.updateCustom({ failCustom: {
			status: "fail",
			fail: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail.cancel"),
			title: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail.cancel.title"),
			warn: null
		} }));
		dispatch(require_index.updateComponent("FailCustom"));
	};
	(0, react.useEffect)(() => () => {
		unloadRef.current = true;
		if (clearRef.current) clearTimeout(clearRef.current);
	}, []);
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-body" }, ((custom === null || custom === void 0 ? void 0 : custom.list) || []).map((item) => /* @__PURE__ */ react.default.createElement(require_step.default, {
		key: item.index,
		pedding: item.pedding,
		resolve: item.resolve,
		reject: item.reject,
		status: item.status
	}))), isManually ? /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ react.default.createElement(antd.Button, {
		block: true,
		className: "pisell-lowcode__eft-pay-step-item-btn",
		onClick: onManually
	}, _pisell_utils.locales.getText("page.eftpos-pay.text.manual.marking"))) : null, /* @__PURE__ */ react.default.createElement("div", {
		className: "pisell-lowcode__eft-pay-step-item-btns",
		style: { flexWrap: "wrap" }
	}, Array.isArray(custom === null || custom === void 0 ? void 0 : custom.btnList) && (custom === null || custom === void 0 ? void 0 : custom.btnList).length > 0 ? /* @__PURE__ */ react.default.createElement("div", { style: {
		width: "100%",
		display: "flex",
		gap: "12px"
	} }, custom === null || custom === void 0 ? void 0 : custom.btnList) : null, !(payStatus === "success" || payStatus === "fail") ? /* @__PURE__ */ react.default.createElement(antd.Button, {
		block: true,
		type: "text",
		disabled: Array.isArray(custom === null || custom === void 0 ? void 0 : custom.btnList) && (custom === null || custom === void 0 ? void 0 : custom.btnList).length > 0,
		className: "pisell-lowcode__eft-pay-step-item-btn",
		onClick: onCancel
	}, _pisell_utils.locales.getText("page.eftpos-pay.text.manual.cancel")) : null));
};
//#endregion
exports.default = tyro_default;
