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/pisellSalesProductCard/index.tsx
var pisellSalesProductCard_exports = {};
__export(pisellSalesProductCard_exports, {
  default: () => pisellSalesProductCard_default,
  isNormalProduct: () => isNormalProduct
});
module.exports = __toCommonJS(pisellSalesProductCard_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_NumericStepper = __toESM(require("../../pro/NumericStepper"));
var import_pisellPriceText = __toESM(require("../productSelect/components/pisellPriceText"));
var import_index = require("./index.less");
var import_locales = __toESM(require("./locales"));
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_utils2 = require("./utils");
var PREFIX = "pisell-plus-sales-product-card";
function resolveText(text) {
  if (!text) return "";
  if (typeof text === "string") return text;
  const values = Object.values(text || {});
  return values.find((item) => !!item) || "";
}
function toStringValue(value) {
  if (value === void 0 || value === null) return "";
  return String(value);
}
function toNumber(value) {
  const parsed = Number(value);
  if (Number.isNaN(parsed)) return 0;
  return parsed;
}
function getTextCover(text) {
  const cleanText = text.trim();
  if (!cleanText) return "";
  return cleanText.substring(0, 2).toUpperCase();
}
function isNormalProduct(product) {
  if (!product) return true;
  const { is_variant, is_bundle } = product;
  const hasOption = Array.isArray(product.option_group) && product.option_group.length > 0;
  return !(hasOption || is_bundle || is_variant);
}
var PisellSalesProductCard = ({
  product,
  value,
  disabled = false,
  stepperProps,
  optionsText,
  skuCardConfig,
  cartElementId,
  style,
  className,
  onChange,
  onClick
}) => {
  var _a, _b;
  const context = (0, import_useEngineContext.default)();
  import_utils.locales.init(
    import_locales.default,
    ((_b = (_a = context.appHelper.utils) == null ? void 0 : _a.storage) == null ? void 0 : _b.get("umi_locale")) || "en"
  );
  const normalProduct = (0, import_react.useMemo)(() => isNormalProduct(product), [product]);
  const mergedDisabled = disabled || (product == null ? void 0 : product.disabled) === 1 || (product == null ? void 0 : product.disabled) === true;
  const title = resolveText((product == null ? void 0 : product.title) || (product == null ? void 0 : product.name));
  const textCover = getTextCover(title);
  const imageStyle = (0, import_react.useMemo)(() => {
    var _a2, _b2, _c, _d;
    let _style = {};
    if ((_a2 = skuCardConfig == null ? void 0 : skuCardConfig.image) == null ? void 0 : _a2.width) {
      _style.width = (_b2 = skuCardConfig == null ? void 0 : skuCardConfig.image) == null ? void 0 : _b2.width;
    }
    if ((_c = skuCardConfig == null ? void 0 : skuCardConfig.image) == null ? void 0 : _c.height) {
      _style.height = (_d = skuCardConfig == null ? void 0 : skuCardConfig.image) == null ? void 0 : _d.height;
    }
    return _style;
  }, [skuCardConfig]);
  const buildNextValue = (nextNum) => {
    if (value) {
      return {
        ...value,
        num: nextNum
      };
    }
    return {
      product_id: product == null ? void 0 : product.id,
      num: nextNum,
      product_option_item: [],
      original_price: toStringValue(product == null ? void 0 : product.original_price),
      is_charge_tax: (product == null ? void 0 : product.is_charge_tax) ?? 0,
      discount_list: [],
      product_bundle: (product == null ? void 0 : product.product_bundle) ?? [],
      metadata: {}
    };
  };
  const cardMaxQuantity = (0, import_react.useMemo)(() => {
    let maxNum = 200;
    const detailAny = product;
    const hasVariantGroup = Array.isArray(detailAny == null ? void 0 : detailAny.variant_group) && detailAny.variant_group.length > 0;
    if (!hasVariantGroup && (detailAny == null ? void 0 : detailAny.is_track) === 1 && (detailAny == null ? void 0 : detailAny.over_sold) === 0) {
      const stock = toNumber(detailAny == null ? void 0 : detailAny.stock_quantity);
      maxNum = stock >= 200 ? 200 : stock;
    }
    const selectedVariantId = toNumber(value == null ? void 0 : value.product_variant_id);
    if (selectedVariantId > 0 && Array.isArray(detailAny == null ? void 0 : detailAny.variant)) {
      const selectedVariant = detailAny.variant.find(
        (item) => toNumber(item == null ? void 0 : item.id) === selectedVariantId
      );
      if ((selectedVariant == null ? void 0 : selectedVariant.is_track) === 1 && (selectedVariant == null ? void 0 : selectedVariant.over_sold) === 0) {
        const variantStock = toNumber(selectedVariant == null ? void 0 : selectedVariant.stock_quantity);
        maxNum = variantStock >= 200 ? 200 : variantStock;
      }
    }
    const propMax = Number.isFinite(stepperProps == null ? void 0 : stepperProps.max) ? Number(stepperProps == null ? void 0 : stepperProps.max) : Infinity;
    const mergedMax = Math.min(maxNum, propMax);
    if (!Number.isFinite(mergedMax)) return Math.max((stepperProps == null ? void 0 : stepperProps.min) ?? 0, maxNum);
    return Math.max((stepperProps == null ? void 0 : stepperProps.min) ?? 0, mergedMax);
  }, [product, value == null ? void 0 : value.product_variant_id, stepperProps == null ? void 0 : stepperProps.max, stepperProps == null ? void 0 : stepperProps.min]);
  const handleCardClick = () => {
    if (mergedDisabled) return;
    const meta = {
      intent: normalProduct ? "detail" : "options",
      trigger: "card",
      product
    };
    onClick == null ? void 0 : onClick(value || {
      product_id: product == null ? void 0 : product.id
    }, meta);
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)(PREFIX, className), style }, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)(`${PREFIX}__inner`, {
        [`${PREFIX}__inner-disabled`]: mergedDisabled
      }),
      onClick: handleCardClick
    },
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: `${PREFIX}__image`,
        style: imageStyle
      },
      (product == null ? void 0 : product.cover) ? /* @__PURE__ */ import_react.default.createElement("img", { src: import_utils.image.ali(product.cover, 360), alt: title }) : /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__image-placeholder` }, textCover)
    ),
    /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__content` }, /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__title` }, title), /* @__PURE__ */ import_react.default.createElement(import_pisellPriceText.default, { value: product.price ?? 0, isMini: !!product.is_bundle })),
    /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__action` }, normalProduct ? /* @__PURE__ */ import_react.default.createElement("div", { className: `${PREFIX}__stepper` }, /* @__PURE__ */ import_react.default.createElement(
      import_NumericStepper.default,
      {
        value: (value == null ? void 0 : value.num) ?? 0,
        min: (stepperProps == null ? void 0 : stepperProps.min) ?? 0,
        max: cardMaxQuantity,
        step: (stepperProps == null ? void 0 : stepperProps.step) ?? 1,
        disabled: mergedDisabled,
        onLimitExceeded: (limitMeta) => {
          const raw = stepperProps == null ? void 0 : stepperProps.limitMessage;
          const limitMsg = typeof raw === "string" ? { max: raw } : raw || {};
          const content = limitMeta.reason === "max" ? limitMsg.max : limitMsg.min;
          if (!content) return;
          (0, import_materials.PisellToast)({
            content,
            duration: 2
          });
        },
        onChange: (next, stepperMeta) => {
          const safeNext = Math.max(
            (stepperProps == null ? void 0 : stepperProps.min) ?? 0,
            Math.min(cardMaxQuantity, next)
          );
          if ((stepperMeta == null ? void 0 : stepperMeta.action) === "increase" && stepperMeta.event && cartElementId) {
            (0, import_utils2.cubicBezier)({
              position: {
                x: stepperMeta.event.clientX,
                y: stepperMeta.event.clientY
              },
              cartElementId
            });
          }
          const nextValue = buildNextValue(safeNext);
          const meta = {
            intent: "quantity",
            trigger: "stepper",
            product
          };
          onChange == null ? void 0 : onChange(nextValue, meta);
        }
      }
    )) : /* @__PURE__ */ import_react.default.createElement(
      import_materials.Button,
      {
        type: "default",
        size: "small",
        className: `${PREFIX}__options-button`,
        disabled: mergedDisabled,
        onClick: (e) => {
          e.stopPropagation();
          const meta = {
            intent: "options",
            trigger: "button",
            product
          };
          onClick == null ? void 0 : onClick(
            value || {
              product_id: product == null ? void 0 : product.id
            },
            meta
          );
        }
      },
      optionsText || import_utils.locales.getText("pisell2.text.options")
    ))
  )));
};
var pisellSalesProductCard_default = PisellSalesProductCard;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  isNormalProduct
});
