const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_PaymentResultToastProvider = require("../components/PaymentResultToast/PaymentResultToastProvider.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let decimal_js = require("decimal.js");
decimal_js = require_runtime.__toESM(decimal_js);
//#region src/components/checkout/utils/PaymentResultToastUtils.tsx
const formatCurrency = (amount, amountSymbol = "$") => {
	return (0, _pisell_utils.formatAmount)(amount, 2, amountSymbol);
};
const calculateTitle = (status, failureReason) => {
	switch (status) {
		case "paid":
		case "partially_paid": return _pisell_utils.locales.getText("payment.result.success");
		case "failed":
			const reason = failureReason ? ` - ${failureReason}` : "";
			return `${_pisell_utils.locales.getText("payment.result.failed")}${reason}`;
		case "wallet-pass-redemption-failed": return _pisell_utils.locales.getText("pisell2.checkout.wallet-pass.redemption-failed") || "Wallet pass redeem failed";
		default: return _pisell_utils.locales.getText("payment.result.success");
	}
};
const calculateSubtitle = (status, amount, gapAmount, getData) => {
	const getSubtitleClassName = (colorClass) => {
		return `payment-result-toast-subtitle-text ${colorClass}`;
	};
	let text;
	let colorClass;
	const amountSymbol = (getData === null || getData === void 0 ? void 0 : getData("shop_symbol")) || "$";
	switch (status) {
		case "paid":
			if (gapAmount) {
				text = `${_pisell_utils.locales.getText("payment.result.changeDue") || "Change Due"} ${formatCurrency(Math.abs(gapAmount), amountSymbol)}`;
				colorClass = "paid";
			} else {
				text = `${_pisell_utils.locales.getText("payment.result.paid") || "Paid"} ${formatCurrency(amount, amountSymbol)}`;
				colorClass = "paid";
			}
			break;
		case "partially_paid":
			const displayAmount = new decimal_js.default(amount).minus(gapAmount !== null && gapAmount !== void 0 ? gapAmount : 0).toNumber();
			text = `${_pisell_utils.locales.getText("payment.result.partiallyPaid") || "Partially Paid"} ${formatCurrency(displayAmount, amountSymbol)}`;
			colorClass = "partially-paid";
			break;
		case "failed":
			text = `${_pisell_utils.locales.getText("payment.result.amountDue") || "Amount Due"} ${formatCurrency(amount, amountSymbol)}`;
			colorClass = "amount-due";
			break;
		case "wallet-pass-redemption-failed":
			const successAmount = gapAmount !== null && gapAmount !== void 0 ? gapAmount : amount;
			text = `${_pisell_utils.locales.getText("payment.result.partiallyPaid") || "Partially Paid"} ${formatCurrency(successAmount, amountSymbol)}`;
			colorClass = "partially-paid";
			break;
		default: return null;
	}
	return /* @__PURE__ */ react.default.createElement("div", { className: "payment-result-toast-subtitle" }, /* @__PURE__ */ react.default.createElement("div", { className: getSubtitleClassName(colorClass) }, text));
};
const calculateStatusClassName = (status) => {
	switch (status) {
		case "paid":
		case "partially_paid": return "success";
		case "failed": return "failed";
		case "wallet-pass-redemption-failed": return "wallet-pass-failed";
		default: return "success";
	}
};
const calculateAutoCloseConfig = (status, hasChangeDue) => {
	const autoCloseDelay = 30;
	switch (status) {
		case "paid": return {
			autoClose: true,
			autoCloseDelay,
			maskClosable: false
		};
		case "partially_paid": return {
			autoClose: true,
			autoCloseDelay,
			maskClosable: false
		};
		case "failed":
		case "wallet-pass-redemption-failed": return {
			autoClose: false,
			autoCloseDelay,
			maskClosable: false
		};
		default: return {
			autoClose: false,
			autoCloseDelay,
			maskClosable: false
		};
	}
};
const displayPaymentResultToast = function() {
	var _ref = require_asyncToGenerator._asyncToGenerator(function* (params, config) {
		const { paymentStatus, orderTotalAmount, gapAmount, failureReason, autoCloseConfig, actionButtons } = params;
		const { getData, onToastClose } = config;
		if (paymentStatus === "paid") yield require_PaymentResultToastProvider.showPaymentResultToastSuccess({
			mask: !(autoCloseConfig === null || autoCloseConfig === void 0 ? void 0 : autoCloseConfig.autoClose) || false,
			title: calculateTitle(paymentStatus),
			subtitle: calculateSubtitle(paymentStatus, orderTotalAmount, gapAmount, getData),
			autoClose: (autoCloseConfig === null || autoCloseConfig === void 0 ? void 0 : autoCloseConfig.autoClose) || false,
			autoCloseDelay: (autoCloseConfig === null || autoCloseConfig === void 0 ? void 0 : autoCloseConfig.autoCloseDelay) || 3e3,
			clickOutsideToClose: (autoCloseConfig === null || autoCloseConfig === void 0 ? void 0 : autoCloseConfig.autoClose) || false,
			showCountdownProgress: true,
			actionButtons: actionButtons || [],
			onClose: onToastClose || (() => {}),
			closable: !(autoCloseConfig === null || autoCloseConfig === void 0 ? void 0 : autoCloseConfig.autoClose)
		});
		else if (paymentStatus === "partially_paid") yield require_PaymentResultToastProvider.showPaymentResultToastSuccess({
			mask: !(autoCloseConfig === null || autoCloseConfig === void 0 ? void 0 : autoCloseConfig.autoClose) || false,
			title: calculateTitle(paymentStatus),
			subtitle: calculateSubtitle(paymentStatus, orderTotalAmount, gapAmount, getData),
			autoClose: (autoCloseConfig === null || autoCloseConfig === void 0 ? void 0 : autoCloseConfig.autoClose) || false,
			actionButtons: actionButtons || [],
			clickOutsideToClose: (autoCloseConfig === null || autoCloseConfig === void 0 ? void 0 : autoCloseConfig.autoClose) || false,
			closable: !(autoCloseConfig === null || autoCloseConfig === void 0 ? void 0 : autoCloseConfig.autoClose),
			onClose: onToastClose || (() => {})
		});
		else if (paymentStatus === "failed" || paymentStatus === "error") yield require_PaymentResultToastProvider.showPaymentResultToastFailed({
			mask: !(autoCloseConfig === null || autoCloseConfig === void 0 ? void 0 : autoCloseConfig.autoClose) || false,
			title: calculateTitle(paymentStatus, failureReason),
			subtitle: calculateSubtitle(paymentStatus, orderTotalAmount / 100, gapAmount, getData),
			autoClose: (autoCloseConfig === null || autoCloseConfig === void 0 ? void 0 : autoCloseConfig.autoClose) || false,
			actionButtons: actionButtons || [],
			clickOutsideToClose: (autoCloseConfig === null || autoCloseConfig === void 0 ? void 0 : autoCloseConfig.autoClose) || false,
			closable: !(autoCloseConfig === null || autoCloseConfig === void 0 ? void 0 : autoCloseConfig.autoClose),
			onClose: onToastClose || (() => {})
		});
	});
	return function displayPaymentResultToast(_x, _x2) {
		return _ref.apply(this, arguments);
	};
}();
const createShowSuccessToast = (config, getActionButtons) => {
	return function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (result) {
			var _result$response;
			const responseData = (_result$response = result.response) === null || _result$response === void 0 ? void 0 : _result$response.data;
			const paymentStatus = responseData === null || responseData === void 0 ? void 0 : responseData.payment_status;
			const orderTotalAmount = Number((responseData === null || responseData === void 0 ? void 0 : responseData.total_amount) || 0);
			const gapAmount = Number((responseData === null || responseData === void 0 ? void 0 : responseData.amount_gap) || 0);
			yield displayPaymentResultToast({
				paymentStatus,
				orderTotalAmount,
				gapAmount,
				autoCloseConfig: calculateAutoCloseConfig(paymentStatus, !!gapAmount),
				actionButtons: getActionButtons ? getActionButtons(paymentStatus) : []
			}, config);
		});
		return function(_x3) {
			return _ref2.apply(this, arguments);
		};
	}();
};
//#endregion
exports.calculateAutoCloseConfig = calculateAutoCloseConfig;
exports.calculateStatusClassName = calculateStatusClassName;
exports.createShowSuccessToast = createShowSuccessToast;
exports.displayPaymentResultToast = displayPaymentResultToast;
exports.formatCurrency = formatCurrency;
