var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/shoppingCart/components/Cart/index.tsx
var Cart_exports = {};
__export(Cart_exports, {
  default: () => Cart_default
});
module.exports = __toCommonJS(Cart_exports);
var import_materials = require("@pisell/materials");
var import_iconfont = __toESM(require("@pisell/materials/src/components/iconfont"));
var import_utils = require("@pisell/utils");
var import_ahooks = require("ahooks");
var import_classnames = __toESM(require("classnames"));
var import_react = __toESM(require("react"));
var import_useContext = require("../../hooks/useContext");
var import_usePromotionModal = require("../../hooks/usePromotionModal");
var import_useTranslationOriginal = __toESM(require("../../../../hooks/useTranslationOriginal"));
var import_locales = __toESM(require("../../locales"));
var import_utils2 = require("../../utils");
var import_Empty = __toESM(require("../Empty"));
var import_Product = __toESM(require("./Product"));
var import_index = require("./index.less");
import_utils.locales.init(import_locales.default, localStorage.getItem("umi_locale") || "en");
var prefixCls = "pisell-lowcode__shopping-cart";
var Cart = () => {
  const { state, updateState, onActiveKey, onChange, externalState } = (0, import_useContext.useContext)();
  const { platform, carts, isProductList, collapseParams, cartInfo } = state;
  const translationOriginal = (0, import_useTranslationOriginal.default)();
  const { openModal, MarketingModal } = (0, import_usePromotionModal.usePromotionModal)({
    onConfirm: async (selectedProduct) => {
      const { selectedProducts, currentItem } = selectedProduct || {};
      const { sourceProductId, isGift } = currentItem || {};
      const key = isGift ? "update_gifts" : "update_products";
      const _pureSourceProductId = (0, import_utils2.getPureSourceProductId)(sourceProductId);
      const params = {
        selectedProducts,
        sourceProductId: _pureSourceProductId
      };
      onChange(params, key);
    }
  });
  const _isShowPromotionAlert = (0, import_react.useMemo)(() => {
    const { promotions, isShowPromotionAlert = true } = cartInfo || {};
    if (!isShowPromotionAlert || !(promotions == null ? void 0 : promotions.length)) return false;
    const _promotions = (promotions || []).filter(
      (item) => (item == null ? void 0 : item.actionType) === "X_ITEMS_FOR_Y_PRICE"
    );
    if (!(_promotions == null ? void 0 : _promotions.length)) return false;
    return true;
  }, [JSON.stringify(cartInfo)]);
  const cartsMap = (0, import_react.useMemo)(() => {
    return carts == null ? void 0 : carts.map((item) => {
      var _a, _b, _c;
      const _length = (_a = item == null ? void 0 : item.products) == null ? void 0 : _a.length;
      return {
        ...item,
        label: /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__shopping-cart-label" }, `${item == null ? void 0 : item.label} ${import_utils.locales.getText(
          "pisell2.text.shopping-cart.item"
        )((_b = item == null ? void 0 : item.products) == null ? void 0 : _b.length)}`),
        children: ((_c = item == null ? void 0 : item.products) == null ? void 0 : _c.length) > 0 ? /* @__PURE__ */ import_react.default.createElement(
          import_Product.default,
          {
            id: item == null ? void 0 : item.key,
            lists: item == null ? void 0 : item.products,
            isAdd: state.maxLength > _length,
            openPromotionModal: openModal,
            isShowPromotionAlert: _isShowPromotionAlert
          }
        ) : /* @__PURE__ */ import_react.default.createElement(import_Empty.default, { id: item == null ? void 0 : item.key }),
        showArrow: platform === "shop"
      };
    });
  }, [carts, platform, openModal, state.maxLength, _isShowPromotionAlert]);
  const handleOpenPromotionModal = (0, import_ahooks.useMemoizedFn)(
    (item, type = "gift") => {
      var _a, _b, _c, _d, _e, _f, _g, _h, _i;
      const isGift = type === "gift";
      const isEdit = ((_b = (_a = item == null ? void 0 : item.giftData) == null ? void 0 : _a.selectedGifts) == null ? void 0 : _b.length) > 0;
      let _products;
      let currentItem;
      if (isGift) {
        const _extend = ((_c = item == null ? void 0 : item._origin) == null ? void 0 : _c._extend) || {};
        currentItem = {
          ...(_extend == null ? void 0 : _extend._promotion) || {},
          sourceProductId: (0, import_utils2.getPureSourceProductId)(item == null ? void 0 : item.sourceProductId)
        };
      } else {
        currentItem = item;
      }
      const rawProducts = ((_d = currentItem == null ? void 0 : currentItem.giftInfo) == null ? void 0 : _d.giftOptions) || (currentItem == null ? void 0 : currentItem.eligibleProducts) || [];
      _products = (0, import_utils2.formatProductsByVariant)(rawProducts);
      const isMultiple = ((_e = currentItem == null ? void 0 : currentItem.giftInfo) == null ? void 0 : _e.giftCount) > 1 || type === "promotion";
      let selected;
      let selectedProducts;
      if (isGift) {
        const services = ((_f = externalState == null ? void 0 : externalState.service) == null ? void 0 : _f.value) || [];
        selectedProducts = services == null ? void 0 : services.filter((option) => {
          var _a2, _b2;
          const _id = (0, import_utils2.getPureSourceProductId)(
            (_a2 = option == null ? void 0 : option._giftInfo) == null ? void 0 : _a2.sourceProductId
          );
          return _id && ((_b2 = currentItem == null ? void 0 : currentItem.sourceProductId) == null ? void 0 : _b2.includes(_id));
        });
        if (isMultiple) {
          selected = (_g = selectedProducts || []) == null ? void 0 : _g.map((option) => {
            var _a2;
            return {
              value: option == null ? void 0 : option.id,
              quantity: ((_a2 = option == null ? void 0 : option._extend) == null ? void 0 : _a2.quantity) || 1
            };
          });
        } else {
          selected = ((_h = selectedProducts == null ? void 0 : selectedProducts[0]) == null ? void 0 : _h.id) || "";
        }
      }
      const _item = {
        ...currentItem,
        promotionInfo: currentItem || {},
        products: _products,
        isMultiple,
        isGift,
        selected,
        selectedProducts,
        isEdit,
        _selectedOriginGifts: (_i = item == null ? void 0 : item.giftData) == null ? void 0 : _i._selectedOriginGifts,
        state: externalState,
        translationOriginal
      };
      openModal(_item);
    }
  );
  const promotionAlert = (0, import_react.useMemo)(() => {
    const { promotions } = cartInfo || {};
    if (!_isShowPromotionAlert) return null;
    return /* @__PURE__ */ import_react.default.createElement("div", { className: `${prefixCls}-promotion-alert` }, /* @__PURE__ */ import_react.default.createElement("div", null, promotions == null ? void 0 : promotions.map((item) => /* @__PURE__ */ import_react.default.createElement(
      import_materials.PisellAlert,
      {
        description: import_utils.locales.getText(
          "pisell2.text.shopping-cart.marketing.alert"
        )(item == null ? void 0 : item.needQuantity, translationOriginal == null ? void 0 : translationOriginal(item == null ? void 0 : item.strategyName)),
        type: "error",
        action: /* @__PURE__ */ import_react.default.createElement(
          import_materials.Button,
          {
            onClick: () => handleOpenPromotionModal(item, "promotion")
          },
          /* @__PURE__ */ import_react.default.createElement("span", null, import_utils.locales.getText(
            "pisell2.text.shopping-cart.marketing.browse.items"
          )),
          /* @__PURE__ */ import_react.default.createElement(import_iconfont.default, { type: "pisell2-arrow-right" })
        )
      }
    ))));
  }, [JSON.stringify(cartInfo), _isShowPromotionAlert]);
  return (0, import_react.useMemo)(() => {
    console.log("render cart 22222", state.prodcuts);
    return /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("pisell-lowcode__shopping-cart") }, isProductList ? /* @__PURE__ */ import_react.default.createElement(
      import_Product.default,
      {
        id: void 0,
        lists: state == null ? void 0 : state.products,
        openPromotionModal: handleOpenPromotionModal,
        isShowPromotionAlert: _isShowPromotionAlert
      }
    ) : /* @__PURE__ */ import_react.default.createElement(
      import_materials.Collapse,
      {
        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 ? void 0 : state.products,
    promotionAlert,
    MarketingModal,
    handleOpenPromotionModal,
    _isShowPromotionAlert
  ]);
};
var Cart_default = Cart;
