function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React from 'react';
import store from "../store/store";
import VariantCard from "../components/VariantCard";
import { checkVariantStockCanSell, checkSkuDisabled, checkVariantStockByValue } from "./checkSkuStatus";
export var transformVariantGroup = function transformVariantGroup(variantGroup) {
  var _store$getState$dataS, _store$getState$custo;
  if (!Array.isArray(variantGroup)) return [];
  var variantProducts = ((_store$getState$dataS = store.getState().dataSource) === null || _store$getState$dataS === void 0 ? void 0 : _store$getState$dataS.variant) || [];
  // 获取自定义Selector布局配置
  var selectorLayout = ((_store$getState$custo = store.getState().customConfig) === null || _store$getState$custo === void 0 ? void 0 : _store$getState$custo.selectorLayout) || null;
  var formattedGroup = variantGroup.map(function (item) {
    var _item$variant_item;
    // Selector中选项数据源
    var selectorOptionsDataSource = item === null || item === void 0 || (_item$variant_item = item.variant_item) === null || _item$variant_item === void 0 ? void 0 : _item$variant_item.map(function (variantItem) {
      var _checkVariantStockCan = checkVariantStockCanSell(variantItem),
        canSell = _checkVariantStockCan.canSell,
        hasStock = _checkVariantStockCan.hasStock;
      var disabled = checkSkuDisabled(variantItem, canSell);
      var price = getVariantItemFixedPrice({
        selectedVariantIds: ["".concat(variantItem === null || variantItem === void 0 ? void 0 : variantItem.variant_group_id, ":").concat(variantItem === null || variantItem === void 0 ? void 0 : variantItem.id)],
        variantProducts: variantProducts
      });
      return {
        _original: _objectSpread({}, variantItem || {}),
        id: variantItem === null || variantItem === void 0 ? void 0 : variantItem.id,
        title: variantItem === null || variantItem === void 0 ? void 0 : variantItem.name,
        value: variantItem === null || variantItem === void 0 ? void 0 : variantItem.id,
        disabled: disabled,
        // 组合规格选项对应的固定价格（若存在多价则返回 null，不展示）
        price: price,
        ruleConfig: {
          autoValidate: true
        },
        _extra: {
          canSell: canSell,
          hasStock: hasStock,
          disabled: disabled
        }
      };
    });

    // Selector数据源
    var selectorDataSource = {
      id: item.id,
      title: item.name || '',
      mode: 'single',
      valueType: 'object',
      fieldNames: {
        value: 'value',
        label: 'title'
      },
      dataSource: selectorOptionsDataSource || [],
      disabled: (item === null || item === void 0 ? 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 = function (itemProps) {
      var _itemProps$dataSource, _itemProps$dataSource2, _itemProps$dataSource3;
      var _checkVariantStockByV = checkVariantStockByValue(itemProps === null || itemProps === void 0 || (_itemProps$dataSource = itemProps.dataSource) === null || _itemProps$dataSource === void 0 ? void 0 : _itemProps$dataSource._original),
        canSell = _checkVariantStockByV.canSell,
        hasStock = _checkVariantStockByV.hasStock;
      var price = getVariantItemFixedPriceByValue({
        variantItem: itemProps === null || itemProps === void 0 || (_itemProps$dataSource2 = itemProps.dataSource) === null || _itemProps$dataSource2 === void 0 ? void 0 : _itemProps$dataSource2._original,
        variantProducts: variantProducts
      });
      var _dataSource = _objectSpread(_objectSpread({}, (itemProps === null || itemProps === void 0 ? void 0 : itemProps.dataSource) || {}), {}, {
        disabled: checkSkuDisabled(itemProps === null || itemProps === void 0 || (_itemProps$dataSource3 = itemProps.dataSource) === null || _itemProps$dataSource3 === void 0 ? void 0 : _itemProps$dataSource3._original, canSell),
        // 组合规格选项对应的固定价格（若存在多价则返回 null，不展示）
        price: price,
        _extra: {
          canSell: canSell,
          hasStock: hasStock
        }
      });
      return /*#__PURE__*/React.createElement(VariantCard, _extends({}, itemProps || {}, {
        dataSource: _dataSource,
        parentDataSource: selectorDataSource
      }));
    };
    return selectorDataSource;
  });
  return formattedGroup;
};

/**
 * @description 在给定匹配范围内计算“固定价格”
 * - 若匹配到多个不同价格，则认为不是固定价格，返回 null（不展示）
 */
function getVariantItemFixedPrice(params) {
  var selectedVariantIds = params.selectedVariantIds;
  var variantProducts = params.variantProducts;
  var matchedVariants = Array.isArray(variantProducts) ? variantProducts.filter(function (item) {
    var _item$sku_ids;
    var ids = (item === null || item === void 0 || (_item$sku_ids = item.sku_ids) === null || _item$sku_ids === void 0 ? void 0 : _item$sku_ids.split('_')) || [];
    return selectedVariantIds.every(function (selectedId) {
      return ids.includes(selectedId);
    });
  }) : [];
  var prices = matchedVariants.map(function (item) {
    var _ref, _item$price;
    return Number((_ref = (_item$price = item === null || item === void 0 ? void 0 : item.price) !== null && _item$price !== void 0 ? _item$price : item === null || item === void 0 ? void 0 : item.base_price) !== null && _ref !== void 0 ? _ref : 0);
  }).filter(function (n) {
    return !Number.isNaN(n);
  });
  if (prices.length === 0) return null;
  var uniquePrices = Array.from(new Set(prices));
  if (uniquePrices.length !== 1) return null;
  return uniquePrices[0];
}

/**
 * @description 基于当前 selectorValue（已选项）计算某个 variantItem 的固定价格
 * - 用当前选项 + 其它规格组已选项筛选组合 SKU
 * - 若筛出来是多价，则不展示
 */
function getVariantItemFixedPriceByValue(params) {
  var _dataSource$variant_g;
  var variantItem = params.variantItem;
  var variantProducts = params.variantProducts;
  var dataSource = store.getState().dataSource;
  var selectorValue = store.getState().selectorValue;
  var currentSkuId = "".concat(variantItem === null || variantItem === void 0 ? void 0 : variantItem.variant_group_id, ":").concat(variantItem === null || variantItem === void 0 ? void 0 : variantItem.id);
  var selectedVariantIds = [currentSkuId];
  dataSource === null || dataSource === void 0 || (_dataSource$variant_g = dataSource.variant_group) === null || _dataSource$variant_g === void 0 || _dataSource$variant_g.forEach(function (group) {
    var _selectorValue$group$;
    var selectedData = selectorValue === null || selectorValue === void 0 || (_selectorValue$group$ = selectorValue[group.id]) === null || _selectorValue$group$ === void 0 ? void 0 : _selectorValue$group$.data;
    if (selectedData && group.id !== (variantItem === null || variantItem === void 0 ? void 0 : variantItem.variant_group_id)) {
      selectedVariantIds.push("".concat(selectedData === null || selectedData === void 0 ? void 0 : selectedData.variant_group_id, ":").concat(selectedData === null || selectedData === void 0 ? void 0 : selectedData.id));
    }
  });
  return getVariantItemFixedPrice({
    selectedVariantIds: selectedVariantIds,
    variantProducts: variantProducts
  });
}