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 NormalCard from "../components/NormalCard";
import TitleExtra from "../components/TitleExtra";
import { checkSkuDisabled, checkSkuStockCanSell } from "./checkSkuStatus";
import store from "../store/store";
export var transformOptionGroup = function transformOptionGroup(optionGroup) {
  var _store$getState$custo;
  if (!Array.isArray(optionGroup)) return [];

  // 获取自定义Selector布局配置
  var selectorLayout = ((_store$getState$custo = store.getState().customConfig) === null || _store$getState$custo === void 0 ? void 0 : _store$getState$custo.selectorLayout) || null;
  var formattedGroup = optionGroup.map(function (item) {
    var _item$option_item;
    // Selector中选项数据源
    var selectorOptionsDataSource = item === null || item === void 0 || (_item$option_item = item.option_item) === null || _item$option_item === void 0 ? void 0 : _item$option_item.map(function (optionItem) {
      var _checkSkuStockCanSell = checkSkuStockCanSell(optionItem),
        canSell = _checkSkuStockCanSell.canSell,
        hasStock = _checkSkuStockCanSell.hasStock;
      var disabled = checkSkuDisabled(optionItem, canSell);
      return {
        _original: _objectSpread(_objectSpread({}, optionItem || {}), {}, {
          product_option_item_id: optionItem === null || optionItem === void 0 ? void 0 : optionItem.id
        }),
        id: optionItem === null || optionItem === void 0 ? void 0 : optionItem.id,
        title: optionItem === null || optionItem === void 0 ? void 0 : optionItem.name,
        value: optionItem === null || optionItem === void 0 ? void 0 : optionItem.id,
        disabled: disabled,
        ruleConfig: {
          min: optionItem === null || optionItem === void 0 ? void 0 : optionItem.min_quantity,
          max: optionItem === null || optionItem === void 0 ? void 0 : optionItem.max_quantity,
          autoValidate: true
        },
        _extra: {
          canSell: canSell,
          hasStock: hasStock,
          disabled: disabled
        }
      };
    });

    // Selector数据源
    var isMultiple = (item === null || item === void 0 ? void 0 : item.is_multiple) == 1;
    var selectorDataSource = {
      id: item.id,
      title: item.name || '',
      mode: isMultiple ? 'multiple' : 'single',
      valueType: 'object',
      fieldNames: {
        value: 'value',
        label: 'title'
      },
      dataSource: selectorOptionsDataSource || [],
      disabled: (item === null || item === void 0 ? void 0 : item.is_enabled) === 0,
      ruleConfig: {
        // 多选时以设置为准；单选时不限制
        min: isMultiple ? item === null || item === void 0 ? void 0 : item.min_quantity : undefined,
        // 多选时，max为0时表示不限制最大选择数量；单选时不限制最大选择数量
        max: isMultiple ? Number(item === null || item === void 0 ? void 0 : item.max_quantity) == 0 ? undefined : item === null || item === void 0 ? void 0 : item.max_quantity : undefined,
        autoValidate: true,
        // 单规格必选判定分成两种：
        // 1. 未开启多选时以：item?.required === 1 为准
        // 2. 开启多选时以：item?.min_quantity > 0 为准
        required: isMultiple ? Number((item === null || item === void 0 ? void 0 : item.min_quantity) || 0) > 0 : (item === null || item === void 0 ? void 0 : item.required) === 1
      },
      headerProps: {
        renderExtra: function renderExtra(props) {
          return /*#__PURE__*/React.createElement(TitleExtra, props || {});
        }
      },
      stepperProps: {
        horizontalPadding: '8px'
      }
    };
    if (!!selectorLayout) {
      selectorDataSource.layout = selectorLayout;
    }

    // 渲染选项卡片
    selectorDataSource.renderItem = function (itemProps) {
      return /*#__PURE__*/React.createElement(NormalCard, _extends({}, itemProps || {}, {
        parentDataSource: selectorDataSource
      }));
    };
    return selectorDataSource;
  });
  return formattedGroup;
};