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/index.tsx
var OptionsCard_exports = {};
__export(OptionsCard_exports, {
  default: () => OptionsCard_default
});
module.exports = __toCommonJS(OptionsCard_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_materials = require("@pisell/materials");
var import_Cards = require("../../../../pro/Cards");
var import_hooks = require("../../hooks");
var import_utils2 = require("../../utils");
var import_OptionsModal = __toESM(require("../OptionsModal"));
var import_OptionItemCard = __toESM(require("./OptionItemCard"));
var import_index = require("./index.less");
var OptionsCard = (props) => {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
  const { _key: uniqueKey, selected, disabled } = (props == null ? void 0 : props.dataSource) || {};
  const {
    cover,
    title,
    name,
    product_subtitle,
    price,
    price_type,
    option_group
  } = ((_a = props == null ? void 0 : props.dataSource) == null ? void 0 : _a._original) || {};
  const symbol = (0, import_hooks.useStore)((s) => s.symbol);
  const skuCardConfig = (0, import_hooks.useStore)((s) => s.skuCardConfig);
  const [isOpen, setIsOpen] = (0, import_react.useState)(false);
  const [modalDataSource, setModalDataSource] = (0, import_react.useState)([]);
  const [modalValue, setModalValue] = (0, import_react.useState)({});
  const currentEditIndexRef = (0, import_react.useRef)(-1);
  const [currentEditIndex, setCurrentEditIndex] = (0, import_react.useState)(-1);
  const _indicator = import_react.default.cloneElement(props == null ? void 0 : props.indicator);
  const selectorRemainingCount = (0, import_react.useMemo)(() => {
    var _a2, _b2;
    const globalMax = ((_b2 = (_a2 = props == null ? void 0 : props.parentDataSource) == null ? void 0 : _a2.ruleConfig) == null ? void 0 : _b2.max) || 0;
    if (globalMax <= 0) return Infinity;
    const values = Array.isArray(props == null ? void 0 : props.values) ? props == null ? void 0 : props.values : [props == null ? void 0 : props.values];
    const totalSelected = values.reduce((sum, i) => sum + ((i == null ? void 0 : i.quantity) || 0), 0) || 0;
    return Math.max(0, globalMax - totalSelected);
  }, [(_c = (_b = props == null ? void 0 : props.parentDataSource) == null ? void 0 : _b.ruleConfig) == null ? void 0 : _c.max, props == null ? void 0 : props.values]);
  const optionRemainingCount = (0, import_react.useMemo)(() => {
    var _a2, _b2, _c2;
    const currentQuantity = ((_a2 = props == null ? void 0 : props.optionItemValue) == null ? void 0 : _a2.quantity) || 0;
    const optionMax = ((_c2 = (_b2 = props == null ? void 0 : props.dataSource) == null ? void 0 : _b2.ruleConfig) == null ? void 0 : _c2.max) || 0;
    const optionRemaining = optionMax > 0 ? optionMax - currentQuantity : Infinity;
    return Math.max(0, Math.min(selectorRemainingCount, optionRemaining));
  }, [
    (_d = props == null ? void 0 : props.optionItemValue) == null ? void 0 : _d.quantity,
    selectorRemainingCount,
    (_f = (_e = props == null ? void 0 : props.dataSource) == null ? void 0 : _e.ruleConfig) == null ? void 0 : _f.max
  ]);
  const modalRemainingCount = (0, import_react.useMemo)(() => {
    if ((props == null ? void 0 : props.mode) === "single" && currentEditIndex === -1) {
      return optionRemainingCount <= 0 ? 1 : optionRemainingCount;
    }
    return optionRemainingCount;
  }, [props == null ? void 0 : props.mode, optionRemainingCount, currentEditIndex]);
  const priceText = (0, import_react.useMemo)(() => {
    return `${price_type === "markdown" ? "-" : ""}${symbol}${Math.abs(
      Number(price || 0)
    )}`;
  }, [price, price_type, symbol]);
  const selectedOptions = (0, import_react.useMemo)(() => {
    var _a2, _b2;
    return ((_b2 = (_a2 = props == null ? void 0 : props.optionItemValue) == null ? void 0 : _a2.data) == null ? void 0 : _b2._data) || [];
  }, [(_h = (_g = props == null ? void 0 : props.optionItemValue) == null ? void 0 : _g.data) == null ? void 0 : _h._data]);
  const transformDataSource = () => {
    if (!!(option_group == null ? void 0 : option_group.length)) {
      const renderOptionGroup = (0, import_utils2.transformOptionGroup)(option_group);
      setModalDataSource(renderOptionGroup || []);
    }
  };
  const handleClick = () => {
    if (disabled) {
      console.log("handleClick_disabled>>>>>", disabled);
      return;
    }
    let needShowToast = false;
    if (optionRemainingCount <= 0) {
      if ((props == null ? void 0 : props.mode) === "single" && !selected) {
        needShowToast = false;
      } else {
        needShowToast = true;
      }
    } else {
      needShowToast = false;
    }
    if (needShowToast) {
      (0, import_materials.PisellToast)({
        content: import_utils.locales.getText(
          "pisell2.skuOptionsSelection.optionItemCard.overSelected"
        )
      });
      return;
    }
    setModalValue({});
    currentEditIndexRef.current = -1;
    setCurrentEditIndex(-1);
    setIsOpen(true);
    transformDataSource();
  };
  const handleUpdateClick = (index) => {
    var _a2, _b2, _c2;
    currentEditIndexRef.current = index;
    setCurrentEditIndex(index);
    setIsOpen(true);
    setModalValue(((_c2 = (_b2 = (_a2 = props == null ? void 0 : props.optionItemValue) == null ? void 0 : _a2.data) == null ? void 0 : _b2._data) == null ? void 0 : _c2[index]) || {});
    transformDataSource();
  };
  const handleConfirm = (v) => {
    var _a2;
    console.log("onConfirm_v>>>>>", v, props == null ? void 0 : props.optionItemValue, props == null ? void 0 : props.values);
    const [newOriginal, totalPrice, totalQuantity] = (0, import_utils2.calculateOptionItem)({
      editIndex: currentEditIndexRef.current,
      optionItemValue: props == null ? void 0 : props.optionItemValue,
      optionItemDataSource: props == null ? void 0 : props.dataSource,
      data: v
    });
    (_a2 = props == null ? void 0 : props.actions) == null ? void 0 : _a2.update(uniqueKey, {
      quantity: totalQuantity,
      totalPrice,
      data: newOriginal
    });
    setIsOpen(false);
    setModalValue({});
  };
  const handleQuantityChange = (_index, _quantity) => {
    var _a2, _b2, _c2, _d2;
    const currentData = ((_c2 = (_b2 = (_a2 = props == null ? void 0 : props.optionItemValue) == null ? void 0 : _a2.data) == null ? void 0 : _b2._data) == null ? void 0 : _c2[_index]) || {};
    const [newOriginal, totalPrice, totalQuantity] = (0, import_utils2.calculateOptionItem)({
      editIndex: _index,
      optionItemValue: props == null ? void 0 : props.optionItemValue,
      optionItemDataSource: props == null ? void 0 : props.dataSource,
      data: { ...currentData, quantity: _quantity }
    });
    (_d2 = props == null ? void 0 : props.actions) == null ? void 0 : _d2.update(uniqueKey, {
      quantity: totalQuantity,
      totalPrice,
      data: newOriginal
    });
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-plus-sku-options-card", onClick: handleClick }, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      onClick: (e) => {
        var _a2;
        e.stopPropagation();
        if (selected) {
          (_a2 = props == null ? void 0 : props.actions) == null ? void 0 : _a2.update(uniqueKey, { quantity: 0 });
        } else {
          handleClick();
        }
      },
      className: "pisell-plus-sku-options-card-indicator-wrapper"
    },
    _indicator,
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-plus-sku-options-card-indicator-wrapper-overlay" })
  ), /* @__PURE__ */ import_react.default.createElement(
    import_Cards.SKUCard,
    {
      cover,
      title: title || name || "",
      subtitle: product_subtitle,
      price: priceText,
      arrow: true,
      selected,
      disabled,
      skuCardConfig,
      skuDisabled: ((_j = (_i = props == null ? void 0 : props.dataSource) == null ? void 0 : _i._extra) == null ? void 0 : _j.disabled) === true
    }
  )), (selectedOptions == null ? void 0 : selectedOptions.length) > 0 && /* @__PURE__ */ import_react.default.createElement(
    import_OptionItemCard.default,
    {
      optionItemValue: props == null ? void 0 : props.optionItemValue,
      parentDataSource: props == null ? void 0 : props.parentDataSource,
      dataSource: props == null ? void 0 : props.dataSource,
      data: selectedOptions,
      remainingCount: optionRemainingCount,
      onEdit: handleUpdateClick,
      onQuantityChange: handleQuantityChange
    }
  ), isOpen && /* @__PURE__ */ import_react.default.createElement(
    import_OptionsModal.default,
    {
      parentDataSource: props == null ? void 0 : props.parentDataSource,
      dataSource: props == null ? void 0 : props.dataSource,
      modalDataSource,
      values: props == null ? void 0 : props.values,
      optionItemValue: props == null ? void 0 : props.optionItemValue,
      modalValue,
      remainingCount: modalRemainingCount,
      open: isOpen,
      title: title || name || "",
      onCancel: () => setIsOpen(false),
      onConfirm: handleConfirm
    }
  ));
};
var OptionsCard_default = OptionsCard;
