import { _asyncToGenerator } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { useEngineUtils, useSymbol } from "../../hooks.js";
import { PayMethodCode } from "../../types/storeTypes.js";
import { useOSPayment, useOSPaymentEffect, useToBPay } from "../../store/hooks.js";
import ToBPayCardGroup from "../CardGroup/index.js";
import ConfirmedPayment from "../ConfirmedPayment/index.js";
import ToBPayTitle from "./Header.js";
import ToBPayFooter from "./Footer.js";
import ToBPayCache from "../Cache/index.js";
import ToBPayCustom from "../Custom/index.js";
import { handleEftposPay, isEftposPay } from "./utils.js";
import useLowCode from "../../../../../hooks/useLowCode.js";
import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from "react";
import { Spin } from "antd";
import { LowCodePage, PisellModal } from "@pisell/materials";
import { useNetwork } from "ahooks";
import "./index.less";
//#region src/components/pay/toB/components/Content/index.tsx
const ToBPayContent = forwardRef((props, ref) => {
	var _order$payment, _order$payment2, _modalStatus$paymetho, _modalStatus$paymetho2, _modalStatus$paymetho3, _modalStatus$paymetho4, _modalStatus$paymetho5;
	const network = useNetwork();
	const engineUtils = useEngineUtils();
	const symbol = useSymbol();
	const osPayment = useOSPayment();
	const { osPaymentEffectOn, osPaymentEffectOff } = useOSPaymentEffect();
	const { order, getPayMethodList, payMethodGroupList } = useToBPay();
	const [orderInfo, setOrderInfo] = useState({});
	const [open, setOpen] = useState(false);
	const [modalStatus, setModalStatus] = useState({
		open: false,
		paymethod: null
	});
	const [submitLoading, setSubmitLoading] = useState(false);
	const eftposOpenRef = useRef(false);
	useEffect(() => {
		return () => {
			osPaymentEffectOff();
		};
	}, []);
	useEffect(() => {
		console.log("tob_pay_useEffect_network", network);
		if (eftposOpenRef.current) {
			var _engineUtils$action;
			engineUtils === null || engineUtils === void 0 || (_engineUtils$action = engineUtils.action) === null || _engineUtils$action === void 0 || _engineUtils$action.call(engineUtils, {
				type: "pisell1.handleEftposPayNetWork",
				data: { network: network.online },
				callback() {}
			});
		}
	}, [network.online]);
	const { lowcodeRef, pageFunc } = useLowCode({ onReady: (e) => {
		var _window;
		console.log("tob_pay_onReady", e);
		(_window = window) === null || _window === void 0 || _window.shopApi.walletPayment.open(JSON.stringify({
			customer_id: orderInfo.customer_id,
			total_amount: orderInfo.total_amount,
			order_id: orderInfo.order_id,
			customer_name: orderInfo.customer_name,
			sale_channel: orderInfo.sale_channel
		}));
	} });
	const openModal = (orderInfo) => {
		setOpen(true);
		osPaymentEffectOn();
		osPayment === null || osPayment === void 0 || osPayment.pushOrderAsync({ order_info: orderInfo || {} });
		getPayMethodList();
		setOrderInfo(orderInfo);
	};
	const onCloseModal = () => {
		var _osPayment$submitPayA, _props$callback;
		if (submitLoading) return;
		closeModalAction();
		osPayment === null || osPayment === void 0 || (_osPayment$submitPayA = osPayment.submitPayAsync) === null || _osPayment$submitPayA === void 0 || _osPayment$submitPayA.call(osPayment, order === null || order === void 0 ? void 0 : order.uuid);
		(_props$callback = props.callback) === null || _props$callback === void 0 || _props$callback.call(props, { status: "cancel" });
	};
	const onSubmit = function() {
		var _ref = _asyncToGenerator(function* () {
			if (submitLoading) return;
			setSubmitLoading(true);
			try {
				var _osPayment$submitPayA2, _props$callback2;
				yield osPayment === null || osPayment === void 0 || (_osPayment$submitPayA2 = osPayment.submitPayAsync) === null || _osPayment$submitPayA2 === void 0 ? void 0 : _osPayment$submitPayA2.call(osPayment, order === null || order === void 0 ? void 0 : order.uuid);
				console.log("tob_pay_onSubmit_res");
				(_props$callback2 = props.callback) === null || _props$callback2 === void 0 || _props$callback2.call(props, { status: "success" });
				closeModalAction();
			} catch (error) {
				var _props$callback3;
				console.log("tob_pay_onSubmit_error", error);
				(_props$callback3 = props.callback) === null || _props$callback3 === void 0 || _props$callback3.call(props, { status: "error" });
			} finally {
				setSubmitLoading(false);
			}
		});
		return function onSubmit() {
			return _ref.apply(this, arguments);
		};
	}();
	const closeModalAction = () => {
		setOpen(false);
		osPaymentEffectOff();
	};
	/**
	* @title: 暴露给父组件的接口
	* @description: 打开支付弹窗
	*/
	useImperativeHandle(ref, () => ({ open: openModal }));
	/**
	* @title: 支付方式改变
	*/
	const onChangePayMethod = (item, type) => {
		console.log("onChange", item, type);
		if (item.code === PayMethodCode.CASHMANUAL) setModalStatus({
			open: true,
			paymethod: item
		});
		else if (item.code === PayMethodCode.CUSTOMMANUAL || item.type === "custom") setModalStatus({
			open: true,
			paymethod: item
		});
		else if (isEftposPay(item.code)) {
			eftposOpenRef.current = true;
			handleEftposPay(engineUtils === null || engineUtils === void 0 ? void 0 : engineUtils.action, {
				order,
				payMethod: item,
				symbol,
				network,
				type
			}, (res) => {
				eftposOpenRef.current = false;
				console.log("tob_pay_onChangePayMethod_eftpos_callback", res);
				osPayment === null || osPayment === void 0 || osPayment.pushPaymentAsync(order === null || order === void 0 ? void 0 : order.uuid, (res === null || res === void 0 ? void 0 : res.result) || {});
			});
		}
	};
	/**
	* @title: 支付方式对应弹窗 取消
	*/
	const onModalCancel = () => {
		setModalStatus({
			open: false,
			paymethod: null
		});
	};
	/**
	* @title: 支付方式对应弹窗 确认
	*/
	const onModalConfirm = (amount, feeConfig) => {
		console.log("onConfirmPay", amount, feeConfig);
		const paymethod = modalStatus.paymethod || {};
		const paymentData = {
			amount,
			code: paymethod.code,
			id: paymethod.id,
			name: paymethod.name,
			type: paymethod.type,
			voucher_id: ""
		};
		if ((paymethod.code === PayMethodCode.CUSTOMMANUAL || paymethod.type === "custom") && feeConfig) paymentData.service_charge = {
			percentage: feeConfig.percentage,
			amount: feeConfig.fixedAmount
		};
		osPayment === null || osPayment === void 0 || osPayment.pushPaymentAsync(order === null || order === void 0 ? void 0 : order.uuid, paymentData);
		setModalStatus({
			open: false,
			paymethod: null
		});
	};
	/**
	* @title: 作废支付项
	*/
	const onVoidPayment = function() {
		var _ref2 = _asyncToGenerator(function* (paymentItem) {
			try {
				yield osPayment === null || osPayment === void 0 ? void 0 : osPayment.deletePaymentAsync(order === null || order === void 0 ? void 0 : order.uuid, paymentItem.uuid);
				console.log("Payment voided successfully", paymentItem);
			} catch (error) {
				console.error("Failed to void payment", error);
			}
		});
		return function onVoidPayment(_x) {
			return _ref2.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ React.createElement(PisellModal, {
		open,
		width: "65vw",
		maskClosable: false,
		bodyStyle: { padding: 0 },
		header: true,
		title: /* @__PURE__ */ React.createElement(ToBPayTitle, { amount: (order === null || order === void 0 ? void 0 : order.expect_amount) || "0.00" }),
		footer: /* @__PURE__ */ React.createElement(ToBPayFooter, {
			onCancel: onCloseModal,
			onSubmit,
			submitDisabled: (order === null || order === void 0 || (_order$payment = order.payment) === null || _order$payment === void 0 ? void 0 : _order$payment.length) ? false : true,
			loading: submitLoading
		}),
		closable: true,
		onCancel: onCloseModal
	}, /* @__PURE__ */ React.createElement(Spin, { spinning: submitLoading }, /* @__PURE__ */ React.createElement("div", { className: "tob-pay-container" }, open && /* @__PURE__ */ React.createElement(LowCodePage, {
		lowcodeRef,
		id: "shop.wallet.payment"
	}), (order === null || order === void 0 || (_order$payment2 = order.payment) === null || _order$payment2 === void 0 ? void 0 : _order$payment2.length) ? /* @__PURE__ */ React.createElement(ConfirmedPayment, {
		data: order === null || order === void 0 ? void 0 : order.payment,
		onVoidPayment
	}) : null, payMethodGroupList.map((item) => /* @__PURE__ */ React.createElement(ToBPayCardGroup, {
		key: item.title,
		data: item,
		onChange: onChangePayMethod
	})), modalStatus.open && ((_modalStatus$paymetho = modalStatus.paymethod) === null || _modalStatus$paymetho === void 0 ? void 0 : _modalStatus$paymetho.code) === PayMethodCode.CASHMANUAL ? /* @__PURE__ */ React.createElement(ToBPayCache, {
		title: ((_modalStatus$paymetho2 = modalStatus.paymethod) === null || _modalStatus$paymetho2 === void 0 ? void 0 : _modalStatus$paymetho2.name) || "",
		uuid: (order === null || order === void 0 ? void 0 : order.uuid) || "",
		expect_amount: (order === null || order === void 0 ? void 0 : order.expect_amount) || "0.00",
		open: modalStatus.open,
		onCancel: onModalCancel,
		onOk: onModalConfirm
	}) : null, modalStatus.open && (((_modalStatus$paymetho3 = modalStatus.paymethod) === null || _modalStatus$paymetho3 === void 0 ? void 0 : _modalStatus$paymetho3.code) === PayMethodCode.CUSTOMMANUAL || ((_modalStatus$paymetho4 = modalStatus.paymethod) === null || _modalStatus$paymetho4 === void 0 ? void 0 : _modalStatus$paymetho4.type) === "custom") ? /* @__PURE__ */ React.createElement(ToBPayCustom, {
		title: ((_modalStatus$paymetho5 = modalStatus.paymethod) === null || _modalStatus$paymetho5 === void 0 ? void 0 : _modalStatus$paymetho5.name) || "",
		uuid: (order === null || order === void 0 ? void 0 : order.uuid) || "",
		expect_amount: (order === null || order === void 0 ? void 0 : order.expect_amount) || "0.00",
		open: modalStatus.open,
		onCancel: onModalCancel,
		onOk: onModalConfirm
	}) : null)));
});
//#endregion
export { ToBPayContent as default };
