const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../../../hooks/useEngineContext.js");
const require_index = require("../../../shoppingCart/index.js");
const require_index$1 = require("../../../subTotal/index.js");
const require_context = require("../../context.js");
const require_index$2 = require("../Voucher/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
require("./index.less");
//#region src/components/scanOrder/components/Cart/index.tsx
function Cart(props) {
	var _engineContext$appHel, _engineContext$engine, _businessUtils$getApp, _businessUtils$getApp2;
	const { open, onCancel, onNext, items, summary, embedded = false, confirmLoading = false, extraContent, disabled = false } = props;
	const { runModuleAction, refreshViewModel, scanOrderModule } = require_context.useScanOrderContext();
	const engineContext = require_useEngineContext.default();
	const businessUtils = engineContext === null || engineContext === void 0 || (_engineContext$appHel = engineContext.appHelper) === null || _engineContext$appHel === void 0 || (_engineContext$appHel = _engineContext$appHel.utils) === null || _engineContext$appHel === void 0 ? void 0 : _engineContext$appHel.businessUtils;
	const locale = (engineContext === null || engineContext === void 0 || (_engineContext$engine = engineContext.engine) === null || _engineContext$engine === void 0 || (_engineContext$engine = _engineContext$engine.props) === null || _engineContext$engine === void 0 ? void 0 : _engineContext$engine.locale) || "en";
	const hasGoodPassApp = !!(businessUtils === null || businessUtils === void 0 || (_businessUtils$getApp = businessUtils.getAppByModal) === null || _businessUtils$getApp === void 0 ? void 0 : _businessUtils$getApp.call(businessUtils, "goodpass"));
	const hasDiscountCardApp = !!(businessUtils === null || businessUtils === void 0 || (_businessUtils$getApp2 = businessUtils.getAppByModal) === null || _businessUtils$getApp2 === void 0 ? void 0 : _businessUtils$getApp2.call(businessUtils, "discountcard"));
	const showVoucher = hasGoodPassApp || hasDiscountCardApp;
	const isVisible = embedded || open;
	if (!isVisible) return null;
	const [dineInConfig, setDineInConfig] = (0, react.useState)(null);
	const getConfig = (0, react.useCallback)(require_asyncToGenerator._asyncToGenerator(function* () {
		var _otherParams$dineInCo;
		const otherParams = yield runModuleAction("getOtherParams");
		console.log("otherParams >>>>> ", otherParams);
		setDineInConfig((_otherParams$dineInCo = otherParams === null || otherParams === void 0 ? void 0 : otherParams.dineInConfig) !== null && _otherParams$dineInCo !== void 0 ? _otherParams$dineInCo : {});
	}), [runModuleAction]);
	(0, react.useEffect)(() => {
		getConfig();
	}, []);
	const isShowNote = (0, react.useMemo)(() => {
		return dineInConfig === null || dineInConfig === void 0 ? void 0 : dineInConfig["sale.allow_item_notes"];
	}, [dineInConfig]);
	/** 与 scanOrder Footer 同一套汇总字段与展示格式 */
	const totalDisplayAmount = (0, react.useMemo)(() => {
		var _ref, _ref2, _ref3, _ref4, _ref5, _summary$total_amount;
		const rawAmount = (_ref = (_ref2 = (_ref3 = (_ref4 = (_ref5 = (_summary$total_amount = summary === null || summary === void 0 ? void 0 : summary.total_amount) !== null && _summary$total_amount !== void 0 ? _summary$total_amount : summary === null || summary === void 0 ? void 0 : summary.expect_amount) !== null && _ref5 !== void 0 ? _ref5 : summary === null || summary === void 0 ? void 0 : summary.amount) !== null && _ref4 !== void 0 ? _ref4 : summary === null || summary === void 0 ? void 0 : summary.total) !== null && _ref3 !== void 0 ? _ref3 : summary === null || summary === void 0 ? void 0 : summary.totalAmount) !== null && _ref2 !== void 0 ? _ref2 : summary === null || summary === void 0 ? void 0 : summary.payable_amount) !== null && _ref !== void 0 ? _ref : 0;
		const parsed = Number(rawAmount);
		if (Number.isNaN(parsed)) return "0.00";
		return parsed.toFixed(2);
	}, [summary]);
	const confirmOrderText = String(_pisell_utils.locales.getText("pisell2.scan.order.action.confirm.order") || _pisell_utils.locales.getText("pisell2.event.confirm") || "Confirm");
	const subTotalList = (0, react.useMemo)(() => {
		var _summary$product_amou, _summary$tax_fee, _ref6, _summary$total_amount2, _scanOrderModule$getT, _tempSummary$tax_rate, _ref7, _tempSummary$is_price;
		const subTotal = Number((_summary$product_amou = summary === null || summary === void 0 ? void 0 : summary.product_amount) !== null && _summary$product_amou !== void 0 ? _summary$product_amou : 0);
		const totalTaxFee = Number((_summary$tax_fee = summary === null || summary === void 0 ? void 0 : summary.tax_fee) !== null && _summary$tax_fee !== void 0 ? _summary$tax_fee : 0);
		const total = Number((_ref6 = (_summary$total_amount2 = summary === null || summary === void 0 ? void 0 : summary.total_amount) !== null && _summary$total_amount2 !== void 0 ? _summary$total_amount2 : summary === null || summary === void 0 ? void 0 : summary.expect_amount) !== null && _ref6 !== void 0 ? _ref6 : 0);
		const tempOrder = scanOrderModule === null || scanOrderModule === void 0 || (_scanOrderModule$getT = scanOrderModule.getTempOrder) === null || _scanOrderModule$getT === void 0 ? void 0 : _scanOrderModule$getT.call(scanOrderModule);
		const tempSummary = (tempOrder === null || tempOrder === void 0 ? void 0 : tempOrder.summary) || {};
		const taxTitle = String((tempSummary === null || tempSummary === void 0 ? void 0 : tempSummary.tax_title) || (summary === null || summary === void 0 ? void 0 : summary.tax_title) || "GST/Tax");
		const rawTaxRate = Number((_tempSummary$tax_rate = tempSummary === null || tempSummary === void 0 ? void 0 : tempSummary.tax_rate) !== null && _tempSummary$tax_rate !== void 0 ? _tempSummary$tax_rate : summary === null || summary === void 0 ? void 0 : summary.tax_rate);
		const normalizedTaxRate = Number.isNaN(rawTaxRate) ? 0 : rawTaxRate > 1 ? rawTaxRate / 100 : rawTaxRate;
		const isPriceIncludeTax = Boolean((_ref7 = (_tempSummary$is_price = tempSummary === null || tempSummary === void 0 ? void 0 : tempSummary.is_price_include_tax) !== null && _tempSummary$is_price !== void 0 ? _tempSummary$is_price : tempSummary === null || tempSummary === void 0 ? void 0 : tempSummary.isPriceIncludeTax) !== null && _ref7 !== void 0 ? _ref7 : summary === null || summary === void 0 ? void 0 : summary.is_price_include_tax);
		/** 将 surcharges 数组映射为 SubTotalItem，amount > 0 才展示（与 venueBooking Cart 一致） */
		const surchargeItems = ((summary === null || summary === void 0 ? void 0 : summary.surcharges) || []).filter((s) => Number(s === null || s === void 0 ? void 0 : s.amount) > 0).map((s) => {
			const labelObj = (s === null || s === void 0 ? void 0 : s.label) || (s === null || s === void 0 ? void 0 : s.name) || {};
			const label = labelObj[locale] || labelObj.en || labelObj.original || (s === null || s === void 0 ? void 0 : s.key) || "";
			return {
				key: (s === null || s === void 0 ? void 0 : s.key) || `surcharge_${s === null || s === void 0 ? void 0 : s.surcharge_id}`,
				label,
				value: Number(s === null || s === void 0 ? void 0 : s.amount)
			};
		});
		return [
			{
				key: "sub_total",
				value: Number.isNaN(subTotal) ? 0 : subTotal
			},
			...surchargeItems,
			{
				key: "tax",
				value: Number.isNaN(totalTaxFee) ? 0 : totalTaxFee,
				hidden: Number.isNaN(totalTaxFee) ? true : totalTaxFee <= 0,
				tax: {
					title: taxTitle,
					rate: normalizedTaxRate,
					is_price_include_tax: isPriceIncludeTax
				}
			},
			{
				key: "custom_total",
				value: Number.isNaN(total) ? 0 : total,
				label: _pisell_utils.locales.getText("pisell2.event.total"),
				bold: true
			}
		];
	}, [
		summary,
		scanOrderModule,
		locale
	]);
	const handleCartChange = function() {
		var _ref10 = require_asyncToGenerator._asyncToGenerator(function* (item, type) {
			if (type === "note") {
				var _ref8, _item$product_id, _item$product_variant;
				const productId = (_ref8 = (_item$product_id = item === null || item === void 0 ? void 0 : item.product_id) !== null && _item$product_id !== void 0 ? _item$product_id : item === null || item === void 0 ? void 0 : item.id) !== null && _ref8 !== void 0 ? _ref8 : null;
				const productVariantId = Number((_item$product_variant = item === null || item === void 0 ? void 0 : item.product_variant_id) !== null && _item$product_variant !== void 0 ? _item$product_variant : 0);
				const identityKey = item === null || item === void 0 ? void 0 : item.identity_key;
				if (productId === null && Number.isNaN(productVariantId)) return;
				const identity = {
					product_id: productId,
					product_variant_id: Number.isNaN(productVariantId) ? 0 : productVariantId
				};
				if (typeof identityKey === "string" && identityKey.length > 0) identity.identity_key = identityKey;
				try {
					var _item$note;
					yield runModuleAction("setOrderProductLineNote", identity, String((_item$note = item === null || item === void 0 ? void 0 : item.note) !== null && _item$note !== void 0 ? _item$note : ""));
					yield refreshViewModel();
				} catch (error) {
					console.error("[scanOrder] setOrderProductLineNote failed", {
						error,
						item
					});
				}
				return;
			} else if (type === "delete") {
				var _ref9, _item$product_id2, _item$product_variant2;
				const productId = (_ref9 = (_item$product_id2 = item === null || item === void 0 ? void 0 : item.product_id) !== null && _item$product_id2 !== void 0 ? _item$product_id2 : item === null || item === void 0 ? void 0 : item.id) !== null && _ref9 !== void 0 ? _ref9 : null;
				const productVariantId = Number((_item$product_variant2 = item === null || item === void 0 ? void 0 : item.product_variant_id) !== null && _item$product_variant2 !== void 0 ? _item$product_variant2 : 0);
				const identityKey = item === null || item === void 0 ? void 0 : item.identity_key;
				if (productId === null && Number.isNaN(productVariantId)) return;
				if (typeof identityKey !== "string" || identityKey.length === 0) {
					console.error("[scanOrder] removeProductFromOrder skipped: missing identity_key", { item });
					return;
				}
				try {
					yield runModuleAction("removeProductFromOrder", {
						product_id: productId,
						product_variant_id: Number.isNaN(productVariantId) ? 0 : productVariantId,
						identity_key: identityKey
					});
					yield refreshViewModel();
				} catch (error) {
					console.error("[scanOrder] removeProductFromOrder failed", {
						error,
						item
					});
				}
			}
		});
		return function handleCartChange(_x, _x2) {
			return _ref10.apply(this, arguments);
		};
	}();
	(0, react.useEffect)(() => {
		if (!isVisible) return;
		refreshViewModel();
	}, [isVisible]);
	const cartLineItems = /* @__PURE__ */ react.default.createElement(require_index.default, {
		productParams: {
			products: items || [],
			hideAddBtn: true,
			disabledEdit: false,
			isShowNote,
			isShowDelete: true,
			isShowPackageNote: false,
			isShowChangeHolder: false,
			isShowHolder: false,
			isShowAddHolderButton: false
		},
		platform: "h5",
		emptyText: _pisell_utils.locales.getText("pisell2.scan.order.empty.product"),
		isOpenGroup: false,
		isHideClearCart: true,
		onChange: handleCartChange
	});
	const cartSubtotalSection = /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, showVoucher ? /* @__PURE__ */ react.default.createElement(require_index$2.default, null) : null, /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		list: subTotalList,
		style: { padding: 0 }
	}), extraContent ? /* @__PURE__ */ react.default.createElement("div", { className: "pisell-scan-order-cart-extra-content" }, extraContent) : null);
	const cartBodyClassName = embedded ? "pisell-scan-order-cart-body pisell-scan-order-cart-body-embedded" : "pisell-scan-order-cart-body";
	const cartBodyContent = /* @__PURE__ */ react.default.createElement("div", { className: cartBodyClassName }, cartLineItems, cartSubtotalSection);
	if (embedded) return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, cartBodyContent, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-scan-order-cart-embedded-footer" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		type: "primary",
		block: true,
		onClick: onNext,
		loading: confirmLoading,
		className: disabled ? "pisell-scan-order-cart-embedded-confirm-disabled" : void 0
	}, confirmOrderText)));
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", {
		className: "pisell-scan-order-cart-mask",
		onClick: onCancel
	}), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-scan-order-cart-drawer" }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-scan-order-cart-header" }, /* @__PURE__ */ react.default.createElement("div", {
		className: "pisell-scan-order-cart-sheet-handle-wrap",
		"aria-hidden": true
	}, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-scan-order-cart-sheet-handle" }))), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-scan-order-cart-list-scroll" }, cartLineItems), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-scan-order-cart-bottom-panel" }, cartSubtotalSection, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-scan-order-cart-footer" }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-scan-order-cart-footer-total-col" }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-scan-order-cart-footer-total-label" }, _pisell_utils.locales.getText("pisell2.event.total")), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-scan-order-cart-footer-total-amount" }, "$", totalDisplayAmount)), /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, {
		type: "primary",
		onClick: onNext,
		loading: confirmLoading
	}, _pisell_utils.locales.getText("pisell2.event.continue"))))));
}
//#endregion
exports.default = Cart;
