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 _pisell_utils = require("@pisell/utils");
let decimal_js = require("decimal.js");
decimal_js = require_runtime.__toESM(decimal_js);
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let _pisell_icon = require("@pisell/icon");
require("./index.less");
//#region src/components/checkout/components/PaymentDetailModal/index.tsx
function PaymentDetailModal({ amountSymbol, orderInfo, cartData, onClose, summaryData = [], labelMaps, paymentItems = [], customAmount, voidPayment, walletPassData, clientInfo }) {
	console.log("🔄 PaymentDetailModal: orderInfo", orderInfo, cartData);
	const [orderTotalExpanded, setOrderTotalExpanded] = (0, react.useState)(true);
	const [orderPaidExpanded, setOrderPaidExpanded] = (0, react.useState)(true);
	const [balanceToPayExpanded, setBalanceToPayExpanded] = (0, react.useState)(true);
	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 getCustomerInfo = () => {
		var _client$contacts;
		const client = clientInfo || {};
		const getValidName = (value) => {
			return value !== void 0 && value !== null && String(value).trim() !== "" ? String(value).trim() : null;
		};
		const displayName = getValidName(client === null || client === void 0 ? void 0 : client.display_name) || getValidName(client === null || client === void 0 ? void 0 : client.name) || getText("pisell2.checkout.customer.default-name") || "Customer";
		return {
			nickname: (client === null || client === void 0 ? void 0 : client.nickname) && (client === null || client === void 0 ? void 0 : client.nickname) !== "Select a customer" ? client === null || client === void 0 ? void 0 : client.nickname : _pisell_utils.locales.getText("pisell2.checkout.customer.default-name"),
			display_name: displayName,
			email: (client === null || client === void 0 ? void 0 : client.email) || (client === null || client === void 0 || (_client$contacts = client.contacts) === null || _client$contacts === void 0 ? void 0 : _client$contacts.email) || ""
		};
	};
	const CollapseIcon = ({ expanded, color = "black" }) => /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("collapse-icon", { expanded }) }, /* @__PURE__ */ react.default.createElement(_pisell_icon.ChevronUp, { style: {
		color,
		fontSize: "24px"
	} }));
	const shouldShowVoidButton = (item) => {
		return (item.code === "cash" || item.type === "custom") && !item.isSynced;
	};
	const getTotalPaidAmount = () => {
		return paymentItems.filter((item) => !item.voucher_id || item.isSynced).reduce((total, item) => {
			const amount = new decimal_js.default(Math.abs(item.amount));
			const roundingAmount = new decimal_js.default(Number(item.rounding_amount) > 0 ? 0 : item.rounding_amount || 0);
			return new decimal_js.default(total).plus(amount).plus(roundingAmount).toNumber();
		}, 0);
	};
	const getBalanceDueAmount = () => {
		return new decimal_js.default(orderInfo.totalAmount || 0).sub(new decimal_js.default(getTotalPaidAmount())).toNumber().toFixed(2);
	};
	const handleVoidPayment = (paymentItem) => {
		console.log("Void payment:", paymentItem);
		if (voidPayment) voidPayment(paymentItem);
	};
	const renderPaymentItems = () => {
		const depositPayments = paymentItems.filter((item) => item.order_payment_type === "deposit" && item.isSynced);
		const regularPayments = paymentItems.filter((item) => item.isSynced && item.order_payment_type !== "deposit");
		if (!paymentItems || paymentItems.length === 0 || regularPayments.length === 0 && depositPayments.length === 0) return /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("section-content") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("no-payment-text") }, getText("no.payments.recorded") || "No payments recorded"));
		const depositPaymentSummary = depositPayments.reduce((acc, item) => {
			return new decimal_js.default(acc).plus(new decimal_js.default(item.amount)).plus(new decimal_js.default(Number(item.rounding_amount) > 0 ? 0 : item.rounding_amount || 0)).toNumber();
		}, 0);
		const regularPaymentSummary = regularPayments.reduce((acc, item) => {
			return new decimal_js.default(acc).plus(new decimal_js.default(item.amount)).plus(new decimal_js.default(Number(item.rounding_amount) > 0 ? 0 : item.rounding_amount || 0)).toNumber();
		}, 0);
		return /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("section-content") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("payment-record-container") }, depositPayments.length > 0 && /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("payment-item-header") }, /* @__PURE__ */ react.default.createElement("div", null, getText("pisell2.subtotal.deposit") || "Deposit"), /* @__PURE__ */ react.default.createElement("div", null, (0, _pisell_utils.formatAmountWithOptions)(depositPaymentSummary, amountSymbol, {
			precision: 2,
			hideDecimalForWholeNumbers: false,
			useThousandsSeparator: true,
			showCurrencySymbol: true
		}))), depositPayments.map((item) => /* @__PURE__ */ react.default.createElement("div", {
			key: item.uuid,
			className: (0, classnames.default)("payment-item")
		}, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("payment-item-bullet") }), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("payment-item-content") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("payment-item-text") }, item.name, " - ", (/* @__PURE__ */ new Date()).toLocaleDateString(), " ", getText("pisell2.checkout.payment-detail.by") || "by", " ", getCustomerInfo().nickname), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("payment-item-actions") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("payment-item-amount") }, "-", (0, _pisell_utils.formatAmountWithOptions)(new decimal_js.default(Math.abs(item.amount)).plus(new decimal_js.default(Number(item.rounding_amount) > 0 ? 0 : item.rounding_amount || 0)).toNumber(), amountSymbol, {
			precision: 2,
			hideDecimalForWholeNumbers: false,
			useThousandsSeparator: true,
			showCurrencySymbol: true
		})), shouldShowVoidButton(item) && /* @__PURE__ */ react.default.createElement("button", {
			className: (0, classnames.default)("void-button"),
			onClick: () => handleVoidPayment(item),
			type: "button"
		}, getText("void") || "Void"))))), regularPayments.length > 0 && /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("payment-item-header") }, /* @__PURE__ */ react.default.createElement("div", null, getText("payment.text") || "Payment"), /* @__PURE__ */ react.default.createElement("div", null, (0, _pisell_utils.formatAmountWithOptions)(regularPaymentSummary, amountSymbol, {
			precision: 2,
			hideDecimalForWholeNumbers: false,
			useThousandsSeparator: true,
			showCurrencySymbol: true
		}))), regularPayments.map((item) => /* @__PURE__ */ react.default.createElement("div", {
			key: item.uuid,
			className: (0, classnames.default)("payment-item")
		}, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("payment-item-bullet") }), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("payment-item-content") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("payment-item-text") }, item.name, " - ", (/* @__PURE__ */ new Date()).toLocaleDateString(), " ", getText("pisell2.checkout.payment-detail.by") || "by", " ", getCustomerInfo().nickname), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("payment-item-actions") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("payment-item-amount") }, "-", (0, _pisell_utils.formatAmountWithOptions)(new decimal_js.default(Math.abs(item.amount)).plus(new decimal_js.default(Number(item.rounding_amount) > 0 ? 0 : item.rounding_amount || 0)).toNumber(), amountSymbol, {
			precision: 2,
			hideDecimalForWholeNumbers: false,
			useThousandsSeparator: true,
			showCurrencySymbol: true
		})), shouldShowVoidButton(item) && /* @__PURE__ */ react.default.createElement("button", {
			className: (0, classnames.default)("void-button"),
			onClick: () => handleVoidPayment(item),
			type: "button"
		}, getText("void") || "Void")))))));
	};
	const renderVoucherItems = () => {
		var _walletPassData$selec;
		const items = [];
		if (walletPassData === null || walletPassData === void 0 || (_walletPassData$selec = walletPassData.selectedWallet) === null || _walletPassData$selec === void 0 ? void 0 : _walletPassData$selec.length) walletPassData.selectedWallet.forEach((wallet) => {
			items.push(/* @__PURE__ */ react.default.createElement("div", {
				key: `wallet-${wallet.id}`,
				className: (0, classnames.default)("balance-item-row")
			}, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("balance-item-text") }, wallet.name || wallet.product_name || getText("wallet.pass") || "Wallet Pass"), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("balance-item-amount") }, "-", wallet.redeem || (0, _pisell_utils.formatAmountWithOptions)(Math.abs(wallet.savedAmount || 0), amountSymbol, {
				precision: 2,
				hideDecimalForWholeNumbers: false,
				useThousandsSeparator: true,
				showCurrencySymbol: true
			}))));
		});
		if (items.length === 0) return /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("balance-item-row", "empty-state") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("balance-item-text", "empty-text") }, getText("pisell2.checkout.payment-detail.no-wallet-pass-items") || "No WalletPass payment items"));
		return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, items);
	};
	const getDisplayLabel = (item) => {
		return item.label || labelMaps && labelMaps[item.key] || item.key;
	};
	const getTotalAmount = () => {
		const totalItem = summaryData.find((item) => item.key === "sub_total");
		if (totalItem) return totalItem.value;
		return 0;
	};
	const renderSummaryItems = () => {
		if (!summaryData || summaryData.length === 0) return /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("order-details-grid") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("order-details-column") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("order-row") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("order-row-text") }, getText("no.items.found") || "No items found"), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("order-row-amount") }, (0, _pisell_utils.formatAmountWithOptions)(0, amountSymbol, {
			precision: 2,
			hideDecimalForWholeNumbers: false,
			useThousandsSeparator: true,
			showCurrencySymbol: true
		})))));
		const leftItems = summaryData.slice(0, Math.ceil(summaryData.length / 2));
		const rightItems = summaryData.slice(Math.ceil(summaryData.length / 2));
		return /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("order-details-grid") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("order-details-column") }, leftItems.map((item) => {
			let prefix = "";
			if ((item === null || item === void 0 ? void 0 : item.key) !== "discount") prefix = item.value >= 0 ? "+" : "";
			return /* @__PURE__ */ react.default.createElement("div", {
				key: item.key,
				className: (0, classnames.default)("order-row")
			}, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("order-row-label") }, getDisplayLabel(item)), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("order-row-amount") }, prefix, (0, _pisell_utils.formatAmountWithOptions)(item.value, amountSymbol, {
				precision: 2,
				hideDecimalForWholeNumbers: false,
				useThousandsSeparator: true,
				showCurrencySymbol: true
			})));
		})), rightItems.length > 0 && /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("divider-vertical") }), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("order-details-column") }, rightItems.map((item) => {
			let prefix = "";
			if ((item === null || item === void 0 ? void 0 : item.key) !== "discount") prefix = item.value >= 0 ? "+" : "";
			return /* @__PURE__ */ react.default.createElement("div", {
				key: item.key,
				className: (0, classnames.default)("order-row")
			}, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("order-row-label") }, getDisplayLabel(item)), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("order-row-amount") }, prefix, (0, _pisell_utils.formatAmountWithOptions)(item.value, amountSymbol, {
				precision: 2,
				hideDecimalForWholeNumbers: false,
				useThousandsSeparator: true,
				showCurrencySymbol: true
			})));
		}))));
	};
	return /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("payment-detail-modal") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("main-content") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("section-container") }, /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("section-header"),
		onClick: () => setOrderTotalExpanded(!orderTotalExpanded)
	}, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("section-title-container") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("section-title") }, getText("order.total") || "Order Total")), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("section-right") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("section-amount") }, (0, _pisell_utils.formatAmountWithOptions)(getTotalAmount(), amountSymbol, {
		precision: 2,
		hideDecimalForWholeNumbers: false,
		useThousandsSeparator: true,
		showCurrencySymbol: true
	})), /* @__PURE__ */ react.default.createElement(CollapseIcon, { expanded: orderTotalExpanded }))), orderTotalExpanded && /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("section-content") }, renderSummaryItems())), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("divider-horizontal") }), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("section-container") }, /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("section-header"),
		onClick: () => setOrderPaidExpanded(!orderPaidExpanded),
		role: "button",
		tabIndex: 0
	}, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("section-title-container") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("section-title") }, getText("order.paid") || "Order Paid")), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("section-right") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("section-amount") }, "-", (0, _pisell_utils.formatAmountWithOptions)(getTotalPaidAmount(), amountSymbol, {
		precision: 2,
		hideDecimalForWholeNumbers: false,
		useThousandsSeparator: true,
		showCurrencySymbol: true
	})), /* @__PURE__ */ react.default.createElement(CollapseIcon, { expanded: orderPaidExpanded }))), orderPaidExpanded && renderPaymentItems())), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("balance-to-pay-section") }, /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("balance-to-pay-header"),
		onClick: () => setBalanceToPayExpanded(!balanceToPayExpanded),
		role: "button",
		tabIndex: 0
	}, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("section-title-container") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("balance-to-pay-title") }, getText("balance.to.pay") || "Balance to Pay")), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("balance-to-pay-right") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("balance-to-pay-amount") }, (0, _pisell_utils.formatAmountWithOptions)(getBalanceDueAmount(), amountSymbol, {
		precision: 2,
		hideDecimalForWholeNumbers: false,
		useThousandsSeparator: true,
		showCurrencySymbol: true
	})), /* @__PURE__ */ react.default.createElement(CollapseIcon, {
		expanded: balanceToPayExpanded,
		color: "#7F56DA"
	}))), balanceToPayExpanded && /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("balance-to-pay-content") }, renderVoucherItems()), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("balance-due-section") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("balance-due-header") }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("balance-due-title") }, getText("balance.due") || "Balance Due"), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("balance-due-amount") }, (0, _pisell_utils.formatAmountWithOptions)(customAmount || 0, amountSymbol, {
		precision: 2,
		hideDecimalForWholeNumbers: false,
		useThousandsSeparator: true,
		showCurrencySymbol: true
	}))))));
}
//#endregion
exports.default = PaymentDetailModal;
