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/OptionsModal/index.tsx
var OptionsModal_exports = {};
__export(OptionsModal_exports, {
  default: () => OptionsModal_default
});
module.exports = __toCommonJS(OptionsModal_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_compensatedPisellContainer = __toESM(require("../../../../pro/compensatedPisellContainer"));
var import_Selector = __toESM(require("../../../../pro/Selector"));
var import_NumericStepper = __toESM(require("../../../../pro/NumericStepper"));
var import_responsive = require("../../../../responsive");
var import_hooks = require("../../hooks");
var import_index = require("./index.less");
var SelectorGroup = import_Selector.default.Group;
var OptionsModal = (props) => {
  var _a, _b, _c, _d, _e, _f;
  const { open, title, remainingCount, onCancel, onConfirm, variant } = props;
  const selectorGroupRef = (0, import_react.useRef)(null);
  const [stepNumber, setStepNumber] = (0, import_react.useState)(1);
  const [modalValue, setModalValue] = (0, import_react.useState)((props == null ? void 0 : props.modalValue) || {});
  const customConfig = (0, import_hooks.useStore)((s) => s.customConfig);
  console.log(customConfig, "customConfig>>>>>>>");
  const platform = variant === "phone" ? "h5" : variant === "tablet" || variant === "smallTablet" ? "ipad" : "pc";
  (0, import_react.useEffect)(() => {
    var _a2, _b2;
    if (!!((_a2 = props == null ? void 0 : props.modalValue) == null ? void 0 : _a2.quantity)) {
      setStepNumber((_b2 = props == null ? void 0 : props.modalValue) == null ? void 0 : _b2.quantity);
    } else if (remainingCount > 0) {
      setStepNumber(1);
    } else {
      setStepNumber(0);
    }
  }, [remainingCount, (_a = props == null ? void 0 : props.optionItemValue) == null ? void 0 : _a.quantity]);
  const canSelectedMax = (0, import_react.useMemo)(() => {
    var _a2, _b2, _c2;
    const currentQuantity = ((_a2 = props == null ? void 0 : props.modalValue) == null ? void 0 : _a2.quantity) || 0;
    const effectiveRemainingCount = remainingCount + currentQuantity;
    if (effectiveRemainingCount <= 0) return 0;
    const _max = ((_c2 = (_b2 = props == null ? void 0 : props.dataSource) == null ? void 0 : _b2.ruleConfig) == null ? void 0 : _c2.max) || 0;
    const _effectiveMax = Math.min(effectiveRemainingCount, _max);
    return _effectiveMax > 0 ? _effectiveMax : Infinity;
  }, [
    remainingCount,
    (_c = (_b = props == null ? void 0 : props.dataSource) == null ? void 0 : _b.ruleConfig) == null ? void 0 : _c.max,
    (_d = props == null ? void 0 : props.modalValue) == null ? void 0 : _d.quantity
  ]);
  const canSelectedMin = (0, import_react.useMemo)(() => {
    var _a2, _b2;
    return ((_b2 = (_a2 = props == null ? void 0 : props.dataSource) == null ? void 0 : _a2.ruleConfig) == null ? void 0 : _b2.min) || 1;
  }, [(_f = (_e = props == null ? void 0 : props.dataSource) == null ? void 0 : _e.ruleConfig) == null ? void 0 : _f.min]);
  const handleConfirm = async () => {
    var _a2;
    try {
      const result = await ((_a2 = selectorGroupRef.current) == null ? void 0 : _a2.validate());
      if (result) {
        onConfirm({ ...modalValue, quantity: stepNumber });
      }
    } catch (error) {
      console.log("handleConfirm_error>>>>>", error);
    }
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_compensatedPisellContainer.default,
    {
      renderType: "modal",
      props: {
        platform,
        open,
        onCancel,
        title,
        className: (0, import_classnames.default)("pisell-sku-options-modal", {
          "pisell-sku-options-modal--smallTablet": variant === "smallTablet",
          "pisell-sku-options-modal--tablet": variant === "tablet"
        }),
        footer: /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-plus-sku-options-card-footer" }, /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement(
          import_NumericStepper.default,
          {
            min: canSelectedMin,
            max: canSelectedMax,
            value: stepNumber,
            onChange: (value) => {
              setStepNumber(value);
            }
          }
        )), /* @__PURE__ */ import_react.default.createElement(
          import_materials.Button,
          {
            type: "primary",
            onClick: handleConfirm,
            size: "large",
            block: true
          },
          import_utils.locales.getText("pisell2.skuOptionsSelection.optionItemCard.confirm")
        )),
        footerStyle: {
          borderTop: "none"
        },
        destroyOnClose: true,
        bodyStyle: {
          paddingLeft: 0,
          paddingRight: 0,
          overflow: "hidden",
          display: "flex",
          flexDirection: "column"
        },
        width: (customConfig == null ? void 0 : customConfig.modalWidth) || "480px"
      }
    },
    /* @__PURE__ */ import_react.default.createElement(
      SelectorGroup,
      {
        ref: selectorGroupRef,
        dataSource: props == null ? void 0 : props.modalDataSource,
        value: (modalValue == null ? void 0 : modalValue.data) || {},
        onChange: (value) => {
          setModalValue({ ...modalValue, data: value });
        },
        style: { height: "100%", padding: "0 24px", overflowY: "auto" }
      }
    )
  );
};
var OptionsModal_default = (0, import_responsive.withResponsive)(OptionsModal);
