const require_runtime = require("../../../_virtual/_rolldown/runtime.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_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_index = require("../../../utils/index.js");
const require_const = require("../const.js");
const require_const$1 = require("./const.js");
const require_helper = require("../helper.js");
const require_index$1 = 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_normal = require("./hooks/normal.js");
const require_useTimeQuery = require("./hooks/useTimeQuery.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
//#region src/components/eftposPay/linkly/index.tsx
const Linkly = ({ api, onChange }) => {
	const utils = require_useEngineContext.default().appHelper.utils || {};
	require_index.request.setRequest(utils === null || utils === void 0 ? void 0 : utils.request);
	const { custom, status } = require_index$1.useContextData();
	const { statusRef, netRef, modeRef, amountRef, orderIdRef, numRef, deviceRef, payRef } = require_hooks.useStoreRef();
	const dispatch = require_index$1.useDispatch();
	const stepRef = (0, react.useRef)(custom === null || custom === void 0 ? void 0 : custom.step);
	const fail = require_hooks.useFail(0);
	const failRef = (0, react.useRef)(fail);
	const initRef = (0, react.useRef)(false);
	const controllerRef = (0, react.useRef)();
	const unloadRef = (0, react.useRef)(false);
	const clearRef = (0, react.useRef)();
	const handleCheck = () => !(unloadRef.current || !netRef.current);
	const handleLinklySuccess = (res) => {
		require_payLog.default.log("Linkly：最终支付成功");
		const resData = (res === null || res === void 0 ? void 0 : res.data) || {};
		let receipt = Array.isArray(resData === null || resData === void 0 ? void 0 : resData.receipt) ? resData.receipt : [resData.receipt];
		receipt = receipt.filter((item) => typeof item === "string").filter(Boolean);
		dispatch(require_index$1.updateNextStep());
		dispatch(require_index$1.updateCustom({
			card_reader_surcharge: (resData === null || resData === void 0 ? void 0 : resData.card_reader_surcharge) || 0,
			receipt
		}));
		dispatch(require_index$1.updateState({
			status: "success",
			title: _pisell_utils.locales.getText("page.eftpos-pay.text.successful")(modeRef.current)
		}));
		clearRef.current = setTimeout(() => {
			var _deviceRef$current;
			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: (resData === null || resData === void 0 ? void 0 : resData.card_reader_surcharge) || 0,
					receipt
				}
			}, "success")), {}, { receipt: ((_deviceRef$current = deviceRef.current) === null || _deviceRef$current === void 0 || (_deviceRef$current = _deviceRef$current.setting) === null || _deviceRef$current === void 0 ? void 0 : _deviceRef$current.receipts) ? [] : receipt }));
		}, 1e3);
	};
	const { payOrRefund } = require_normal.useLinklyPayOrRefund(handleCheck);
	const { linklyCancel } = require_normal.useLinklyCancel(handleCheck);
	const { run: resultQuery, stop: stopResultQuery } = require_useTimeQuery.useTimeResultQuery(api, handleCheck, handleLinklySuccess);
	const { linklySignature } = require_normal.useLinklySignature(handleCheck);
	(0, react.useEffect)(() => {
		stepRef.current = custom === null || custom === void 0 ? void 0 : custom.step;
	}, [custom === null || custom === void 0 ? void 0 : custom.step]);
	(0, react.useEffect)(() => {
		failRef.current = fail;
	}, [fail]);
	(0, react.useEffect)(() => {
		var _utils$isTerminal, _utils$interaction;
		console.log("statusRef.current11111", statusRef.current);
		if (statusRef.current === require_const.StatusEnum.Success || statusRef.current === require_const.StatusEnum.Fail) return;
		if (initRef.current) return;
		initRef.current = true;
		if (numRef.current) {
			resultQuery();
			return;
		}
		if ((utils === null || utils === void 0 || (_utils$isTerminal = utils.isTerminal) === null || _utils$isTerminal === void 0 ? void 0 : _utils$isTerminal.call(utils)) && (utils === null || utils === void 0 || (_utils$interaction = utils.interaction) === null || _utils$interaction === void 0 || (_utils$interaction = _utils$interaction.utils) === null || _utils$interaction === void 0 ? void 0 : _utils$interaction.asyncDataManager)) (function() {
			var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
				try {
					var _utils$interaction2, _utils$interaction2$a;
					const activeStatus = yield utils === null || utils === void 0 || (_utils$interaction2 = utils.interaction) === null || _utils$interaction2 === void 0 || (_utils$interaction2 = _utils$interaction2.utils) === null || _utils$interaction2 === void 0 || (_utils$interaction2$a = _utils$interaction2.asyncDataManager) === null || _utils$interaction2$a === void 0 ? void 0 : _utils$interaction2$a.call(_utils$interaction2, {
						module: "get",
						key: "pay_webview_active"
					});
					if (activeStatus !== "unActive") {
						console.log("当前页面活跃状态，可以进行支付");
						payOrRefund(resultQuery, handleLinklySuccess);
					} else {
						console.log("当前页面非活跃状态，无法进行支付");
						require_payLog.default.log("Linkly：页面非活跃状态，阻断支付", { activeStatus });
					}
				} catch (error) {
					require_payLog.default.log("Linkly：获取页面活跃状态失败", { error });
					payOrRefund(resultQuery, handleLinklySuccess);
				}
			});
			return function _payOrRefund() {
				return _ref.apply(this, arguments);
			};
		})()();
		else {
			console.log("非terminal环境，直接进行支付");
			payOrRefund(resultQuery, handleLinklySuccess);
		}
	}, []);
	(0, react.useEffect)(() => {
		return () => {
			var _controllerRef$curren;
			(_controllerRef$curren = controllerRef.current) === null || _controllerRef$curren === void 0 || _controllerRef$curren.abort();
			unloadRef.current = true;
			if (clearRef.current) clearTimeout(clearRef.current);
		};
	}, []);
	/**
	* 取消支付
	*/
	const onCancel = () => {
		require_payLog.default.log("Linkly：点击取消支付");
		stopResultQuery();
		linklyCancel(resultQuery);
	};
	const onDecline = () => {
		require_payLog.default.log("Linkly：签名-点击拒绝");
		stopResultQuery();
		linklySignature(2, resultQuery);
	};
	const onAccept = () => {
		require_payLog.default.log("Linkly：签名-点击接受");
		stopResultQuery();
		linklySignature(3, resultQuery);
	};
	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
	})), status === require_const.StatusEnum.Loading ? /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip" }, _pisell_utils.locales.getText("page.eftpos-pay.text.processing")) : null, status === require_const.StatusEnum.Question ? /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip" }, _pisell_utils.locales.getText("page.eftpos-pay.text.receipt.again.title")), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip" }, _pisell_utils.locales.getText("page.eftpos-pay.text.receipt.again.text"))) : null, status === require_const.StatusEnum.Success ? /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item pisell-lowcode__eft-pay-step-item-tip" }, _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.end")) : null), (custom === null || custom === void 0 ? void 0 : custom.actionStatus) === require_const$1.LinklyActionStatus.Cancel ? /* @__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: onCancel,
		disabled: !!(custom === null || custom === void 0 ? void 0 : custom.buttonDisabled)
	}, _pisell_utils.locales.getText("page.eftpos-pay.text.cancel"))) : null, (custom === null || custom === void 0 ? void 0 : custom.actionStatus) === require_const$1.LinklyActionStatus.CancelWaiting || (custom === null || custom === void 0 ? void 0 : custom.actionStatus) === require_const$1.LinklyActionStatus.SignatureWaiting ? /* @__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",
		disabled: true
	}, _pisell_utils.locales.getText("page.eftpos-pay.text.leave"))) : null, (custom === null || custom === void 0 ? void 0 : custom.actionStatus) === require_const$1.LinklyActionStatus.Signature ? /* @__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: onDecline
	}, _pisell_utils.locales.getText("page.eftpos-pay.text.decline")), /* @__PURE__ */ react.default.createElement(antd.Button, {
		block: true,
		type: "primary",
		className: "pisell-lowcode__eft-pay-step-item-btn",
		onClick: onAccept
	}, _pisell_utils.locales.getText("page.eftpos-pay.text.accept"))) : null);
};
//#endregion
exports.default = Linkly;
