import useEngineContext from "../../../../../hooks/useEngineContext.js";
import useLogger from "../../../../../hooks/useLogger.js";
import { forwardRef, useImperativeHandle } from "react";
//#region src/components/pay/toB/components/MX51/index.tsx
const getUuid = () => {
	return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxx".replace(/[xy]/g, (c) => {
		const r = Math.random() * 16 | 0;
		return (c === "x" ? r : r & 3 | 8).toString(16);
	});
};
const MX51 = (props, ref) => {
	var _context$appHelper;
	const logger = useLogger();
	const context = useEngineContext();
	const action = context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.action;
	const onOpen = ({ data, payCallback }) => {
		const { params, mode } = data;
		const uniquePaymentNumber = getUuid();
		const newData = {
			module: mode === "refund" ? "refund" : "payment",
			key: "mx51",
			data: {
				order_id: params.orderId,
				amount: params.amount,
				surcharge: params.surcharge,
				uniquePaymentNumber
			}
		};
		logger === null || logger === void 0 || logger.addLog({
			type: "info",
			title: "ToB_MX51_onOpen 开始调用",
			metadata: { params: newData }
		});
		action({
			type: "webpos.handleMX51Pay",
			data: newData,
			callback: (res) => {
				let isSame = false;
				if (res === null || res === void 0 ? void 0 : res.uniquePaymentNumber) {
					const _res = res;
					isSame = (_res === null || _res === void 0 ? void 0 : _res.uniquePaymentNumber) === uniquePaymentNumber;
					if (isSame) payCallback === null || payCallback === void 0 || payCallback(_res);
				} else {
					var _res$data, _res$data2;
					const _res = res;
					const newRes = {
						result: (_res === null || _res === void 0 ? void 0 : _res.code) == "200" ? true : false,
						msg: _res === null || _res === void 0 ? void 0 : _res.msg,
						amount: _res === null || _res === void 0 || (_res$data = _res.data) === null || _res$data === void 0 ? void 0 : _res$data.amount,
						surcharge: _res === null || _res === void 0 || (_res$data2 = _res.data) === null || _res$data2 === void 0 ? void 0 : _res$data2.surcharge,
						uniquePaymentNumber: _res === null || _res === void 0 ? void 0 : _res.uuid
					};
					isSame = (_res === null || _res === void 0 ? void 0 : _res.uuid) === uniquePaymentNumber;
					if (isSame) payCallback === null || payCallback === void 0 || payCallback(newRes);
				}
				logger === null || logger === void 0 || logger.addLog({
					type: "info",
					title: "ToB_MX51_onOpen_callback",
					metadata: {
						msg: isSame ? "交易流水号一致" : "交易流水号不一致",
						res
					}
				});
			}
		});
	};
	useImperativeHandle(ref, () => ({ open: onOpen }));
	return null;
};
var MX51_default = forwardRef(MX51);
//#endregion
export { MX51_default as default };
