const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_usePrefix = require("../../../hooks/usePrefix.js");
const require_Action = require("./Action.js");
const require_types = require("./types.js");
const require_utils = require("./utils.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_materials = require("@pisell/materials");
let ahooks = require("ahooks");
require("./index.less");
//#region src/components/eftpos/SettlementModal/index.tsx
const SettlementModal = ({ open, onClose, data }) => {
	var _context$appHelper;
	const context = require_useEngineContext.default();
	const prefix = require_usePrefix.default();
	const { isTerminal, interaction } = (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const [receiptModalShow, setReceiptModalShow] = (0, react.useState)(false);
	const [receiptModalData, setReceiptModalData] = (0, react.useState)(null);
	const [action, setAction] = (0, react.useState)(null);
	const [status, setStatus] = (0, react.useState)(require_types.ESettlementStatus.Processing);
	const runSettlementQuery = (0, ahooks.useMemoizedFn)(() => {
		setAction(null);
		setStatus(require_types.ESettlementStatus.Processing);
		console.log("settlementQuery_start");
		require_utils.settlementQuery(data, (status, res) => {
			console.log("settlementQuery_callback", status, res);
			const parsedData = require_utils.parseMX51Params(res === null || res === void 0 ? void 0 : res.data, status, res);
			console.log("settlementQuery_parsedData", parsedData);
			if (parsedData) setAction(parsedData);
			setStatus(status);
		});
	});
	const onCloseReceiptModal = () => {
		setReceiptModalData(null);
		setReceiptModalShow(false);
	};
	const onElementClick = (element) => {
		console.log("onElementClick", element);
		switch (element.key) {
			case require_types.EButtonKeyType.SettlementComplete:
				onClose();
				break;
			case require_types.EButtonKeyType.RetrySettlement:
				runSettlementQuery();
				break;
			case require_types.EButtonKeyType.PrintMerchantReceipt:
				if (typeof isTerminal === "function" ? isTerminal() : false) {
					var _interaction$utils, _interaction$utils$po;
					const receipt = (element === null || element === void 0 ? void 0 : element.receipt) ? Array.isArray(element === null || element === void 0 ? void 0 : element.receipt) ? element === null || element === void 0 ? void 0 : element.receipt : [element === null || element === void 0 ? void 0 : element.receipt] : [];
					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: "print",
						data: receipt
					});
				} else {
					setReceiptModalShow(true);
					setReceiptModalData(element === null || element === void 0 ? void 0 : element.receipt);
				}
				break;
		}
	};
	(0, react.useEffect)(() => {
		if (open && data) runSettlementQuery();
	}, [
		open,
		data,
		runSettlementQuery
	]);
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		open,
		closable: false,
		header: false,
		footer: null,
		width: 600
	}, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-eftpos-settlement-modal" }, status === require_types.ESettlementStatus.Processing && /* @__PURE__ */ react.default.createElement("div", {
		style: {
			backgroundImage: `url(${prefix}/loading.png)`,
			marginBottom: "24px"
		},
		className: "pisell-lowcode__eft-pay-img pisell-lowcode__eft-pay-load"
	}), status === require_types.ESettlementStatus.Failed && /* @__PURE__ */ react.default.createElement("div", {
		style: {
			backgroundImage: `url(${prefix}/warn.png)`,
			marginBottom: "24px"
		},
		className: "pisell-lowcode__eft-pay-img pisell-lowcode__eft-pay-warn"
	}), /* @__PURE__ */ react.default.createElement(require_Action.default, {
		data: action,
		onClick: onElementClick
	})), receiptModalShow ? /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		open: receiptModalShow,
		onCancel: onCloseReceiptModal,
		maskClosable: true,
		footer: null
	}, /* @__PURE__ */ react.default.createElement("div", { style: {
		padding: "16px",
		display: "flex",
		alignItems: "center",
		justifyContent: "center"
	} }, /* @__PURE__ */ react.default.createElement("span", { style: {
		display: "inline-block",
		whiteSpace: "pre-wrap",
		fontFamily: "monospace",
		backgroundColor: "#fff"
	} }, receiptModalData))) : null);
};
//#endregion
exports.default = SettlementModal;
