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/Product.tsx
var Product_exports = {};
__export(Product_exports, {
  default: () => Product_default
});
module.exports = __toCommonJS(Product_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_useClearCart = __toESM(require("../../hooks/useClearCart"));
var import_useContext = require("../../hooks/useContext");
var import_useAddService = __toESM(require("../../hooks/useAddService"));
var import_ahooks = require("ahooks");
var import_utils = require("../../../booking/info/service2/utils");
var import_pisellNumberSelector = __toESM(require("../../../../pro/pisellNumberSelector"));
var import_utils2 = require("@pisell/utils");
var import_utils3 = require("@pisell/utils");
var import_decimal = __toESM(require("decimal.js"));
var Product = (props) => {
  const {
    id,
    lists,
    isAdd = true,
    openPromotionModal,
    isShowPromotionAlert = false
  } = props;
  const { state, onAction, onChange } = (0, import_useContext.useContext)();
  const {
    disabledEdit,
    isShowPackageNote,
    isShowNote,
    hideAddBtn,
    platform,
    isShowChangeResource,
    isShowDelete,
    isShowAmountFooter,
    isShowChangeHolder,
    isShowHolder,
    isShowAddHolderButton,
    isShowEditProduct,
    productCardStyleConfig = {},
    productsPerRow: rawProductsPerRow = 1
  } = state;
  const cartColumns = (0, import_react.useMemo)(() => {
    const n = Math.floor(Number(rawProductsPerRow));
    if (!Number.isFinite(n) || n < 1) return 1;
    return Math.min(n, 8);
  }, [rawProductsPerRow]);
  const clearCart = (0, import_useClearCart.default)(id);
  const [isEditExpanded, setIsEditExpanded] = (0, import_react.useState)(false);
  const handleChange = (0, import_ahooks.useMemoizedFn)(
    (item, type, other) => {
      if (disabledEdit && !(other == null ? void 0 : other.disabled)) return;
      onChange(item, type, other);
    }
  );
  const toggleEditExpanded = (0, import_ahooks.useMemoizedFn)((e) => {
    e == null ? void 0 : e.preventDefault();
    setIsEditExpanded(!isEditExpanded);
  });
  const productParams = (0, import_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, import_ahooks.useMemoizedFn)(
    (type) => {
      const conf = productCardStyleConfig[`${type}_product_card_style`];
      if (state.scenario !== "booking2") return {};
      return {
        isShowImage: conf == null ? void 0 : conf.show_product_image,
        isShowDelete: (conf == null ? void 0 : conf.show_remove_button) && isShowDelete,
        isShowAmountFooter,
        disabledEdit: true,
        maxSpecsCount: 1,
        itemType: type === "retail" ? "product" : "booking"
      };
    }
  );
  const editItems = (0, import_react.useMemo)(
    () => (lists || []).filter((item) => {
      var _a;
      return (_a = item == null ? void 0 : item._origin) == null ? void 0 : _a.edit;
    }),
    [lists]
  );
  const normalItems = (0, import_react.useMemo)(
    () => (lists || []).filter((item) => {
      var _a;
      return !((_a = item == null ? void 0 : item._origin) == null ? void 0 : _a.edit);
    }),
    [lists]
  );
  const handleGift = (0, import_ahooks.useMemoizedFn)((value, item) => {
    return openPromotionModal == null ? void 0 : openPromotionModal({ sourceProductId: item == null ? void 0 : item._id, ...value });
  });
  const editTotal = (0, import_react.useMemo)(() => {
    const total = editItems.reduce((total2, item) => {
      const itemTotal = new import_decimal.default((item == null ? void 0 : item.total) || 0).mul(item.num);
      return total2.add(itemTotal);
    }, new import_decimal.default(0));
    return (0, import_utils2.formatAmount)(total.toNumber(), 2, (state == null ? void 0 : state.amountSymbol) || "$");
  }, [editItems]);
  const renderProductItem = (0, import_ahooks.useMemoizedFn)((item) => {
    let Comp = import_materials.ProductCard;
    let isRetail;
    if (state.scenario === "booking2") {
      isRetail = (0, import_utils.isNormalProductByDurationSchedule)(item == null ? void 0 : item._origin);
      Comp = import_materials.ProductCard.V2;
    }
    const isHiddenRightActions = !!(item == null ? void 0 : item.isGift);
    return /* @__PURE__ */ import_react.default.createElement(
      Comp,
      {
        type: isRetail ? "a9" : "a5",
        rightActions: isHiddenRightActions ? [] : [
          {
            key: "update_number",
            text: /* @__PURE__ */ import_react.default.createElement(
              import_pisellNumberSelector.default,
              {
                enablePriceAdjust: true,
                min: 1,
                className: "pisell-sku-card__pisell-number-setter",
                value: item == null ? 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 ? 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 ? void 0 : handleChange(e, "edit_product"),
        onCard: (e) => handleChange == null ? 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,
        onGift: (e) => handleGift(e, item)
      }
    );
  });
  const productGridStyle = cartColumns > 1 ? {
    display: "grid",
    gridTemplateColumns: `repeat(${cartColumns}, minmax(0, 1fr))`,
    gap: 8
  } : void 0;
  return (0, import_react.useMemo)(() => {
    console.log("render cart", lists);
    return /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: "pisell-lowcode__shopping-cart-content",
        style: {
          paddingBottom: isShowPromotionAlert ? "66px" : 0
        }
      },
      clearCart,
      cartColumns > 1 ? /* @__PURE__ */ import_react.default.createElement("div", { style: productGridStyle }, normalItems.map(renderProductItem)) : normalItems.map(renderProductItem),
      editItems.length > 0 && /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__shopping-cart-edit" }, /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: "pisell-lowcode__shopping-cart-edit-header",
          ...{ onClick: toggleEditExpanded },
          style: { cursor: "pointer" }
        },
        /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__shopping-cart-edit-header-title" }, import_utils3.locales.getText("pisell2.text.shopping-cart.save.items")(editItems.length)),
        /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__shopping-cart-edit-header-summary" }, editTotal),
        /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__shopping-cart-edit-header-switch" }, /* @__PURE__ */ import_react.default.createElement(
          import_materials.Iconfont,
          {
            type: "pisell2-chevron-down",
            style: {
              transform: isEditExpanded ? "rotate(180deg)" : "rotate(0deg)",
              transition: "transform 0.3s ease"
            }
          }
        ))
      ), /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: "pisell-lowcode__shopping-cart-edit-content",
          style: {
            maxHeight: isEditExpanded ? "unset" : "0",
            overflow: "hidden",
            transition: "max-height 0.3s ease",
            ...cartColumns > 1 ? {
              display: "grid",
              gridTemplateColumns: `repeat(${cartColumns}, minmax(0, 1fr))`,
              gap: 8
            } : {}
          }
        },
        editItems.map(renderProductItem)
      )),
      !hideAddBtn && platform !== "h5" && isAdd ? (0, import_useAddService.default)(id, "more") : null
    );
  }, [
    clearCart,
    editItems,
    normalItems,
    productParams,
    hideAddBtn,
    isAdd,
    platform,
    isEditExpanded,
    isShowPromotionAlert,
    productGridStyle,
    cartColumns
  ]);
};
var Product_default = Product;
