import { _asyncToGenerator } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../../../../hooks/useEngineContext.js";
import useLogger from "../../../../../hooks/useLogger.js";
import { request } from "../../../../../utils/index.js";
import EFTPos_default from "../../../../eftposPay/EFTPos.js";
import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from "react";
import { Modal, Skeleton } from "antd";
import { useNetwork } from "ahooks";
import "./index.less";
//#region src/components/pay/toB/components/EFTPOS/index.tsx
const updateTransactionReceiveStatus = function() {
	var _ref = _asyncToGenerator(function* (params) {
		return request.getRequest().post("/shop/pay/card-reader/transaction/receive/status", params);
	});
	return function updateTransactionReceiveStatus(_x) {
		return _ref.apply(this, arguments);
	};
}();
const EFTPOS = (props, ref) => {
	var _context$appHelper;
	const { interaction } = ((_context$appHelper = useEngineContext().appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const logger = useLogger();
	const network = useNetwork();
	const [visible, setVisible] = useState(false);
	const eftposRef = useRef(null);
	const payCallbackRef = useRef(null);
	const eftposHandlingRef = useRef(false);
	const eftposOpenParamsRef = useRef(null);
	const eftposInitDataRef = useRef(null);
	const [isEftposLoading, setIsEftposLoading] = useState(false);
	useEffect(() => {
		if (eftposRef.current) {
			var _eftposRef$current, _eftposRef$current$up;
			(_eftposRef$current = eftposRef.current) === null || _eftposRef$current === void 0 || (_eftposRef$current$up = _eftposRef$current.updateNetwork) === null || _eftposRef$current$up === void 0 || _eftposRef$current$up.call(_eftposRef$current, { network: network.online });
		}
	}, [network.online]);
	const closeModal = () => {
		setIsEftposLoading(false);
		eftposHandlingRef.current = false;
		setVisible(false);
	};
	/**
	* @title: 打开支付弹窗
	* @param {ToBPayParams} data 支付参数
	* @param {function} callback 支付结果回调
	* @param {string} source 支付来源（正常支付、恢复支付）
	*/
	const onOpen = function() {
		var _ref2 = _asyncToGenerator(function* ({ source, data, payCallback }) {
			if (eftposHandlingRef.current || visible) {
				logger === null || logger === void 0 || logger.addLog({
					type: "info",
					title: "ToB_EFTPOS_onOpen 重复触发被拦截",
					metadata: { data }
				});
				return;
			}
			eftposHandlingRef.current = true;
			let device_number = "";
			try {
				var _interaction$utils, _interaction$utils$as;
				device_number = yield interaction === null || interaction === void 0 || (_interaction$utils = interaction.utils) === null || _interaction$utils === void 0 || (_interaction$utils$as = _interaction$utils.asyncDataManager) === null || _interaction$utils$as === void 0 ? void 0 : _interaction$utils$as.call(_interaction$utils, {
					module: "get",
					key: "device_number"
				});
			} catch (error) {
				console.error("获取设备编号失败", error);
			}
			const { type, payMethod, params, mode } = data;
			const newData = {
				type,
				code: payMethod.code,
				terminal: { network: network.online },
				apiParams: {
					order_id: params.orderId,
					name: payMethod.name,
					symbol: params.symbol,
					amount: params.amount,
					device_number,
					custom_payment_id: payMethod === null || payMethod === void 0 ? void 0 : payMethod.id,
					custom_payment_code: payMethod === null || payMethod === void 0 ? void 0 : payMethod.code,
					custom_payment_name: payMethod === null || payMethod === void 0 ? void 0 : payMethod.name,
					custom_payment_type: payMethod === null || payMethod === void 0 ? void 0 : payMethod.type,
					operator_type: params === null || params === void 0 ? void 0 : params.operator_type,
					operator_id: params === null || params === void 0 ? void 0 : params.operator_id,
					platform: params === null || params === void 0 ? void 0 : params.platform
				},
				mode
			};
			console.log("newData>>>>>>>>>>>>>>", newData);
			if (!(newData === null || newData === void 0 ? void 0 : newData.type) && (newData === null || newData === void 0 ? void 0 : newData.code)) {
				var _newData$code;
				newData.type = (_newData$code = newData.code) === null || _newData$code === void 0 || (_newData$code = _newData$code.split("_")) === null || _newData$code === void 0 || (_newData$code = _newData$code[1]) === null || _newData$code === void 0 ? void 0 : _newData$code.toLocaleLowerCase();
			}
			logger === null || logger === void 0 || logger.addLog({
				type: "info",
				title: "ToB_EFTPOS_onOpen 开始调用",
				metadata: {
					params: data,
					initData: newData
				}
			});
			eftposOpenParamsRef.current = data;
			payCallbackRef.current = payCallback;
			eftposInitDataRef.current = {
				data: newData,
				source
			};
			setVisible(true);
			setIsEftposLoading(true);
		});
		return function onOpen(_x2) {
			return _ref2.apply(this, arguments);
		};
	}();
	const onEftposLoad = (status) => {
		logger === null || logger === void 0 || logger.addLog({
			type: "info",
			title: "ToB_EFTPOS_onEftposLoad",
			metadata: { status }
		});
		setIsEftposLoading(false);
		if (!status) closeModal();
	};
	const onAfterOpenChange = (v) => {
		logger === null || logger === void 0 || logger.addLog({
			type: "info",
			title: "ToB_EFTPOS_modal_onAfterOpenChange",
			metadata: { eftposModalVisible: v }
		});
		if (v) {
			var _eftposRef$current2, _eftposRef$current2$u;
			const { data, source } = eftposInitDataRef.current;
			(_eftposRef$current2 = eftposRef.current) === null || _eftposRef$current2 === void 0 || (_eftposRef$current2$u = _eftposRef$current2.update) === null || _eftposRef$current2$u === void 0 || _eftposRef$current2$u.call(_eftposRef$current2, data, source);
			eftposInitDataRef.current = null;
		}
	};
	const handleResult = (status, params) => {
		if (!eftposHandlingRef.current) return;
		if (status === "mark_tx_processed" || (params === null || params === void 0 ? void 0 : params.uniquePaymentNumber)) updateTransactionReceiveStatus({
			numbers: params,
			receive_status: "processed"
		});
		if (status === "mark_tx_processed") return;
		if (status === "page" || status === "print") return;
		if (status === "cache") return;
		eftposHandlingRef.current = false;
	};
	/**
	* @title: 支付结果回调
	* @param {string} status 支付状态
	* @param {any} params 支付参数
	*/
	const onPayResult = ({ status, params }) => {
		logger === null || logger === void 0 || logger.addLog({
			type: "info",
			title: "ToB_EFTPOS_onPayResult",
			metadata: {
				status,
				params
			}
		});
		handleResult(status, params);
		if (status === "success" || status === "fail") {
			var _payCallbackRef$curre;
			(_payCallbackRef$curre = payCallbackRef.current) === null || _payCallbackRef$curre === void 0 || _payCallbackRef$curre.call(payCallbackRef, {
				status,
				params
			});
			closeModal();
		}
	};
	useImperativeHandle(ref, () => ({
		open: onOpen,
		close: closeModal
	}));
	return /* @__PURE__ */ React.createElement(Modal, {
		open: visible,
		onCancel: closeModal,
		footer: null,
		closable: false,
		width: "fit-content",
		destroyOnClose: true,
		bodyStyle: { padding: 0 },
		centered: true,
		afterOpenChange: onAfterOpenChange,
		className: "tob-pay-eftpos-modal",
		maskClosable: false
	}, isEftposLoading ? /* @__PURE__ */ React.createElement("div", { className: "tob-pay-eftpos-modal-skeleton" }, /* @__PURE__ */ React.createElement(Skeleton, {
		loading: true,
		active: true,
		round: true,
		paragraph: { rows: 3 }
	}), /* @__PURE__ */ React.createElement(Skeleton, {
		loading: true,
		active: true,
		round: true,
		paragraph: { rows: 3 }
	})) : null, /* @__PURE__ */ React.createElement("div", { style: {
		width: "600px",
		height: isEftposLoading ? "1px" : "auto"
	} }, /* @__PURE__ */ React.createElement(EFTPos_default, {
		ref: eftposRef,
		onEftposLoad,
		onPayResult,
		styles: { deviceContainer: { maxHeight: (props === null || props === void 0 ? void 0 : props.dynamicMaxHeight) || "600px" } },
		isWebPos: props === null || props === void 0 ? void 0 : props.isWebPos
	})));
};
var EFTPOS_default = forwardRef(EFTPOS);
//#endregion
export { EFTPOS_default as default };
