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 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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
import Decimal from 'decimal.js';
import { cloneDeep } from 'lodash';
export var calculateOptionItem = function calculateOptionItem(params) {
  var _optionItemValue$data, _newOriginal, _newOriginal$forEach, _newOriginal2, _newOriginal3;
  var editIndex = params.editIndex,
    optionItemValue = params.optionItemValue,
    optionItemDataSource = params.optionItemDataSource,
    data = params.data;
  var currentData = (optionItemValue === null || optionItemValue === void 0 || (_optionItemValue$data = optionItemValue.data) === null || _optionItemValue$data === void 0 ? void 0 : _optionItemValue$data._data) || [];
  var uniqueKey = optionItemDataSource === null || optionItemDataSource === void 0 ? void 0 : optionItemDataSource._key;
  var newOriginal = cloneDeep((optionItemDataSource === null || optionItemDataSource === void 0 ? void 0 : optionItemDataSource._original) || {});
  if (data !== null && data !== void 0 && data.quantity) {
    if (editIndex === -1) {
      // 如果当前编辑的索引为-1，则表示是新增选项
      newOriginal._data = [].concat(_toConsumableArray(currentData), [_objectSpread({
        value: uniqueKey
      }, data)]);
    } else {
      // 如果当前编辑的索引不为-1，则表示是编辑选项
      currentData[editIndex] = _objectSpread({
        value: uniqueKey
      }, data);
      newOriginal._data = _toConsumableArray(currentData);
    }
  } else {
    // 如果数量为0，则执行删除逻辑
    if (editIndex !== -1) {
      currentData.splice(editIndex, 1);
      newOriginal._data = _toConsumableArray(currentData);
    }
  }

  // 计算新的quantity和price
  var totalQuantity = 0;
  var totalPrice = new Decimal(0);
  (_newOriginal = newOriginal) === null || _newOriginal === void 0 || (_newOriginal = _newOriginal._data) === null || _newOriginal === void 0 || (_newOriginal$forEach = _newOriginal.forEach) === null || _newOriginal$forEach === void 0 || _newOriginal$forEach.call(_newOriginal, function (item) {
    totalQuantity += (item === null || item === void 0 ? void 0 : item.quantity) || 0;
    var itemTotalPrice = new Decimal(0);
    // 判定item.data是否是Object，并且不是其他例如Array，Function，Date等
    if (Object.prototype.toString.call(item.data) === '[object Object]') {
      Object.keys(item.data).forEach(function (key) {
        var _item$data;
        var itemDataItem = (_item$data = item.data) === null || _item$data === void 0 ? void 0 : _item$data[key];
        console.log('newOriginal_item_key>>>>>', key, itemDataItem);
        var tempPrice = new Decimal(0);
        if (Array.isArray(itemDataItem)) {
          itemDataItem.forEach(function (_item) {
            var _item$data2, _item$data3;
            tempPrice = tempPrice.plus(new Decimal((_item === null || _item === void 0 || (_item$data2 = _item.data) === null || _item$data2 === void 0 ? void 0 : _item$data2.price) || 0).times(new Decimal((_item === null || _item === void 0 || (_item$data3 = _item.data) === null || _item$data3 === void 0 ? void 0 : _item$data3.quantity) || 0)));
          });
        } else {
          var _itemDataItem$data, _itemDataItem$data2;
          tempPrice = new Decimal((itemDataItem === null || itemDataItem === void 0 || (_itemDataItem$data = itemDataItem.data) === null || _itemDataItem$data === void 0 ? void 0 : _itemDataItem$data.price) || 0).times(new Decimal((itemDataItem === null || itemDataItem === void 0 || (_itemDataItem$data2 = itemDataItem.data) === null || _itemDataItem$data2 === void 0 ? void 0 : _itemDataItem$data2.quantity) || 0));
        }
        itemTotalPrice = itemTotalPrice.plus(tempPrice);
      });
    }
    totalPrice = totalPrice.plus(itemTotalPrice.times(new Decimal((item === null || item === void 0 ? void 0 : item.quantity) || 0)));
  });
  var _pricePrefix = ((_newOriginal2 = newOriginal) === null || _newOriginal2 === void 0 ? void 0 : _newOriginal2.price_type) === 'markdown' ? -1 : 1;
  totalPrice = totalPrice.plus(new Decimal(((_newOriginal3 = newOriginal) === null || _newOriginal3 === void 0 ? void 0 : _newOriginal3.price) || 0).times(new Decimal(totalQuantity || 0)).times(_pricePrefix));
  var totalPriceNumber = totalPrice.toNumber();
  newOriginal = _objectSpread(_objectSpread({}, newOriginal), {}, {
    quantity: totalQuantity,
    num: totalQuantity
  });
  return [newOriginal, totalPriceNumber, totalQuantity];
};