const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_useEngineContext = require("../../../../hooks/useEngineContext.js");
const require_locales = require("../../locales.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let decimal_js = require("decimal.js");
decimal_js = require_runtime.__toESM(decimal_js);
let _pisell_materials = require("@pisell/materials");
//#region src/components/checkout/components/SavePayLaterHandler/index.tsx
const SavePayLaterHandler = (0, react.forwardRef)(({ amountSymbol = "$", walletPassData, onSavePayLater }, ref) => {
	const [showWalletBalanceConfirmModal, setShowWalletBalanceConfirmModal] = (0, react.useState)(false);
	const [walletBalanceDeductAmount, setWalletBalanceDeductAmount] = (0, react.useState)("");
	const context = require_useEngineContext.default();
	const getText = (key, fallback) => {
		var _context$appHelper$ut, _context$appHelper$ut2, _context$appHelper$ut3, _context$appHelper$ut4, _checkoutLocales;
		const checkoutText = (_checkoutLocales = require_locales.default[((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut.getApp) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.call(_context$appHelper$ut)) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.locales) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut3 = _context$appHelper$ut2.getLocale) === null || _context$appHelper$ut3 === void 0 ? void 0 : _context$appHelper$ut3.call(_context$appHelper$ut2)) || ((_context$appHelper$ut4 = context.appHelper.utils) === null || _context$appHelper$ut4 === void 0 || (_context$appHelper$ut4 = _context$appHelper$ut4.storage) === null || _context$appHelper$ut4 === void 0 ? void 0 : _context$appHelper$ut4.get("umi_locale")) || "en"]) === null || _checkoutLocales === void 0 ? void 0 : _checkoutLocales[key];
		if (checkoutText) return checkoutText;
		return _pisell_utils.locales.getText(key) || fallback;
	};
	const getWalletTotalAmount = () => {
		if (!(walletPassData === null || walletPassData === void 0 ? void 0 : walletPassData.selectedWallet) || !Array.isArray(walletPassData.selectedWallet)) return 0;
		return walletPassData.selectedWallet.reduce((totalDecimal, wallet) => {
			const redeemDecimal = new decimal_js.default(parseFloat(wallet.redeem) || 0);
			return totalDecimal.plus(redeemDecimal);
		}, new decimal_js.default(0)).toNumber();
	};
	const handleSavePayLaterClick = () => {
		if ((walletPassData === null || walletPassData === void 0 ? void 0 : walletPassData.selectedWallet) && walletPassData.selectedWallet.length > 0) {
			setWalletBalanceDeductAmount((0, _pisell_utils.formatAmount)(getWalletTotalAmount(), 2, amountSymbol));
			setShowWalletBalanceConfirmModal(true);
		} else onSavePayLater();
	};
	const handleWalletBalanceConfirm = (action) => {
		setShowWalletBalanceConfirmModal(false);
		onSavePayLater(action);
	};
	(0, react.useImperativeHandle)(ref, () => ({ handleSavePayLaterClick }));
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		visible: showWalletBalanceConfirmModal,
		onCancel: () => handleWalletBalanceConfirm("cancel"),
		closable: false,
		mask: true,
		maskClosable: false,
		centered: true,
		width: 530,
		title: getText("pisell2.checkout.wallet-balance-confirm.title"),
		footer: null,
		className: "wallet-balance-confirm-modal",
		styles: { header: {
			textAlign: "center",
			borderBottom: "1px solid #EAECF0",
			paddingBottom: "16px"
		} }
	}, /* @__PURE__ */ react.default.createElement("div", { style: { padding: "40px 0 0 0" } }, /* @__PURE__ */ react.default.createElement("div", { style: {
		textAlign: "center",
		marginBottom: "32px",
		lineHeight: "1.5"
	} }, /* @__PURE__ */ react.default.createElement("span", { style: {
		color: "#533795",
		fontWeight: 600,
		fontSize: "18px"
	} }, walletBalanceDeductAmount), /* @__PURE__ */ react.default.createElement("span", { style: {
		color: "#101828",
		fontSize: "16px"
	} }, getText("pisell2.checkout.wallet-balance-confirm.description-before")), /* @__PURE__ */ react.default.createElement("span", { style: {
		color: "#533795",
		fontWeight: 600,
		fontSize: "16px"
	} }, getText("pisell2.checkout.wallet-balance-confirm.description-after"))), /* @__PURE__ */ react.default.createElement("div", { style: {
		display: "flex",
		gap: "12px",
		flexDirection: "row"
	} }, /* @__PURE__ */ react.default.createElement(antd.Button, {
		onClick: () => handleWalletBalanceConfirm("cancel"),
		style: {
			flex: "0 1 auto",
			minWidth: "fit-content",
			height: "48px",
			borderRadius: "12px",
			fontSize: "16px",
			fontWeight: 500,
			border: "1px solid #D0D5DD",
			color: "#344054",
			padding: "0 16px",
			whiteSpace: "nowrap"
		}
	}, getText("pisell2.checkout.wallet-balance-confirm.cancel")), /* @__PURE__ */ react.default.createElement(antd.Button, {
		onClick: () => handleWalletBalanceConfirm("save-without-deduct"),
		style: {
			flex: "1 1 auto",
			height: "48px",
			borderRadius: "12px",
			fontSize: "16px",
			fontWeight: 500,
			border: "1px solid #D0D5DD",
			color: "#344054",
			padding: "0 16px",
			whiteSpace: "nowrap"
		}
	}, getText("pisell2.checkout.wallet-balance-confirm.save-without-deduct")), /* @__PURE__ */ react.default.createElement(antd.Button, {
		type: "primary",
		style: {
			flex: "1.2 1 auto",
			height: "48px",
			backgroundColor: "#533795",
			borderColor: "#533795",
			borderRadius: "12px",
			fontWeight: 600,
			fontSize: "16px",
			border: "none",
			boxShadow: "0px 1px 2px rgba(16, 24, 40, 0.05)",
			padding: "0 16px",
			whiteSpace: "nowrap"
		},
		onClick: () => handleWalletBalanceConfirm("apply-deduct-now")
	}, getText("pisell2.checkout.wallet-balance-confirm.apply-deduct-now"))))));
});
SavePayLaterHandler.displayName = "SavePayLaterHandler";
//#endregion
exports.default = SavePayLaterHandler;
