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/utils/transformVariantGroup.tsx
var transformVariantGroup_exports = {};
__export(transformVariantGroup_exports, {
  transformVariantGroup: () => transformVariantGroup
});
module.exports = __toCommonJS(transformVariantGroup_exports);
var import_react = __toESM(require("react"));
var import_store = __toESM(require("../store/store"));
var import_VariantCard = __toESM(require("../components/VariantCard"));
var import_checkSkuStatus = require("./checkSkuStatus");
var transformVariantGroup = (variantGroup) => {
  var _a, _b;
  if (!Array.isArray(variantGroup)) return [];
  const variantProducts = ((_a = import_store.default.getState().dataSource) == null ? void 0 : _a.variant) || [];
  const selectorLayout = ((_b = import_store.default.getState().customConfig) == null ? void 0 : _b.selectorLayout) || null;
  const formattedGroup = variantGroup.map((item) => {
    var _a2;
    const selectorOptionsDataSource = (_a2 = item == null ? void 0 : item.variant_item) == null ? void 0 : _a2.map(
      (variantItem) => {
        const { canSell, hasStock } = (0, import_checkSkuStatus.checkVariantStockCanSell)(variantItem);
        const disabled = (0, import_checkSkuStatus.checkSkuDisabled)(variantItem, canSell);
        const price = getVariantItemFixedPrice({
          selectedVariantIds: [`${variantItem == null ? void 0 : variantItem.variant_group_id}:${variantItem == null ? void 0 : variantItem.id}`],
          variantProducts
        });
        return {
          _original: { ...variantItem || {} },
          id: variantItem == null ? void 0 : variantItem.id,
          title: variantItem == null ? void 0 : variantItem.name,
          value: variantItem == null ? void 0 : variantItem.id,
          disabled,
          // 组合规格选项对应的固定价格（若存在多价则返回 null，不展示）
          price,
          ruleConfig: {
            autoValidate: true
          },
          _extra: { canSell, hasStock, disabled }
        };
      }
    );
    const selectorDataSource = {
      id: item.id,
      title: item.name || "",
      mode: "single",
      valueType: "object",
      fieldNames: { value: "value", label: "title" },
      dataSource: selectorOptionsDataSource || [],
      disabled: (item == null ? void 0 : item.is_enabled) === 0,
      ruleConfig: {
        autoValidate: true,
        required: true
      },
      variant: "card",
      preset: {
        variant: "1"
      },
      itemProps: {
        styles: {
          container: {
            padding: 0
          }
        }
      },
      stepperProps: {
        horizontalPadding: "8px"
      },
      layout: {
        type: "antdGrid",
        columns: 2,
        gutter: [16, 16]
      }
    };
    if (!!selectorLayout) {
      selectorDataSource.layout = selectorLayout;
    }
    selectorDataSource.renderItem = (itemProps) => {
      var _a3, _b2, _c;
      const { canSell, hasStock } = (0, import_checkSkuStatus.checkVariantStockByValue)(
        (_a3 = itemProps == null ? void 0 : itemProps.dataSource) == null ? void 0 : _a3._original
      );
      const price = getVariantItemFixedPriceByValue({
        variantItem: (_b2 = itemProps == null ? void 0 : itemProps.dataSource) == null ? void 0 : _b2._original,
        variantProducts
      });
      const _dataSource = {
        ...(itemProps == null ? void 0 : itemProps.dataSource) || {},
        disabled: (0, import_checkSkuStatus.checkSkuDisabled)((_c = itemProps == null ? void 0 : itemProps.dataSource) == null ? void 0 : _c._original, canSell),
        // 组合规格选项对应的固定价格（若存在多价则返回 null，不展示）
        price,
        _extra: {
          canSell,
          hasStock
        }
      };
      return /* @__PURE__ */ import_react.default.createElement(
        import_VariantCard.default,
        {
          ...itemProps || {},
          dataSource: _dataSource,
          parentDataSource: selectorDataSource
        }
      );
    };
    return selectorDataSource;
  });
  return formattedGroup;
};
function getVariantItemFixedPrice(params) {
  const selectedVariantIds = params.selectedVariantIds;
  const variantProducts = params.variantProducts;
  const matchedVariants = Array.isArray(variantProducts) ? variantProducts.filter((item) => {
    var _a;
    const ids = ((_a = item == null ? void 0 : item.sku_ids) == null ? void 0 : _a.split("_")) || [];
    return selectedVariantIds.every((selectedId) => ids.includes(selectedId));
  }) : [];
  const prices = matchedVariants.map((item) => Number((item == null ? void 0 : item.price) ?? (item == null ? void 0 : item.base_price) ?? 0)).filter((n) => !Number.isNaN(n));
  if (prices.length === 0) return null;
  const uniquePrices = Array.from(new Set(prices));
  if (uniquePrices.length !== 1) return null;
  return uniquePrices[0];
}
function getVariantItemFixedPriceByValue(params) {
  var _a;
  const variantItem = params.variantItem;
  const variantProducts = params.variantProducts;
  const dataSource = import_store.default.getState().dataSource;
  const selectorValue = import_store.default.getState().selectorValue;
  const currentSkuId = `${variantItem == null ? void 0 : variantItem.variant_group_id}:${variantItem == null ? void 0 : variantItem.id}`;
  const selectedVariantIds = [currentSkuId];
  (_a = dataSource == null ? void 0 : dataSource.variant_group) == null ? void 0 : _a.forEach((group) => {
    var _a2;
    const selectedData = (_a2 = selectorValue == null ? void 0 : selectorValue[group.id]) == null ? void 0 : _a2.data;
    if (selectedData && group.id !== (variantItem == null ? void 0 : variantItem.variant_group_id)) {
      selectedVariantIds.push(`${selectedData == null ? void 0 : selectedData.variant_group_id}:${selectedData == null ? void 0 : selectedData.id}`);
    }
  });
  return getVariantItemFixedPrice({ selectedVariantIds, variantProducts });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  transformVariantGroup
});
