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