const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_utils = require("../../utils.js");
const require_locales = require("../../locales.js");
const require_useContext = require("../../hooks/useContext.js");
const require_index = require("../Empty/index.js");
const require_useTranslationOriginal = require("../../../../hooks/useTranslationOriginal.js");
const require_usePromotionModal = require("../../hooks/usePromotionModal.js");
const require_Product = require("./Product.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let ahooks = require("ahooks");
let _pisell_materials_src_components_iconfont = require("@pisell/materials/src/components/iconfont");
_pisell_materials_src_components_iconfont = require_runtime.__toESM(_pisell_materials_src_components_iconfont);
require("./index.less");
//#region src/components/shoppingCart/components/Cart/index.tsx
_pisell_utils.locales.init(require_locales.default, localStorage.getItem("umi_locale") || "en");
const prefixCls = "pisell-lowcode__shopping-cart";
const Cart = () => {
	const { state, updateState, onActiveKey, onChange, externalState } = require_useContext.useContext();
	const { platform, carts, isProductList, collapseParams, cartInfo } = state;
	const translationOriginal = require_useTranslationOriginal.default();
	/**
	* @title: 使用营销弹窗 hook
	* @description: 管理营销弹窗的状态和逻辑
	* @Author: WangHan
	* @Date: 2025-01-26
	*/
	const { openModal, MarketingModal } = require_usePromotionModal.usePromotionModal({ onConfirm: function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (selectedProduct) {
			const { selectedProducts, currentItem } = selectedProduct || {};
			const { sourceProductId, isGift } = currentItem || {};
			const key = isGift ? "update_gifts" : "update_products";
			onChange({
				selectedProducts,
				sourceProductId: require_utils.getPureSourceProductId(sourceProductId)
			}, key);
		});
		return function onConfirm(_x) {
			return _ref.apply(this, arguments);
		};
	}() });
	/**
	* @description: 是否显示营销提示
	* @param {*} useMemo
	* @return {*}
	*/
	const _isShowPromotionAlert = (0, react.useMemo)(() => {
		const { promotions, isShowPromotionAlert = true } = cartInfo || {};
		if (!isShowPromotionAlert || !(promotions === null || promotions === void 0 ? void 0 : promotions.length)) return false;
		const _promotions = (promotions || []).filter((item) => (item === null || item === void 0 ? void 0 : item.actionType) === "X_ITEMS_FOR_Y_PRICE");
		if (!(_promotions === null || _promotions === void 0 ? void 0 : _promotions.length)) return false;
		return true;
	}, [JSON.stringify(cartInfo)]);
	/**
	* @title: 折叠面板
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: WangHan
	* @Date: 2024-11-27 15:01
	*/
	const cartsMap = (0, react.useMemo)(() => {
		return carts === null || carts === void 0 ? void 0 : carts.map((item) => {
			var _item$products, _item$products2, _item$products3;
			const _length = item === null || item === void 0 || (_item$products = item.products) === null || _item$products === void 0 ? void 0 : _item$products.length;
			return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, {
				label: /* @__PURE__ */ react.default.createElement("div", { className: "pisell-lowcode__shopping-cart-label" }, `${item === null || item === void 0 ? void 0 : item.label} ${_pisell_utils.locales.getText("pisell2.text.shopping-cart.item")(item === null || item === void 0 || (_item$products2 = item.products) === null || _item$products2 === void 0 ? void 0 : _item$products2.length)}`),
				children: (item === null || item === void 0 || (_item$products3 = item.products) === null || _item$products3 === void 0 ? void 0 : _item$products3.length) > 0 ? /* @__PURE__ */ react.default.createElement(require_Product.default, {
					id: item === null || item === void 0 ? void 0 : item.key,
					lists: item === null || item === void 0 ? void 0 : item.products,
					isAdd: state.maxLength > _length,
					openPromotionModal: openModal,
					isShowPromotionAlert: _isShowPromotionAlert
				}) : /* @__PURE__ */ react.default.createElement(require_index.default, { id: item === null || item === void 0 ? void 0 : item.key }),
				showArrow: platform === "shop"
			});
		});
	}, [
		carts,
		platform,
		openModal,
		state.maxLength,
		_isShowPromotionAlert
	]);
	const handleOpenPromotionModal = (0, ahooks.useMemoizedFn)((item, type = "gift") => {
		var _item$giftData, _currentItem$giftInfo, _currentItem$giftInfo2, _item$giftData2;
		const isGift = type === "gift";
		const isEdit = (item === null || item === void 0 || (_item$giftData = item.giftData) === null || _item$giftData === void 0 || (_item$giftData = _item$giftData.selectedGifts) === null || _item$giftData === void 0 ? void 0 : _item$giftData.length) > 0;
		let _products;
		let currentItem;
		if (isGift) {
			var _item$_origin;
			const _extend = (item === null || item === void 0 || (_item$_origin = item._origin) === null || _item$_origin === void 0 ? void 0 : _item$_origin._extend) || {};
			currentItem = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (_extend === null || _extend === void 0 ? void 0 : _extend._promotion) || {}), {}, { sourceProductId: require_utils.getPureSourceProductId(item === null || item === void 0 ? void 0 : item.sourceProductId) });
		} else currentItem = item;
		_products = require_utils.formatProductsByVariant((currentItem === null || currentItem === void 0 || (_currentItem$giftInfo = currentItem.giftInfo) === null || _currentItem$giftInfo === void 0 ? void 0 : _currentItem$giftInfo.giftOptions) || (currentItem === null || currentItem === void 0 ? void 0 : currentItem.eligibleProducts) || []);
		const isMultiple = (currentItem === null || currentItem === void 0 || (_currentItem$giftInfo2 = currentItem.giftInfo) === null || _currentItem$giftInfo2 === void 0 ? void 0 : _currentItem$giftInfo2.giftCount) > 1 || type === "promotion";
		let selected;
		let selectedProducts;
		if (isGift) {
			var _externalState$servic;
			const services = (externalState === null || externalState === void 0 || (_externalState$servic = externalState.service) === null || _externalState$servic === void 0 ? void 0 : _externalState$servic.value) || [];
			selectedProducts = services === null || services === void 0 ? void 0 : services.filter((option) => {
				var _option$_giftInfo, _currentItem$sourcePr;
				const _id = require_utils.getPureSourceProductId(option === null || option === void 0 || (_option$_giftInfo = option._giftInfo) === null || _option$_giftInfo === void 0 ? void 0 : _option$_giftInfo.sourceProductId);
				return _id && (currentItem === null || currentItem === void 0 || (_currentItem$sourcePr = currentItem.sourceProductId) === null || _currentItem$sourcePr === void 0 ? void 0 : _currentItem$sourcePr.includes(_id));
			});
			if (isMultiple) {
				var _ref2;
				selected = (_ref2 = selectedProducts || []) === null || _ref2 === void 0 ? void 0 : _ref2.map((option) => {
					var _option$_extend;
					return {
						value: option === null || option === void 0 ? void 0 : option.id,
						quantity: (option === null || option === void 0 || (_option$_extend = option._extend) === null || _option$_extend === void 0 ? void 0 : _option$_extend.quantity) || 1
					};
				});
			} else {
				var _selectedProducts$;
				selected = (selectedProducts === null || selectedProducts === void 0 || (_selectedProducts$ = selectedProducts[0]) === null || _selectedProducts$ === void 0 ? void 0 : _selectedProducts$.id) || "";
			}
		}
		openModal(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, currentItem), {}, {
			promotionInfo: currentItem || {},
			products: _products,
			isMultiple,
			isGift,
			selected,
			selectedProducts,
			isEdit,
			_selectedOriginGifts: item === null || item === void 0 || (_item$giftData2 = item.giftData) === null || _item$giftData2 === void 0 ? void 0 : _item$giftData2._selectedOriginGifts,
			state: externalState,
			translationOriginal
		}));
	});
	/**
	* @description: 营销提示内容
	* @param {*} useMemo
	* @return {*}
	*/
	const promotionAlert = (0, react.useMemo)(() => {
		const { promotions } = cartInfo || {};
		if (!_isShowPromotionAlert) return null;
		return /* @__PURE__ */ react.default.createElement("div", { className: `${prefixCls}-promotion-alert` }, /* @__PURE__ */ react.default.createElement("div", null, promotions === null || promotions === void 0 ? void 0 : promotions.map((item) => /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellAlert, {
			description: _pisell_utils.locales.getText("pisell2.text.shopping-cart.marketing.alert")(item === null || item === void 0 ? void 0 : item.needQuantity, translationOriginal === null || translationOriginal === void 0 ? void 0 : translationOriginal(item === null || item === void 0 ? void 0 : item.strategyName)),
			type: "error",
			action: /* @__PURE__ */ react.default.createElement(_pisell_materials.Button, { onClick: () => handleOpenPromotionModal(item, "promotion") }, /* @__PURE__ */ react.default.createElement("span", null, _pisell_utils.locales.getText("pisell2.text.shopping-cart.marketing.browse.items")), /* @__PURE__ */ react.default.createElement(_pisell_materials_src_components_iconfont.default, { type: "pisell2-arrow-right" }))
		}))));
	}, [JSON.stringify(cartInfo), _isShowPromotionAlert]);
	return (0, react.useMemo)(() => {
		console.log("render cart 22222", state.prodcuts);
		return /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("pisell-lowcode__shopping-cart") }, isProductList ? /* @__PURE__ */ react.default.createElement(require_Product.default, {
			id: void 0,
			lists: state === null || state === void 0 ? void 0 : state.products,
			openPromotionModal: handleOpenPromotionModal,
			isShowPromotionAlert: _isShowPromotionAlert
		}) : /* @__PURE__ */ react.default.createElement(_pisell_materials.Collapse, require_objectSpread2._objectSpread2({
			items: cartsMap,
			showHeader: true,
			panelBackground: "#ffffff",
			collapsible: platform !== "shop" ? "disabled" : "-",
			accordion: platform === "shop",
			onChange: (key) => {
				updateState({ activeKey: key });
				onActiveKey(key);
			}
		}, collapseParams)), promotionAlert, MarketingModal);
	}, [
		isProductList,
		cartsMap,
		platform,
		collapseParams,
		state === null || state === void 0 ? void 0 : state.products,
		promotionAlert,
		MarketingModal,
		handleOpenPromotionModal,
		_isShowPromotionAlert
	]);
};
//#endregion
exports.default = Cart;
