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/plus/skuOptionsSelection/components/OptionsCard/OptionItemCard.tsx
var OptionItemCard_exports = {};
__export(OptionItemCard_exports, {
  default: () => OptionItemCard_default
});
module.exports = __toCommonJS(OptionItemCard_exports);
var import_react = __toESM(require("react"));
var import_decimal = __toESM(require("decimal.js"));
var import_utils = require("@pisell/utils");
var import_materials = require("@pisell/materials");
var import_NumericStepper = __toESM(require("../../../../pro/NumericStepper"));
var import_hooks = require("../../hooks");
var import_index = require("./index.less");
var OptionItemCard = (props) => {
  const { data, dataSource, remainingCount } = props;
  const symbol = (0, import_hooks.useStore)((s) => s.symbol);
  const renderItem = (data_2_item) => {
    if (!Array.isArray(data_2_item) && !Object.keys(data_2_item || {}).length) {
      return null;
    }
    if (Array.isArray(data_2_item)) {
      return data_2_item.map((_item) => {
        const data_3 = (_item == null ? void 0 : _item.data) || {};
        return /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            key: _item == null ? void 0 : _item.id,
            className: "pisell-plus-sku-options-card-option-item-card-item-content-text"
          },
          `${(data_3 == null ? void 0 : data_3.quantity) || 1} ×`,
          " ",
          (data_3 == null ? void 0 : data_3.title) || (data_3 == null ? void 0 : data_3.name) || ""
        );
      });
    } else {
      const data_3 = (data_2_item == null ? void 0 : data_2_item.data) || {};
      return /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          key: data_3 == null ? void 0 : data_3.id,
          className: "pisell-plus-sku-options-card-option-item-card-item-content-text"
        },
        `${(data_3 == null ? void 0 : data_3.quantity) || 1} ×`,
        " ",
        (data_3 == null ? void 0 : data_3.title) || (data_3 == null ? void 0 : data_3.name) || ""
      );
    }
  };
  const calculateTotalPrice = (data_2_item) => {
    let totalPrice = 0;
    if (Array.isArray(data_2_item)) {
      totalPrice = data_2_item.reduce((acc, _item) => {
        return acc.plus(new import_decimal.default((_item == null ? void 0 : _item.totalPrice) || 0));
      }, new import_decimal.default(0)).toNumber();
    } else if (Object.keys(data_2_item || {}).length) {
      totalPrice = new import_decimal.default((data_2_item == null ? void 0 : data_2_item.totalPrice) || 0).toNumber();
    }
    return `${totalPrice >= 0 ? "+" : "-"}${symbol}${Math.abs(totalPrice)}`;
  };
  const getGroupName = (key) => {
    var _a, _b;
    const groupItem = (_b = (_a = dataSource == null ? void 0 : dataSource._original) == null ? void 0 : _a.option_group) == null ? void 0 : _b.find(
      (item) => (item == null ? void 0 : item.id) == key
    );
    return (groupItem == null ? void 0 : groupItem.title) || (groupItem == null ? void 0 : groupItem.name) || "";
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-plus-sku-options-card-option-item-card" }, data == null ? void 0 : data.map((item, index) => {
    const data_2 = (item == null ? void 0 : item.data) || {};
    if (!data_2 || Object.keys(data_2 || {}).length === 0) {
      return null;
    }
    return /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: "pisell-plus-sku-options-card-option-item-card-item",
        key: `${item == null ? void 0 : item.value}-${index}`
      },
      /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-plus-sku-options-card-option-item-card-item-float" }, import_utils.locales.getText(
        "pisell2.skuOptionsSelection.optionItemCard.typeText"
      ), " ", index + 1),
      /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-plus-sku-options-card-option-item-card-item-section" }, Object.keys(data_2).map((key) => {
        const data_2_item = (data_2 == null ? void 0 : data_2[key]) || {};
        return /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            className: "pisell-plus-sku-options-card-option-item-card-item-content",
            key: `${item == null ? void 0 : item.value}-${index}-${key}`
          },
          /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-plus-sku-options-card-option-item-card-item-content-top" }, /* @__PURE__ */ import_react.default.createElement("span", { className: "pisell-plus-sku-options-card-option-item-card-item-content-title" }, getGroupName(key)), /* @__PURE__ */ import_react.default.createElement("span", { className: "pisell-plus-sku-options-card-option-item-card-item-content-price" }, calculateTotalPrice(data_2_item))),
          renderItem(data_2_item)
        );
      }), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-plus-sku-options-card-option-item-card-item-section-divider" })),
      /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-plus-sku-options-card-option-item-card-item-footer" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-plus-sku-options-card-option-item-card-item-footer-stepper" }, /* @__PURE__ */ import_react.default.createElement(
        import_NumericStepper.default,
        {
          min: 0,
          max: ((item == null ? void 0 : item.quantity) || 1) + remainingCount,
          value: (item == null ? void 0 : item.quantity) || 1,
          onChange: (value) => {
            var _a;
            (_a = props == null ? void 0 : props.onQuantityChange) == null ? void 0 : _a.call(props, index, value);
          },
          horizontalPadding: "16px"
        }
      )), /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: "pisell-plus-sku-options-card-option-item-card-item-footer-info",
          onClick: () => props.onEdit(index)
        },
        /* @__PURE__ */ import_react.default.createElement("span", null, import_utils.locales.getText(
          "pisell2.skuOptionsSelection.optionItemCard.editSelections"
        )),
        /* @__PURE__ */ import_react.default.createElement(
          import_materials.Iconfont,
          {
            type: "pisell2-chevron-right",
            style: { fontSize: "20px", color: "#000000" }
          }
        )
      ))
    );
  }));
};
var OptionItemCard_default = OptionItemCard;
