import useEngineContext from "../../../../../../hooks/useEngineContext.js";
import shoppingCart from "../../../../../shoppingCart/index.js";
import { useStep } from "../../../../../Sales/Step/index.js";
import { useCart } from "../../../../../Sales/Cart/index.js";
import { useHolder } from "../../../../../Sales/Holder/index.js";
import { useSummary } from "../../../../../Sales/Summary/index.js";
import SelectHolderModal from "../../SelectHolderModal.js";
import ContinueButton from "../ContinueButton/index.js";
import SubTotal from "../../../../../subTotal/index.js";
import Deposit from "../Deposit/index.js";
import VoucherCard from "../../../Voucher/index.js";
import React, { useMemo } from "react";
import { isMobile, locales } from "@pisell/utils";
import "./index.less";
//#region src/components/eventBooking/components/Provider/Cart/Item/index.tsx
const CartItem = () => {
	var _context$appHelper, _cartData$cartItems2;
	const { open, holderData, selectedHolderIds } = useHolder();
	const { deleteCartItem, deleteCartItemByHolder, cartData, addNoteToCartItem } = useCart();
	const { taxTitle, taxRate, totalTaxFee, isPriceIncludeTax, total, deposit, subtotal, surcharge } = useSummary();
	const { isLastStep } = useStep();
	const context = useEngineContext();
	const { translation } = (context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const handleClickCard = (e, type, key) => {
		if (type === "note") return addNoteToCartItem(e);
		if (type === "delete") return deleteCartItem(e);
		if (type === "delete_holders") return deleteCartItemByHolder(key);
	};
	/**
	* @description: 是否显示定金
	* @param {*} useMemo
	* @return {*}
	*/
	const showDeposit = useMemo(() => {
		var _cartData$cartItems;
		return (cartData === null || cartData === void 0 || (_cartData$cartItems = cartData.cartItems) === null || _cartData$cartItems === void 0 ? void 0 : _cartData$cartItems.length) > 0 && (deposit === null || deposit === void 0 ? void 0 : deposit.hasDeposit);
	}, [cartData === null || cartData === void 0 || (_cartData$cartItems2 = cartData.cartItems) === null || _cartData$cartItems2 === void 0 ? void 0 : _cartData$cartItems2.length, deposit === null || deposit === void 0 ? void 0 : deposit.hasDeposit]);
	/**
	* @description: 结算列表数据
	* @param {*} useMemo
	* @return {*}
	*/
	const subTotalList = useMemo(() => {
		const surchargeList = ((surcharge === null || surcharge === void 0 ? void 0 : surcharge.surchargeList) || []).map((d, index) => {
			return {
				key: `custom_surcharge_${index}`,
				label: translation(d === null || d === void 0 ? void 0 : d.name),
				value: d.amount
			};
		});
		return [
			{
				key: "deposit",
				value: deposit === null || deposit === void 0 ? void 0 : deposit.total,
				label: locales.getText("pisell2.appointment.deposit"),
				hidden: !showDeposit
			},
			{
				key: "sub_total",
				value: subtotal
			},
			...surchargeList,
			{
				key: "tax",
				value: totalTaxFee,
				hidden: Number(totalTaxFee) <= 0,
				tax: {
					rate: taxRate,
					is_price_include_tax: isPriceIncludeTax,
					title: taxTitle
				}
			},
			{
				key: "custom_total",
				value: total,
				label: locales.getText("pisell2.event.total"),
				bold: !showDeposit
			}
		];
	}, [
		total,
		subtotal,
		surcharge === null || surcharge === void 0 ? void 0 : surcharge.surchargeList,
		deposit === null || deposit === void 0 ? void 0 : deposit.total,
		taxRate,
		isPriceIncludeTax,
		totalTaxFee,
		taxTitle,
		showDeposit
	]);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", { className: `cart-item-wrap ${isMobile() ? "cart-mobile-item-wrap" : ""}` }, /* @__PURE__ */ React.createElement("div", { className: "content" }, /* @__PURE__ */ React.createElement(shoppingCart, {
		productParams: {
			products: (cartData === null || cartData === void 0 ? void 0 : cartData.cartItems) || [],
			hideAddBtn: false,
			holders: (holderData === null || holderData === void 0 ? void 0 : holderData.selectedHolderItems) || [],
			disabledEdit: isLastStep
		},
		collapseParams: {
			activeKey: selectedHolderIds,
			ghost: true
		},
		platform: "h5",
		emptyText: locales.getText("pisell2.event.empty.service"),
		isOpenGroup: !!(selectedHolderIds === null || selectedHolderIds === void 0 ? void 0 : selectedHolderIds.length),
		onChange: (e, type, key) => handleClickCard(e, type, key)
	})), /* @__PURE__ */ React.createElement("div", { className: "event-footer" }, /* @__PURE__ */ React.createElement(VoucherCard, null), /* @__PURE__ */ React.createElement(SubTotal, {
		list: subTotalList,
		style: { padding: 0 },
		children: showDeposit ? /* @__PURE__ */ React.createElement(Deposit, { showTitle: false }) : null
	}), /* @__PURE__ */ React.createElement(ContinueButton, null))), /* @__PURE__ */ React.createElement(SelectHolderModal, { open }));
};
//#endregion
export { CartItem as default };
