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 = ["selectedBundle", "selectedOptions"];
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';
/**
 * 计算商品小计（不含其他费用）
 * @param items - 购物车商品数组
 * @returns 商品总价字符串，保留2位小数
 */
export var calculateSubtotal = function calculateSubtotal(items) {
  if (!(items !== null && items !== void 0 && items.length)) {
    return '0.00';
  }
  var subtotal = items.reduce(function (sum, item) {
    // const price = new Decimal(item.price || 0);
    // const quantity = new Decimal(item.quantity || 1);
    // return sum.plus(price.times(quantity));
    return sum.plus(item.total || 0);
  }, new Decimal(0));
  return subtotal.toFixed(2);
};

/**
 * @description: 将购物车数据格式化成summaryModule需要的格式
 * @param {any} items 购物车数据
 * @return {*}
 */
export var formatCartsToSummaryModule = function formatCartsToSummaryModule(items) {
  if (!Array.isArray(items) || !(items !== null && items !== void 0 && items.length)) return [];
  return items.map(function (item) {
    var selectedBundle = item.selectedBundle,
      selectedOptions = item.selectedOptions,
      rest = _objectWithoutProperties(item, _excluded);
    return _objectSpread(_objectSpread({}, rest), {}, {
      bundle: selectedBundle || [],
      option: selectedOptions || []
    });
  });
};

/**
 * @title: 单个商品的税费
 * @description:
 * 单个商品的税费 = 商品销售单价（折扣后） * 税率 * is_charge_tax /( 1+ 税率 * is_price_include_tax)
 * $taxFee = $price * $taxRate * $isChargeTax / (1 + $taxRate * $isPriceIncludeTax);
 * @return {*}
 * @Author: xiangfeng.xue
 */
export var calculateTaxFee = function calculateTaxFee(shopInfo, items, summaryModule) {
  if (!(items !== null && items !== void 0 && items.length)) {
    return '0.00';
  }
  var _ref = shopInfo || {},
    is_price_include_tax = _ref.is_price_include_tax,
    tax_rate = _ref.tax_rate;
  console.log('shopInfo>>>', is_price_include_tax, tax_rate);
  // const totalTaxFee = items.reduce((sum, item) => {
  //   const price = new Decimal(item.price || 0);
  //   const taxRate = new Decimal(tax_rate || 0).div(100);
  //   const productTaxRate = price
  //     .times(taxRate)
  //     .times(item?.is_charge_tax || 0)
  //     .div(taxRate.times(is_price_include_tax || 0).plus(1));

  //   return sum.plus(productTaxRate);
  // }, new Decimal(0));
  var subtotal = new Decimal(calculateSubtotal(items));
  var totalTax = summaryModule.getTaxforUtils(items, {
    tax_rate: Number(tax_rate),
    is_price_include_tax: Number(is_price_include_tax || 0)
  }, subtotal);
  var totalTaxFee = totalTax === null || totalTax === void 0 ? void 0 : totalTax.tax;
  return totalTaxFee;
};

/**
 * 计算所有价格明细
 * @param items - 购物车商品数组
 * @param options - 其他费用配置
 * @returns 价格明细对象
 */
export var calculatePriceDetails = function calculatePriceDetails(shopInfo, items, summaryModule) {
  // 小计
  var subtotal = new Decimal(calculateSubtotal(items));
  var _carts = formatCartsToSummaryModule(items);
  // 附加费
  var surcharge = summaryModule.getSurchargeforUtils(_carts);
  var surchargeFee = new Decimal((surcharge === null || surcharge === void 0 ? void 0 : surcharge.surchargeAmount) || 0);
  // 税费
  var totalTaxFee = new Decimal(calculateTaxFee(shopInfo, _carts, summaryModule));
  // 税率
  var taxRate = new Decimal((shopInfo === null || shopInfo === void 0 ? void 0 : shopInfo.tax_rate) || 0).div(100);

  // 计算总价
  var total = shopInfo !== null && shopInfo !== void 0 && shopInfo.is_price_include_tax ? subtotal : subtotal.plus(totalTaxFee);
  total = total.plus(surchargeFee);

  // 计算定金
  var deposit = calculateDeposit(items);
  return {
    subtotal: subtotal.toFixed(2),
    total: total.toFixed(2),
    taxTitle: shopInfo === null || shopInfo === void 0 ? void 0 : shopInfo.tax_title,
    totalTaxFee: totalTaxFee.toFixed(2),
    isPriceIncludeTax: shopInfo === null || shopInfo === void 0 ? void 0 : shopInfo.is_price_include_tax,
    taxRate: taxRate.toNumber(),
    surcharge: surcharge,
    deposit: deposit
  };
};

/**
 * @title: 计算定金
 * @param items - 购物车商品数组
 * @returns 定金字符串，保留2位小数
 */
export var calculateDeposit = function calculateDeposit(items) {
  if (!(items !== null && items !== void 0 && items.length)) {
    return undefined;
  }
  var protocols = [];
  var hasDeposit = false;
  var total = items.reduce(function (sum, item) {
    if (item !== null && item !== void 0 && item.deposit) {
      var _item$deposit, _item$deposit2;
      hasDeposit = true;
      var cartItemTotalPrice = new Decimal((item === null || item === void 0 || (_item$deposit = item.deposit) === null || _item$deposit === void 0 ? void 0 : _item$deposit.total) || 0);
      // 将协议数据去重合并
      item === null || item === void 0 || (_item$deposit2 = item.deposit) === null || _item$deposit2 === void 0 || (_item$deposit2 = _item$deposit2.protocols) === null || _item$deposit2 === void 0 || _item$deposit2.forEach(function (protocol) {
        if (protocols.findIndex(function (p) {
          return p.id === protocol.id;
        }) === -1) {
          protocols.push(protocol);
        }
      });
      return sum.plus(cartItemTotalPrice);
    }
    return sum;
  }, new Decimal(0));
  if (hasDeposit) {
    return {
      total: total.toFixed(2),
      protocols: protocols,
      hasDeposit: hasDeposit
    };
  }
  return undefined;
};