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); }
var _excluded = ["_data"];
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); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import Decimal from 'decimal.js';
import store from "../store/store";
/**
 * 将SelectorValue转换为Value
 */
export var transformSelectorValueToValue = function transformSelectorValueToValue(selectorValue) {
  var _dataSource$bundle_gr, _dataSource$option_gr, _dataSource$variant_g;
  if (!selectorValue) {
    return {};
  }
  var dataSource = store.getState().dataSource;
  var transformValue = {
    bundle: [],
    // 套餐商品组数据
    option: [],
    // 单规格组数据
    product_id: dataSource === null || dataSource === void 0 ? void 0 : dataSource.id,
    // 商品id
    product_variant_id: 0,
    // 多规格商品id
    product_option_string: '' // 多规格商品选项字符串
  };
  // selectorValue整体是一个Object，根据key从dataSourct中进行判断，
  // 如果key落在了bundle_group中，则key对应的value中的data需要处理后落入bundle中，如果data中存在_data需要遍历后和外层的data数据合并后拆分落入bundle
  // 如果key落在了option_group中，则key对应的value中的data需要处理后落入option中
  // 如果key落在了variant_group中，则key对应的value中的data需要需要从dataSource中的variant中进行匹配，最终筛选出对应的组合规格商品
  // 商品是套餐商品，只需要转换到bundle中
  if (dataSource !== null && dataSource !== void 0 && (_dataSource$bundle_gr = dataSource.bundle_group) !== null && _dataSource$bundle_gr !== void 0 && _dataSource$bundle_gr.length) {
    var _dataSource$bundle_gr2;
    dataSource === null || dataSource === void 0 || (_dataSource$bundle_gr2 = dataSource.bundle_group) === null || _dataSource$bundle_gr2 === void 0 || _dataSource$bundle_gr2.forEach(function (bundleGroupItem) {
      var groupId = bundleGroupItem === null || bundleGroupItem === void 0 ? void 0 : bundleGroupItem.id;
      var groupItemValue = selectorValue === null || selectorValue === void 0 ? void 0 : selectorValue[groupId];
      // 判断groupId是否在selectorValue中
      if (!groupId || !groupItemValue) {
        return;
      }
      // groupItemValue可能是数组也可能是对象
      var _groupItemValue = Array.isArray(groupItemValue) ? groupItemValue : [groupItemValue];
      _groupItemValue.forEach(function (item) {
        // transformValue.bundle.push(item);
        // 需要判断item.data中是否存在_data，如果存在则需要遍历后和外层的data数据合并后拆分落入bundle
        // 存在_data则表示是单规格组
        var _ref = (item === null || item === void 0 ? void 0 : item.data) || {},
          _data = _ref._data,
          rest = _objectWithoutProperties(_ref, _excluded);
        if (_data) {
          _data === null || _data === void 0 || _data.forEach(function (_item) {
            if (_item !== null && _item !== void 0 && _item.data) {
              var _newData = _objectSpread(_objectSpread({}, rest), {}, {
                option: []
              });
              var totalPrice = new Decimal(0);
              Object.keys(_item === null || _item === void 0 ? void 0 : _item.data).forEach(function (key) {
                var _item$data;
                var _itemDataItem = _item === null || _item === void 0 || (_item$data = _item.data) === null || _item$data === void 0 ? void 0 : _item$data[key];
                if (Array.isArray(_itemDataItem)) {
                  _itemDataItem.forEach(function (_itemDataItem_item) {
                    var _itemDataItem_item$da;
                    totalPrice = totalPrice.plus(new Decimal((_itemDataItem_item === null || _itemDataItem_item === void 0 ? void 0 : _itemDataItem_item.totalPrice) || 0));
                    _newData.option.push(_objectSpread(_objectSpread({}, (_itemDataItem_item === null || _itemDataItem_item === void 0 ? void 0 : _itemDataItem_item.data) || {}), {}, {
                      product_option_item_id: _itemDataItem_item === null || _itemDataItem_item === void 0 || (_itemDataItem_item$da = _itemDataItem_item.data) === null || _itemDataItem_item$da === void 0 ? void 0 : _itemDataItem_item$da.id
                    }));
                  });
                } else {
                  var _itemDataItem$data;
                  totalPrice = totalPrice.plus(new Decimal((_itemDataItem === null || _itemDataItem === void 0 ? void 0 : _itemDataItem.totalPrice) || 0));
                  _newData.option.push(_objectSpread(_objectSpread({}, (_itemDataItem === null || _itemDataItem === void 0 ? void 0 : _itemDataItem.data) || {}), {}, {
                    product_option_item_id: _itemDataItem === null || _itemDataItem === void 0 || (_itemDataItem$data = _itemDataItem.data) === null || _itemDataItem$data === void 0 ? void 0 : _itemDataItem$data.id
                  }));
                }
              });
              var _restPricePrefix = (rest === null || rest === void 0 ? void 0 : rest.price_type) === 'markdown' ? -1 : 1;
              _newData.price = totalPrice.plus(new Decimal((rest === null || rest === void 0 ? void 0 : rest.price) || 0).times(_restPricePrefix)).toString();
              _newData.original_price = _newData === null || _newData === void 0 ? void 0 : _newData.price;
              _newData.original_total = _newData === null || _newData === void 0 ? void 0 : _newData.price;
              _newData.quantity = (_item === null || _item === void 0 ? void 0 : _item.quantity) || 0;
              _newData.num = (_item === null || _item === void 0 ? void 0 : _item.quantity) || 0;
              // 必须：构建一个新的 bundle_product_id
              _newData._bundle_product_id = _newData === null || _newData === void 0 ? void 0 : _newData.bundle_product_id;
              transformValue.bundle.push(_newData);
            }
          });
        } else {
          var _newData = _objectSpread({}, (item === null || item === void 0 ? void 0 : item.data) || {});
          var _pricePrefix = (_newData === null || _newData === void 0 ? void 0 : _newData.price_type) === 'markdown' ? -1 : 1;
          _newData.price = new Decimal((_newData === null || _newData === void 0 ? void 0 : _newData.price) || 0).times(_pricePrefix).toString();
          _newData.original_price = _newData === null || _newData === void 0 ? void 0 : _newData.price;
          _newData.original_total = _newData === null || _newData === void 0 ? void 0 : _newData.price;
          // 必须：构建一个新的 bundle_product_id
          _newData._bundle_product_id = _newData === null || _newData === void 0 ? void 0 : _newData.bundle_product_id;
          transformValue.bundle.push(_newData);
        }
      });
    });
  }

  // 商品存在单规格组，需要转换到option中
  if (dataSource !== null && dataSource !== void 0 && (_dataSource$option_gr = dataSource.option_group) !== null && _dataSource$option_gr !== void 0 && _dataSource$option_gr.length) {
    var _dataSource$option_gr2;
    dataSource === null || dataSource === void 0 || (_dataSource$option_gr2 = dataSource.option_group) === null || _dataSource$option_gr2 === void 0 || _dataSource$option_gr2.forEach(function (optionGroupItem) {
      var _groupItemValue2;
      var groupId = optionGroupItem === null || optionGroupItem === void 0 ? void 0 : optionGroupItem.id;
      var groupItemValue = selectorValue === null || selectorValue === void 0 ? void 0 : selectorValue[groupId];
      if (!groupId || !groupItemValue) {
        return;
      }
      var _groupItemValue = null;
      if (Array.isArray(groupItemValue)) {
        _groupItemValue = groupItemValue;
      } else {
        _groupItemValue = [groupItemValue];
      }
      (_groupItemValue2 = _groupItemValue) === null || _groupItemValue2 === void 0 || _groupItemValue2.forEach(function (item) {
        if (item !== null && item !== void 0 && item.data) {
          var _item$data2;
          transformValue.option.push(_objectSpread(_objectSpread({}, (item === null || item === void 0 ? void 0 : item.data) || {}), {}, {
            product_option_item_id: item === null || item === void 0 || (_item$data2 = item.data) === null || _item$data2 === void 0 ? void 0 : _item$data2.id
          }));
        }
      });
    });
  }

  // 商品存在组合规格组，需要先收集已选择的规格，然后去variant中做全匹配筛选出对应的组合规格商品
  if (dataSource !== null && dataSource !== void 0 && (_dataSource$variant_g = dataSource.variant_group) !== null && _dataSource$variant_g !== void 0 && _dataSource$variant_g.length) {
    var _dataSource$variant_g2;
    var selectedVariantIds = [];
    var selectedVariantOptionString = [];
    dataSource === null || dataSource === void 0 || (_dataSource$variant_g2 = dataSource.variant_group) === null || _dataSource$variant_g2 === void 0 || _dataSource$variant_g2.forEach(function (variantGroupItem) {
      var _groupItemValue3;
      var groupId = variantGroupItem === null || variantGroupItem === void 0 ? void 0 : variantGroupItem.id;
      var groupItemValue = selectorValue === null || selectorValue === void 0 ? void 0 : selectorValue[groupId];
      if (!groupId || !groupItemValue) {
        return;
      }
      var _groupItemValue = null;
      if (Array.isArray(groupItemValue)) {
        _groupItemValue = groupItemValue;
      } else {
        _groupItemValue = [groupItemValue];
      }
      (_groupItemValue3 = _groupItemValue) === null || _groupItemValue3 === void 0 || _groupItemValue3.forEach(function (item) {
        if (item !== null && item !== void 0 && item.data) {
          var _item$data3, _item$data4, _item$data5;
          selectedVariantIds.push("".concat(item === null || item === void 0 || (_item$data3 = item.data) === null || _item$data3 === void 0 ? void 0 : _item$data3.variant_group_id, ":").concat(item === null || item === void 0 || (_item$data4 = item.data) === null || _item$data4 === void 0 ? void 0 : _item$data4.id));
          selectedVariantOptionString.push("".concat(variantGroupItem === null || variantGroupItem === void 0 ? void 0 : variantGroupItem.name, ":").concat(item === null || item === void 0 || (_item$data5 = item.data) === null || _item$data5 === void 0 ? void 0 : _item$data5.name));
        }
      });
    });
    if (selectedVariantIds !== null && selectedVariantIds !== void 0 && selectedVariantIds.length) {
      var _dataSource$variant;
      var filteredVariant = dataSource === null || dataSource === void 0 || (_dataSource$variant = dataSource.variant) === null || _dataSource$variant === void 0 ? void 0 : _dataSource$variant.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 (idItem) {
          return ids.includes(idItem);
        });
      });
      if (filteredVariant !== null && filteredVariant !== void 0 && filteredVariant.length) {
        var _filteredVariant$;
        transformValue.product_variant_id = (_filteredVariant$ = filteredVariant[0]) === null || _filteredVariant$ === void 0 ? void 0 : _filteredVariant$.id;
        transformValue.product_option_string = selectedVariantOptionString.join(', ');
      }
    }
  }
  console.log('transformSelectorValueToValue>>>>>', transformValue);
  return transformValue;
};