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 { formatProductsByVariant, getPureSourceProductId } from "../../utils.js";
import locales_default from "../../locales.js";
import { useContext as useContext$1 } from "../../hooks/useContext.js";
import Empty$1 from "../Empty/index.js";
import useTranslationOriginal from "../../../../hooks/useTranslationOriginal.js";
import { usePromotionModal } from "../../hooks/usePromotionModal.js";
import Product from "./Product.js";
import React, { useMemo } from "react";
import { locales } from "@pisell/utils";
import { Button, Collapse, PisellAlert } from "@pisell/materials";
import classNames from "classnames";
import { useMemoizedFn } from "ahooks";
import IconFont from "@pisell/materials/src/components/iconfont";
import "./index.less";
//#region src/components/shoppingCart/components/Cart/index.tsx
locales.init(locales_default, localStorage.getItem("umi_locale") || "en");
const prefixCls = "pisell-lowcode__shopping-cart";
const Cart = () => {
	const { state, updateState, onActiveKey, onChange, externalState } = useContext$1();
	const { platform, carts, isProductList, collapseParams, cartInfo } = state;
	const translationOriginal = useTranslationOriginal();
	/**
	* @title: 使用营销弹窗 hook
	* @description: 管理营销弹窗的状态和逻辑
	* @Author: WangHan
	* @Date: 2025-01-26
	*/
	const { openModal, MarketingModal } = usePromotionModal({ onConfirm: function() {
		var _ref = _asyncToGenerator(function* (selectedProduct) {
			const { selectedProducts, currentItem } = selectedProduct || {};
			const { sourceProductId, isGift } = currentItem || {};
			const key = isGift ? "update_gifts" : "update_products";
			onChange({
				selectedProducts,
				sourceProductId: getPureSourceProductId(sourceProductId)
			}, key);
		});
		return function onConfirm(_x) {
			return _ref.apply(this, arguments);
		};
	}() });
	/**
	* @description: 是否显示营销提示
	* @param {*} useMemo
	* @return {*}
	*/
	const _isShowPromotionAlert = 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 = 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 _objectSpread2(_objectSpread2({}, item), {}, {
				label: /* @__PURE__ */ React.createElement("div", { className: "pisell-lowcode__shopping-cart-label" }, `${item === null || item === void 0 ? void 0 : item.label} ${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.createElement(Product, {
					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.createElement(Empty$1, { id: item === null || item === void 0 ? void 0 : item.key }),
				showArrow: platform === "shop"
			});
		});
	}, [
		carts,
		platform,
		openModal,
		state.maxLength,
		_isShowPromotionAlert
	]);
	const handleOpenPromotionModal = 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 = _objectSpread2(_objectSpread2({}, (_extend === null || _extend === void 0 ? void 0 : _extend._promotion) || {}), {}, { sourceProductId: getPureSourceProductId(item === null || item === void 0 ? void 0 : item.sourceProductId) });
		} else currentItem = item;
		_products = 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 = 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(_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 = useMemo(() => {
		const { promotions } = cartInfo || {};
		if (!_isShowPromotionAlert) return null;
		return /* @__PURE__ */ React.createElement("div", { className: `${prefixCls}-promotion-alert` }, /* @__PURE__ */ React.createElement("div", null, promotions === null || promotions === void 0 ? void 0 : promotions.map((item) => /* @__PURE__ */ React.createElement(PisellAlert, {
			description: 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.createElement(Button, { onClick: () => handleOpenPromotionModal(item, "promotion") }, /* @__PURE__ */ React.createElement("span", null, locales.getText("pisell2.text.shopping-cart.marketing.browse.items")), /* @__PURE__ */ React.createElement(IconFont, { type: "pisell2-arrow-right" }))
		}))));
	}, [JSON.stringify(cartInfo), _isShowPromotionAlert]);
	return useMemo(() => {
		console.log("render cart 22222", state.prodcuts);
		return /* @__PURE__ */ React.createElement("div", { className: classNames("pisell-lowcode__shopping-cart") }, isProductList ? /* @__PURE__ */ React.createElement(Product, {
			id: void 0,
			lists: state === null || state === void 0 ? void 0 : state.products,
			openPromotionModal: handleOpenPromotionModal,
			isShowPromotionAlert: _isShowPromotionAlert
		}) : /* @__PURE__ */ React.createElement(Collapse, _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
export { Cart as default };
