const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_index = require("../../../../pro/priceKeyboard/components/discountKeyboard/index.js");
const require_index$1 = require("../../../../components/booking/components/clearCartModal/index.js");
const require_index$2 = require("../../../../components/booking/components/customiseItemModal/index.js");
const require_index$3 = require("../Voucher/index.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");
let _pisell_icon = require("@pisell/icon");
require("./index.less");
//#region src/plus/saleDetail/components/CartActions/index.tsx
function CartActions({ dataSource, className, hasCartItems }) {
	var _cart$items, _cart$bookings, _summary$summary2;
	const Toast = (0, _pisell_materials.useToast)();
	const [showClearModal, setShowClearModal] = (0, react.useState)(false);
	const [showPriceAdjustModal, setShowPriceAdjustModal] = (0, react.useState)(false);
	const [showCustomiseModal, setShowCustomiseModal] = (0, react.useState)(false);
	const [showVoucher, setShowVoucher] = (0, react.useState)(false);
	const { cart, sales, summary } = dataSource;
	const canClear = hasCartItems !== null && hasCartItems !== void 0 ? hasCartItems : Boolean(((_cart$items = cart.items) === null || _cart$items === void 0 ? void 0 : _cart$items.length) || ((_cart$bookings = cart.bookings) === null || _cart$bookings === void 0 ? void 0 : _cart$bookings.length));
	const tempOrder = sales.tempOrder || cart.tempOrder;
	const currencySymbol = (tempOrder === null || tempOrder === void 0 ? void 0 : tempOrder.currency_symbol) || "$";
	const currentTotalPrice = (0, react.useMemo)(() => {
		var _summary$summary;
		return new decimal_js.default(((_summary$summary = summary.summary) === null || _summary$summary === void 0 ? void 0 : _summary$summary.product_amount) || 0).toNumber();
	}, [(_summary$summary2 = summary.summary) === null || _summary$summary2 === void 0 ? void 0 : _summary$summary2.product_amount]);
	const currentTotalPriceAfterDiscount = (0, react.useMemo)(() => new decimal_js.default(currentTotalPrice).minus((tempOrder === null || tempOrder === void 0 ? void 0 : tempOrder.shop_discount) || 0).toNumber(), [currentTotalPrice, tempOrder === null || tempOrder === void 0 ? void 0 : tempOrder.shop_discount]);
	const discountList = cart.discountList;
	const usedVouchersCount = (0, react.useMemo)(() => discountList.filter((discount) => discount === null || discount === void 0 ? void 0 : discount.isSelected).length, [discountList]);
	const shouldShowWarning = (0, react.useMemo)(() => {
		return canClear && discountList.length > 0 && !discountList.some((discount) => discount === null || discount === void 0 ? void 0 : discount.isSelected) && discountList.some((discount) => discount === null || discount === void 0 ? void 0 : discount.isManualSelect);
	}, [canClear, discountList]);
	const handleOpenClearModal = (0, react.useCallback)(() => {
		if (!canClear) return;
		setShowClearModal(true);
	}, [canClear]);
	const handleCloseClearModal = (0, react.useCallback)(() => {
		setShowClearModal(false);
	}, []);
	const handleOpenCustomiseModal = (0, react.useCallback)(() => {
		setShowCustomiseModal(true);
	}, []);
	const handleCloseCustomiseModal = (0, react.useCallback)(() => {
		setShowCustomiseModal(false);
	}, []);
	const handleOpenPriceAdjustModal = (0, react.useCallback)(() => {
		if (!canClear) {
			Toast.info(_pisell_utils.locales.getText("pisell2.sale-detail.no-items-to-adjust"));
			return;
		}
		setShowPriceAdjustModal(true);
	}, [Toast, canClear]);
	const handleClosePriceAdjustModal = (0, react.useCallback)(() => {
		setShowPriceAdjustModal(false);
	}, []);
	const handleOpenVoucher = (0, react.useCallback)(() => {
		setShowVoucher(true);
	}, []);
	const handleCloseVoucher = (0, react.useCallback)(() => {
		setShowVoucher(false);
	}, []);
	const handlePriceAdjustConfirm = (0, react.useCallback)(function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (discountAmount) {
			try {
				const normalizedDiscountAmount = new decimal_js.default(discountAmount || 0).toDecimalPlaces(2, decimal_js.default.ROUND_DOWN).toString();
				sales.setShopDiscount(normalizedDiscountAmount);
				yield cart.recalcSummary();
				setShowPriceAdjustModal(false);
				Toast.success(_pisell_utils.locales.getText("pisell2.sale-detail.price-adjusted-success"));
			} catch (e) {
				console.error("[SaleDetail] setShopDiscount failed", e);
			}
		});
		return function(_x) {
			return _ref.apply(this, arguments);
		};
	}(), [
		Toast,
		cart,
		sales
	]);
	const handleCustomiseItemSave = (0, react.useCallback)(function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (formData) {
			try {
				yield cart.addCustomProduct(formData);
				setShowCustomiseModal(false);
				Toast.success(_pisell_utils.locales.getText("pisell2.customise-item.save-success"));
			} catch (e) {
				console.error("[SaleDetail] addCustomProduct failed", e);
			}
		});
		return function(_x2) {
			return _ref2.apply(this, arguments);
		};
	}(), [Toast, cart]);
	const handleClearItemsOnly = (0, react.useCallback)(require_asyncToGenerator._asyncToGenerator(function* () {
		try {
			yield sales.clearCartItemsOnly();
			setShowClearModal(false);
		} catch (e) {
			console.error("[SaleDetail] clearCartItemsOnly failed", e);
		}
	}), [sales]);
	const handleClearAllAndReset = (0, react.useCallback)(require_asyncToGenerator._asyncToGenerator(function* () {
		try {
			yield sales.clearCartAndReset();
			setShowClearModal(false);
		} catch (e) {
			console.error("[SaleDetail] clearCartAndReset failed", e);
		}
	}), [sales]);
	return /* @__PURE__ */ react.default.createElement("section", { className: className ? `pisell-sale-detail-cart-actions ${className}` : "pisell-sale-detail-cart-actions" }, /* @__PURE__ */ react.default.createElement("div", { className: "pisell-sale-detail-cart-actions__buttons" }, /* @__PURE__ */ react.default.createElement(antd.Button, {
		className: "pisell-sale-detail-cart-actions__button pisell-sale-detail-cart-actions__button--clear",
		icon: /* @__PURE__ */ react.default.createElement(_pisell_icon.Trash01, { size: 22 }),
		onClick: handleOpenClearModal,
		type: "text",
		disabled: !canClear
	}), /* @__PURE__ */ react.default.createElement(antd.Button, {
		className: "pisell-sale-detail-cart-actions__button pisell-sale-detail-cart-actions__button--custom-item",
		icon: /* @__PURE__ */ react.default.createElement(_pisell_icon.PlusCircle, { size: 22 }),
		onClick: handleOpenCustomiseModal,
		type: "text"
	}, _pisell_utils.locales.getText("pisell2.sale-detail.custom-item")), /* @__PURE__ */ react.default.createElement(antd.Button, {
		className: "pisell-sale-detail-cart-actions__button pisell-sale-detail-cart-actions__button--manual-order",
		icon: /* @__PURE__ */ react.default.createElement(_pisell_icon.Calculator, { size: 22 }),
		onClick: handleOpenPriceAdjustModal,
		type: "text"
	}, _pisell_utils.locales.getText("pisell2.sale-detail.manual-order")), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-sale-detail-cart-actions__promotion-wrapper" }, /* @__PURE__ */ react.default.createElement(antd.Button, {
		className: "pisell-sale-detail-cart-actions__button pisell-sale-detail-cart-actions__button--promotion",
		icon: /* @__PURE__ */ react.default.createElement(_pisell_icon.Sale03, { size: 22 }),
		onClick: handleOpenVoucher,
		type: "text"
	}, _pisell_utils.locales.getText("pisell2.sale-detail.promotion")), usedVouchersCount > 0 && /* @__PURE__ */ react.default.createElement("div", { className: "pisell-sale-detail-cart-actions__voucher-badge" }, usedVouchersCount), shouldShowWarning && /* @__PURE__ */ react.default.createElement("div", { className: "pisell-sale-detail-cart-actions__voucher-warning-icon" }, "!"))), /* @__PURE__ */ react.default.createElement(require_index$1.default, {
		visible: showClearModal,
		onCancel: handleCloseClearModal,
		onClearItemsOnly: handleClearItemsOnly,
		onClearAllAndReset: handleClearAllAndReset
	}), /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		visible: showCustomiseModal,
		onClose: handleCloseCustomiseModal,
		onSave: handleCustomiseItemSave,
		currencySymbol
	}), /* @__PURE__ */ react.default.createElement(require_index.default, {
		containerProps: {
			renderType: "modal",
			open: showPriceAdjustModal,
			onCancel: handleClosePriceAdjustModal
		},
		amountSymbol: currencySymbol,
		selectType: "dark",
		originalValue: currentTotalPrice,
		defaultValue: currentTotalPriceAfterDiscount,
		onEnter: handlePriceAdjustConfirm
	}), /* @__PURE__ */ react.default.createElement(require_index$3.default, {
		dataSource: {
			sales,
			cart
		},
		renderType: "modal",
		visible: showVoucher,
		onClose: handleCloseVoucher
	}));
}
//#endregion
exports.CartActions = CartActions;
