import { _objectSpread2 } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import shoppingCart from "../../../../shoppingCart/index.js";
import { formatCurrentUser, formatHolder, getMultiResources, openForm, updateCartResources } from "../../../utils.js";
import SubTotal from "../../../../subTotal/index.js";
import { useAppointmentBookingContext } from "../../../context.js";
import { contextUtils, useAppointmentBooking, useAppointmentBookingAccount, useAppointmentBookingLogin, useAppointmentBookingResource, useAppointmentBookingSetting, useConfig } from "../../../hooks.js";
import Deposit from "../../Deposit/index.js";
import { handleAddHolder } from "../utils.js";
import ContinueButton from "../ContinueButton/index.js";
import VoucherCard from "../../Voucher/index.js";
import React, { useMemo } from "react";
import { locales } from "@pisell/utils";
import "./index.less";
//#region src/components/appointmentBooking/components/Cart/Item/index.tsx
const CartItem = ({ onNext, renderContactInfo }) => {
	var _businessUtils$getApp, _businessUtils$getApp2, _currentStep$step, _currentStep$step2;
	const appointmentBooking = useAppointmentBooking();
	const { carts, currentStep, setResourceOtherParams, resourceOtherParams, summary, isLastStep, currentSelectedResources, currentStepParams, isErrorHolder } = useAppointmentBookingContext();
	const { accountLists, getHolders, addAccount } = useAppointmentBookingAccount();
	const { getResourceList } = useAppointmentBookingResource();
	const { isFormSubject, bookingSubject, isRetailTemplate } = useAppointmentBookingSetting();
	const { isLogin } = useAppointmentBookingLogin();
	const { isMobile } = useConfig();
	const utils = contextUtils();
	const { businessUtils, translation } = utils;
	const { taxTitle, totalTaxFee, isPriceIncludeTax, total, deposit } = summary;
	/**
	* @title: 当前客户
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-05-14 14:59
	*/
	const currentUser = useMemo(() => {
		return formatCurrentUser(utils) || {};
	}, []);
	/**
	* @title: 格式化购物车ids
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2025-05-06 10:02
	*/
	const holderIds = useMemo(() => {
		if (isFormSubject) return [];
		return accountLists.map((item) => item.id);
	}, [accountLists, isFormSubject]);
	const holders = useMemo(() => {
		if (isFormSubject) return [];
		return formatHolder(accountLists);
	}, [accountLists, isFormSubject]);
	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 handleClickCard = (e, type, val) => {
		console.log(e, type, val, "handleClickCard");
		if (type === "note") return appointmentBooking.updateCart(_objectSpread2({ _id: e === null || e === void 0 ? void 0 : e._id }, val));
		if (type === "delete") return appointmentBooking.deleteCart(e === null || e === void 0 ? void 0 : e._id);
		if (type === "delete_holders") return appointmentBooking.removeAccount(val);
		if (type === "change_resource") {
			setResourceOtherParams(_objectSpread2(_objectSpread2({}, resourceOtherParams), {}, { errorList: null }));
			val.like_status = "like";
			appointmentBooking.updateCart({
				_id: e === null || e === void 0 ? void 0 : e._id,
				resources: updateCartResources(e, val, currentSelectedResources)
			});
			getResourceList();
			return;
		}
		if (type === "change_holder") {
			if (!val) appointmentBooking.deleteCartItemInfo("holder", e === null || e === void 0 ? void 0 : e._id);
			else appointmentBooking.updateCart({
				_id: e === null || e === void 0 ? void 0 : e._id,
				account: val
			});
			return;
		}
		if (type === "add_holder") {
			const relationShipFormId = bookingSubject === null || bookingSubject === void 0 ? void 0 : bookingSubject.relationship_form_id;
			return openForm({
				form_id: relationShipFormId || (bookingSubject === null || bookingSubject === void 0 ? void 0 : bookingSubject.form_id),
				title: locales.getText("pisell2.event.create.form"),
				utils,
				callback: function() {
					var _ref = _asyncToGenerator(function* (res) {
						if ((res === null || res === void 0 ? void 0 : res.status) === "success") {
							let result = void 0;
							if (!relationShipFormId) result = yield addAccount(res === null || res === void 0 ? void 0 : res.values, "holder");
							else result = yield handleAddHolder({
								res,
								bookingSubject,
								accountLists,
								cartItem: e,
								getHolders,
								appointmentBooking
							});
							if (result) appointmentBooking.updateCart({
								_id: e === null || e === void 0 ? void 0 : e._id,
								account: result
							});
						}
					});
					return function callback(_x) {
						return _ref.apply(this, arguments);
					};
				}()
			});
		}
	};
	const products = useMemo(() => {
		if (!(carts === null || carts === void 0 ? void 0 : carts.length)) return [];
		carts.forEach((item) => {
			item.isShowChangeHolder = !appointmentBooking.isTargetCartIdNormalProduct(item === null || item === void 0 ? void 0 : item._id);
		});
		if ((currentSelectedResources === null || currentSelectedResources === void 0 ? void 0 : currentSelectedResources.length) > 0) return carts.map((item) => {
			const currentResourceData = currentSelectedResources.find((resource) => (resource === null || resource === void 0 ? void 0 : resource._id) === (item === null || item === void 0 ? void 0 : item._id));
			const multiResources = getMultiResources(item, currentResourceData === null || currentResourceData === void 0 ? void 0 : currentResourceData.currentSelectedResource, currentStepParams === null || currentStepParams === void 0 ? void 0 : currentStepParams.key);
			return _objectSpread2(_objectSpread2({}, item), {}, {
				isMultipleResource: true,
				multiResources,
				isErrorHolder: isErrorHolder || false
			});
		});
		else return carts;
	}, [
		carts,
		currentSelectedResources,
		currentStepParams === null || currentStepParams === void 0 ? void 0 : currentStepParams.key,
		isErrorHolder
	]);
	/**
	* @description: 结算列表数据
	* @param {*} useMemo
	* @return {*}
	*/
	const subTotalList = useMemo(() => {
		const hideDeposit = (carts === null || carts === void 0 ? void 0 : carts.length) === 0 || !(deposit === null || deposit === void 0 ? void 0 : deposit.hasDeposit);
		const surcharge = ((summary === null || summary === void 0 ? void 0 : summary.surcharge) || []).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: hideDeposit
			},
			{
				key: "sub_total",
				value: summary === null || summary === void 0 ? void 0 : summary.subtotal
			},
			...surcharge,
			{
				key: "tax",
				value: totalTaxFee,
				hidden: Number(totalTaxFee) <= 0,
				tax: {
					rate: Number((summary === null || summary === void 0 ? void 0 : summary.taxRate) / 100),
					is_price_include_tax: isPriceIncludeTax,
					title: taxTitle
				}
			},
			{
				key: "custom_total",
				value: total,
				label: locales.getText("pisell2.event.total"),
				bold: !!hideDeposit
			}
		];
	}, [
		total,
		summary,
		carts === null || carts === void 0 ? void 0 : carts.length
	]);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", { className: `appointment-cart-item-wrap ${isMobile ? "appointment-cart-mobile-item-wrap" : ""}` }, /* @__PURE__ */ React.createElement("div", { className: "content" }, renderContactInfo ? renderContactInfo() : null, /* @__PURE__ */ React.createElement(shoppingCart, {
		productParams: {
			products,
			hideAddBtn: false,
			holders,
			disabledEdit: isLastStep,
			isShowChangeResource: currentStep === null || currentStep === void 0 || (_currentStep$step = currentStep.step) === null || _currentStep$step === void 0 || (_currentStep$step = _currentStep$step.key) === null || _currentStep$step === void 0 ? void 0 : _currentStep$step.startsWith("select-resource"),
			isShowDelete: (currentStep === null || currentStep === void 0 || (_currentStep$step2 = currentStep.step) === null || _currentStep$step2 === void 0 ? void 0 : _currentStep$step2.key) === "select-service",
			isShowAmountFooter: true,
			isShowPackageNote: false,
			isShowChangeHolder: isLastStep ? false : isFormSubject && isLogin,
			isShowHolder: isFormSubject,
			isShowAddHolderButton: true
		},
		collapseParams: {
			activeKey: holderIds,
			ghost: true
		},
		platform: "h5",
		emptyText: locales.getText("pisell2.event.empty.service"),
		isOpenGroup: !!(holderIds === null || holderIds === void 0 ? void 0 : holderIds.length),
		isHideClearCart: true,
		onChange: (e, type, key) => handleClickCard(e, type, key)
	})), /* @__PURE__ */ React.createElement("div", { className: "appointment-footer" }, hasGoodPassApp || hasDiscountCardApp ? /* @__PURE__ */ React.createElement(VoucherCard, {
		currentUser,
		products: carts
	}) : null, /* @__PURE__ */ React.createElement(SubTotal, {
		list: subTotalList,
		style: { padding: 0 },
		children: !!((carts === null || carts === void 0 ? void 0 : carts.length) > 0 && (deposit === null || deposit === void 0 ? void 0 : deposit.hasDeposit)) ? /* @__PURE__ */ React.createElement(Deposit, { showTitle: false }) : null
	}), /* @__PURE__ */ React.createElement(ContinueButton, { onNext }))));
};
//#endregion
export { CartItem as default };
