const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_hooks = require("../../hooks.js");
const require_storeTypes = require("../../types/storeTypes.js");
const require_hooks$1 = require("../../store/hooks.js");
const require_index = require("../CardGroup/index.js");
const require_index$1 = require("../ConfirmedPayment/index.js");
const require_Header = require("./Header.js");
const require_Footer = require("./Footer.js");
const require_index$2 = require("../Cache/index.js");
const require_index$3 = require("../Custom/index.js");
const require_utils = require("./utils.js");
const require_useLowCode = require("../../../../../hooks/useLowCode.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_materials = require("@pisell/materials");
let ahooks = require("ahooks");
require("./index.less");
//#region src/components/pay/toB/components/Content/index.tsx
const ToBPayContent = (0, react.forwardRef)((props, ref) => {
	var _order$payment, _order$payment2, _modalStatus$paymetho, _modalStatus$paymetho2, _modalStatus$paymetho3, _modalStatus$paymetho4, _modalStatus$paymetho5;
	const network = (0, ahooks.useNetwork)();
	const engineUtils = require_hooks.useEngineUtils();
	const symbol = require_hooks.useSymbol();
	const osPayment = require_hooks$1.useOSPayment();
	const { osPaymentEffectOn, osPaymentEffectOff } = require_hooks$1.useOSPaymentEffect();
	const { order, getPayMethodList, payMethodGroupList } = require_hooks$1.useToBPay();
	const [orderInfo, setOrderInfo] = (0, react.useState)({});
	const [open, setOpen] = (0, react.useState)(false);
	const [modalStatus, setModalStatus] = (0, react.useState)({
		open: false,
		paymethod: null
	});
	const [submitLoading, setSubmitLoading] = (0, react.useState)(false);
	const eftposOpenRef = (0, react.useRef)(false);
	(0, react.useEffect)(() => {
		return () => {
			osPaymentEffectOff();
		};
	}, []);
	(0, react.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 } = require_useLowCode.default({ 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 = require_asyncToGenerator._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: 打开支付弹窗
	*/
	(0, react.useImperativeHandle)(ref, () => ({ open: openModal }));
	/**
	* @title: 支付方式改变
	*/
	const onChangePayMethod = (item, type) => {
		console.log("onChange", item, type);
		if (item.code === require_storeTypes.PayMethodCode.CASHMANUAL) setModalStatus({
			open: true,
			paymethod: item
		});
		else if (item.code === require_storeTypes.PayMethodCode.CUSTOMMANUAL || item.type === "custom") setModalStatus({
			open: true,
			paymethod: item
		});
		else if (require_utils.isEftposPay(item.code)) {
			eftposOpenRef.current = true;
			require_utils.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 === require_storeTypes.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 = require_asyncToGenerator._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.default.createElement(_pisell_materials.PisellModal, {
		open,
		width: "65vw",
		maskClosable: false,
		bodyStyle: { padding: 0 },
		header: true,
		title: /* @__PURE__ */ react.default.createElement(require_Header.default, { amount: (order === null || order === void 0 ? void 0 : order.expect_amount) || "0.00" }),
		footer: /* @__PURE__ */ react.default.createElement(require_Footer.default, {
			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.default.createElement(antd.Spin, { spinning: submitLoading }, /* @__PURE__ */ react.default.createElement("div", { className: "tob-pay-container" }, open && /* @__PURE__ */ react.default.createElement(_pisell_materials.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.default.createElement(require_index$1.default, {
		data: order === null || order === void 0 ? void 0 : order.payment,
		onVoidPayment
	}) : null, payMethodGroupList.map((item) => /* @__PURE__ */ react.default.createElement(require_index.default, {
		key: item.title,
		data: item,
		onChange: onChangePayMethod
	})), modalStatus.open && ((_modalStatus$paymetho = modalStatus.paymethod) === null || _modalStatus$paymetho === void 0 ? void 0 : _modalStatus$paymetho.code) === require_storeTypes.PayMethodCode.CASHMANUAL ? /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		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) === require_storeTypes.PayMethodCode.CUSTOMMANUAL || ((_modalStatus$paymetho4 = modalStatus.paymethod) === null || _modalStatus$paymetho4 === void 0 ? void 0 : _modalStatus$paymetho4.type) === "custom") ? /* @__PURE__ */ react.default.createElement(require_index$3.default, {
		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
exports.default = ToBPayContent;
