import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { isNormalProductByDurationSchedule } from "../../../booking/info/service2/utils.js";
import { useContext as useContext$1 } from "../../hooks/useContext.js";
import useAddService from "../../hooks/useAddService.js";
import useClearCart from "../../hooks/useClearCart.js";
import PisellNumberSelector from "../../../../pro/pisellNumberSelector/index.js";
import React, { useMemo, useState } from "react";
import { formatAmount, locales } from "@pisell/utils";
import Decimal from "decimal.js";
import { Iconfont, ProductCard } from "@pisell/materials";
import { useMemoizedFn } from "ahooks";
//#region src/components/shoppingCart/components/Cart/Product.tsx
const Product = (props) => {
	const { id, lists, isAdd = true } = props;
	const { state, onAction, onChange } = useContext$1();
	const { disabledEdit, isShowPackageNote, isShowNote, hideAddBtn, platform, isShowChangeResource, isShowDelete, isShowAmountFooter, isShowChangeHolder, isShowHolder, isShowAddHolderButton, isShowEditProduct, productCardStyleConfig = {} } = state;
	const clearCart = useClearCart(id);
	const [isEditExpanded, setIsEditExpanded] = useState(true);
	/**
	* @title: 更新商品数据
	* @description:
	* @param {any} item
	* @return {*}
	* @Author: WangHan
	* @Date: 2024-11-28 22:02
	*/
	const handleChange = useMemoizedFn((item, type, other) => {
		if (disabledEdit && !(other === null || other === void 0 ? void 0 : other.disabled)) return;
		onChange(item, type, other);
	});
	const toggleEditExpanded = useMemoizedFn((e) => {
		e === null || e === void 0 || e.preventDefault();
		setIsEditExpanded(!isEditExpanded);
	});
	const productParams = useMemo(() => {
		return {
			isShowNote,
			isShowPackageNote,
			disabledEdit,
			isShowChangeResource,
			isShowChangeHolder,
			isShowDelete,
			isShowHolder,
			isShowAddHolderButton,
			isShowEditProduct,
			isShowAmountFooter
		};
	}, [
		isShowNote,
		isShowPackageNote,
		disabledEdit,
		isShowChangeResource,
		isShowDelete,
		isShowHolder,
		isShowAddHolderButton,
		isShowChangeHolder,
		isShowEditProduct,
		isShowAmountFooter
	]);
	const getProductCardStyleProps = useMemoizedFn((type) => {
		const conf = productCardStyleConfig[`${type}_product_card_style`];
		if (state.scenario !== "booking2") return {};
		return {
			isShowImage: conf === null || conf === void 0 ? void 0 : conf.show_product_image,
			isShowDelete: (conf === null || conf === void 0 ? void 0 : conf.show_remove_button) && isShowDelete,
			isShowAmountFooter,
			disabledEdit: true
		};
	});
	const editItems = useMemo(() => (lists || []).filter((item) => {
		var _item$_origin;
		return item === null || item === void 0 || (_item$_origin = item._origin) === null || _item$_origin === void 0 ? void 0 : _item$_origin.edit;
	}), [lists]);
	const normalItems = useMemo(() => (lists || []).filter((item) => {
		var _item$_origin2;
		return !(item === null || item === void 0 || (_item$_origin2 = item._origin) === null || _item$_origin2 === void 0 ? void 0 : _item$_origin2.edit);
	}), [lists]);
	const editTotal = useMemo(() => {
		return formatAmount(editItems.reduce((total, item) => {
			const itemTotal = new Decimal((item === null || item === void 0 ? void 0 : item.total) || 0).mul(item.num);
			return total.add(itemTotal);
		}, new Decimal(0)).toNumber(), 2, (state === null || state === void 0 ? void 0 : state.amountSymbol) || "$");
	}, [editItems]);
	const renderProductItem = useMemoizedFn((item) => {
		let Comp = ProductCard;
		let isRetail;
		if (state.scenario === "booking2") {
			isRetail = isNormalProductByDurationSchedule(item === null || item === void 0 ? void 0 : item._origin);
			Comp = ProductCard.V2;
		}
		return /* @__PURE__ */ React.createElement(Comp, _objectSpread2(_objectSpread2(_objectSpread2({
			type: isRetail ? "a2" : "a5",
			rightActions: [{
				key: "update_number",
				text: /* @__PURE__ */ React.createElement(PisellNumberSelector, {
					enablePriceAdjust: true,
					min: 1,
					className: "pisell-sku-card__pisell-number-setter",
					value: item === null || item === void 0 ? void 0 : item.num,
					onChange: (value) => {
						if (value < 1) handleChange(item, "num", { num: 1 });
						else handleChange(item, "num", { num: value });
					}
				}),
				color: "#667085"
			}],
			escapeDom: ".pisell-number-selector-popover",
			key: item._id,
			dataSource: item,
			onAction: (e) => onAction === null || onAction === void 0 ? void 0 : onAction(e),
			onDelete: (e) => handleChange(e, "delete", item),
			onNote: (e) => handleChange(e, "note", { note: e.note }),
			onLike: (e) => handleChange(e, "like", { like_status: e.like_status }),
			onEditProduct: (e) => handleChange === null || handleChange === void 0 ? void 0 : handleChange(e, "edit_product"),
			onCard: (e) => handleChange === null || handleChange === void 0 ? void 0 : handleChange(e, "view"),
			onChangeResource: (e) => handleChange(item, "change_resource", e),
			onChangeHolder: (e) => handleChange(item, "change_holder", e),
			onAddHolder: (e) => handleChange(item, "add_holder", e)
		}, productParams), getProductCardStyleProps(isRetail ? "retail" : "appointment")), {}, { scenario: state.scenario }));
	});
	return useMemo(() => {
		console.log("render cart", lists);
		return /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__shopping-cart-content" }, clearCart, editItems.length > 0 && /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__shopping-cart-edit" }, /* @__PURE__ */ React.createElement("div", _objectSpread2(_objectSpread2({ className: "pisell-lowcode__shopping-cart-edit-header" }, { onClick: toggleEditExpanded }), {}, { style: { cursor: "pointer" } }), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__shopping-cart-edit-header-title" }, locales.getText("pisell2.text.shopping-cart.save.items")(editItems.length)), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__shopping-cart-edit-header-summary" }, editTotal), /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__shopping-cart-edit-header-switch" }, /* @__PURE__ */ React.createElement(Iconfont, {
			type: "pisell2-chevron-down",
			style: {
				transform: isEditExpanded ? "rotate(180deg)" : "rotate(0deg)",
				transition: "transform 0.3s ease"
			}
		}))), /* @__PURE__ */ React.createElement("div", {
			className: "pisell-lowcode__shopping-cart-edit-content",
			style: {
				maxHeight: isEditExpanded ? "unset" : "0",
				overflow: "hidden",
				transition: "max-height 0.3s ease"
			}
		}, editItems.map(renderProductItem))), normalItems.map(renderProductItem), !hideAddBtn && platform !== "h5" && isAdd ? useAddService(id, "more") : null);
	}, [
		clearCart,
		editItems,
		normalItems,
		productParams,
		hideAddBtn,
		isAdd,
		platform,
		isEditExpanded
	]);
};
//#endregion
export { Product as default };
