const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_utils = require("../../../booking/info/service2/utils.js");
const require_useContext = require("../../hooks/useContext.js");
const require_useAddService = require("../../hooks/useAddService.js");
const require_useClearCart = require("../../hooks/useClearCart.js");
const require_index = require("../../../../pro/pisellNumberSelector/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
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 ahooks = require("ahooks");
//#region src/components/shoppingCart/components/Cart/Product.tsx
const Product = (props) => {
	const { id, lists, isAdd = true } = props;
	const { state, onAction, onChange } = require_useContext.useContext();
	const { disabledEdit, isShowPackageNote, isShowNote, hideAddBtn, platform, isShowChangeResource, isShowDelete, isShowAmountFooter, isShowChangeHolder, isShowHolder, isShowAddHolderButton, isShowEditProduct, productCardStyleConfig = {} } = state;
	const clearCart = require_useClearCart.default(id);
	const [isEditExpanded, setIsEditExpanded] = (0, react.useState)(true);
	/**
	* @title: 更新商品数据
	* @description:
	* @param {any} item
	* @return {*}
	* @Author: WangHan
	* @Date: 2024-11-28 22:02
	*/
	const handleChange = (0, ahooks.useMemoizedFn)((item, type, other) => {
		if (disabledEdit && !(other === null || other === void 0 ? void 0 : other.disabled)) return;
		onChange(item, type, other);
	});
	const toggleEditExpanded = (0, ahooks.useMemoizedFn)((e) => {
		e === null || e === void 0 || e.preventDefault();
		setIsEditExpanded(!isEditExpanded);
	});
	const productParams = (0, react.useMemo)(() => {
		return {
			isShowNote,
			isShowPackageNote,
			disabledEdit,
			isShowChangeResource,
			isShowChangeHolder,
			isShowDelete,
			isShowHolder,
			isShowAddHolderButton,
			isShowEditProduct,
			isShowAmountFooter
		};
	}, [
		isShowNote,
		isShowPackageNote,
		disabledEdit,
		isShowChangeResource,
		isShowDelete,
		isShowHolder,
		isShowAddHolderButton,
		isShowChangeHolder,
		isShowEditProduct,
		isShowAmountFooter
	]);
	const getProductCardStyleProps = (0, ahooks.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 = (0, react.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 = (0, react.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 = (0, react.useMemo)(() => {
		return (0, _pisell_utils.formatAmount)(editItems.reduce((total, item) => {
			const itemTotal = new decimal_js.default((item === null || item === void 0 ? void 0 : item.total) || 0).mul(item.num);
			return total.add(itemTotal);
		}, new decimal_js.default(0)).toNumber(), 2, (state === null || state === void 0 ? void 0 : state.amountSymbol) || "$");
	}, [editItems]);
	const renderProductItem = (0, ahooks.useMemoizedFn)((item) => {
		let Comp = _pisell_materials.ProductCard;
		let isRetail;
		if (state.scenario === "booking2") {
			isRetail = require_utils.isNormalProductByDurationSchedule(item === null || item === void 0 ? void 0 : item._origin);
			Comp = _pisell_materials.ProductCard.V2;
		}
		return /* @__PURE__ */ react.default.createElement(Comp, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({
			type: isRetail ? "a2" : "a5",
			rightActions: [{
				key: "update_number",
				text: /* @__PURE__ */ react.default.createElement(require_index.default, {
					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 (0, react.useMemo)(() => {
		console.log("render cart", lists);
		return /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__shopping-cart-content" }, clearCart, editItems.length > 0 && /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__shopping-cart-edit" }, /* @__PURE__ */ react.default.createElement("div", require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({ className: "pisell-lowcode__shopping-cart-edit-header" }, { onClick: toggleEditExpanded }), {}, { style: { cursor: "pointer" } }), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__shopping-cart-edit-header-title" }, _pisell_utils.locales.getText("pisell2.text.shopping-cart.save.items")(editItems.length)), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__shopping-cart-edit-header-summary" }, editTotal), /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__shopping-cart-edit-header-switch" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
			type: "pisell2-chevron-down",
			style: {
				transform: isEditExpanded ? "rotate(180deg)" : "rotate(0deg)",
				transition: "transform 0.3s ease"
			}
		}))), /* @__PURE__ */ react.default.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 ? require_useAddService.default(id, "more") : null);
	}, [
		clearCart,
		editItems,
		normalItems,
		productParams,
		hideAddBtn,
		isAdd,
		platform,
		isEditExpanded
	]);
};
//#endregion
exports.default = Product;
