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_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_types = require("../aggregatePayment/types.js");
const require_useMicropay = require("../aggregatePayment/hooks/useMicropay.js");
const require_Action = require("./Action.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
require("./index.less");
//#region src/components/eftposPay/huifu/index.tsx
const Huifu = ({ isNativeApp, onChange }) => {
	var _context$appHelper, _data$custom;
	const data = require_index.useContextData();
	const { interaction } = ((_context$appHelper = require_useEngineContext.default().appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const { statusRef, numRef, netRef, modeRef, amountRef, orderIdRef, deviceRef, payRef } = require_hooks.useStoreRef();
	const dispatch = require_index.useDispatch();
	const fail = require_hooks.useFail(0);
	const failRef = (0, react.useRef)(fail);
	const initRef = (0, react.useRef)(false);
	const unloadRef = (0, react.useRef)(false);
	const clearRef = (0, react.useRef)(null);
	const payment = require_useMicropay.default({
		paymentCode: (data === null || data === void 0 || (_data$custom = data.custom) === null || _data$custom === void 0 ? void 0 : _data$custom.custom_payment_code) || "EFTPOS_HUIFU",
		paymentNum: numRef.current || "",
		shouldContinue: () => !(unloadRef.current || !netRef.current),
		callback: (_instance, response) => {
			console.log("callback = ", response);
			const { status, data } = response || {};
			if (status === require_types.EPaymentStatus.Success) {
				dispatch(require_index.updateNextStep());
				dispatch(require_index.updateCustom({
					card_reader_surcharge: 0,
					receipt: []
				}));
				dispatch(require_index.updateState({
					status: "success",
					title: _pisell_utils.locales.getText("page.eftpos-pay.text.successful")(modeRef.current)
				}));
				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: 0,
							receipt: []
						}
					}, "success")), {}, { receipt: [] }));
				}, 1e3);
			} else if (status === require_types.EPaymentStatus.Failed) handleFail(data);
		}
	});
	(0, react.useEffect)(() => {
		failRef.current = fail;
	}, [fail]);
	(0, react.useEffect)(() => {
		if (statusRef.current === require_const.StatusEnum.Success || statusRef.current === require_const.StatusEnum.Fail) return;
		if (initRef.current) return;
		initRef.current = true;
		if (numRef.current) {
			console.log("payment.query");
			payment.query();
			return;
		}
		console.log("data?.custom =>>>> ", data === null || data === void 0 ? void 0 : data.custom);
		if (!(modeRef.current === require_const.ModeEnum.Pay || modeRef.current === require_const.ModeEnum.FullPay)) {
			var _data$custom2;
			payment.run({ orderPaymentId: data === null || data === void 0 || (_data$custom2 = data.custom) === null || _data$custom2 === void 0 ? void 0 : _data$custom2.order_payment_id });
			return;
		}
	}, []);
	(0, react.useEffect)(() => {
		if (isNativeApp && (data === null || data === void 0 ? void 0 : data.status) === require_const.StatusEnum.Init) {
			var _interaction$utils, _interaction$utils$po;
			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: "other",
				data: { key: "show_close_button" }
			});
		}
	}, [isNativeApp, data === null || data === void 0 ? void 0 : data.status]);
	(0, react.useEffect)(() => {
		return () => {
			unloadRef.current = true;
			if (clearRef.current) clearTimeout(clearRef.current);
		};
	}, []);
	/**
	* 触发支付失败流程：交由 useFail 统一处理失败/重试逻辑
	*/
	const handleFail = (errorData) => {
		var _errorData$data, _failRef$current;
		const { message, response_code } = (errorData === null || errorData === void 0 || (_errorData$data = errorData.data) === null || _errorData$data === void 0 ? void 0 : _errorData$data.external_unified_response) || {};
		const _code = `${response_code || ""}` || require_const.PayStatus.PaymentFailed;
		const _message = message || (errorData === null || errorData === void 0 ? void 0 : errorData.message) || "";
		console.log("handleFail =>>>> ", errorData, _code, _message);
		(_failRef$current = failRef.current) === null || _failRef$current === void 0 || _failRef$current.call(failRef, {
			code: _code,
			message: _message
		}, false);
	};
	/**
	* 更新到加载状态
	*/
	const updateToLoading = () => {
		if (isNativeApp) {
			var _interaction$utils2, _interaction$utils2$p;
			interaction === null || interaction === void 0 || (_interaction$utils2 = interaction.utils) === null || _interaction$utils2 === void 0 || (_interaction$utils2$p = _interaction$utils2.postMessageToApp) === null || _interaction$utils2$p === void 0 || _interaction$utils2$p.call(_interaction$utils2, {
				module: "pos",
				key: "other",
				data: { key: "hide_close_button" }
			});
		}
		dispatch(require_index.updateStatus(require_const.StatusEnum.Loading));
		dispatch(require_index.updateState({ title: _pisell_utils.locales.getText("page.eftpos-pay.text.loading")(modeRef.current) }));
	};
	const onClose = () => {
		onChange("fail", {
			number: data.number,
			uniquePaymentNumber: data.number,
			device: data.device,
			order_id: data.order_id
		});
	};
	const onPayClick = (scanResult) => {
		console.log("scanResult = ", scanResult);
		updateToLoading();
		payment.run({ authCode: scanResult });
	};
	const onScanCallback = (scanResult) => {
		console.log("scanResult = ", scanResult);
		updateToLoading();
		payment.run({ authCode: scanResult });
	};
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, (data === null || data === void 0 ? void 0 : data.status) === require_const.StatusEnum.Init && /* @__PURE__ */ react.default.createElement(require_Action.ScanAction, {
		onClose,
		onScanCallback,
		onPayClick,
		isNativeApp
	}), (data === null || data === void 0 ? void 0 : data.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);
};
//#endregion
exports.default = Huifu;
