export var handleProductInfo = function handleProductInfo(eventDetail, num) {
  var productDetail = eventDetail.productDetail,
    bookingOptionItem = eventDetail.bookingOptionItem;
  var normalProduct = bookingOptionItem === null || bookingOptionItem === void 0 ? void 0 : bookingOptionItem._originData;
  var totalPrice = Number((normalProduct === null || normalProduct === void 0 ? void 0 : normalProduct.price) || '');
  var originTotal = Number((normalProduct === null || normalProduct === void 0 ? void 0 : normalProduct.original_price) || '');
  var is_charge_tax = normalProduct === null || normalProduct === void 0 ? void 0 : normalProduct.is_charge_tax;
  if (productDetail) {
    var origin = productDetail.origin,
      bundle = productDetail.bundle,
      options = productDetail.options,
      product_variant_id = productDetail.product_variant_id;
    var variant = null;

    // 如果是组合规格，需要单独处理
    if (product_variant_id) {
      var _origin$variant, _variant;
      variant = origin === null || origin === void 0 || (_origin$variant = origin.variant) === null || _origin$variant === void 0 ? void 0 : _origin$variant.find(function (item) {
        return (item === null || item === void 0 ? void 0 : item.id) === product_variant_id;
      });
      is_charge_tax = (_variant = variant) === null || _variant === void 0 ? void 0 : _variant.is_charge_tax;
    }
    var params = {
      product: origin,
      variant: variant,
      bundle: bundle,
      options: options,
      num: num
    };
    totalPrice = getTotalPrice(params);
    originTotal = getOriginTotalPrice(params);
  }
  return {
    totalPrice: totalPrice,
    originTotal: originTotal,
    is_charge_tax: is_charge_tax
  };
};

/**
 * 获取商品总价
 * @description 当前总价计算基于商品数量为1
 * @param item 商品
 * @returns 商品总价
 */
export var getTotalPrice = function getTotalPrice(params) {
  var product = params.product,
    bundle = params.bundle,
    options = params.options,
    variant = params.variant;
  var num = params.num || 1;
  var price = Number(product.price);

  // 组合规格
  if (variant) {
    price = Number(variant === null || variant === void 0 ? void 0 : variant.price);
  }

  // 套餐
  if (bundle !== null && bundle !== void 0 && bundle.length) {
    price = bundle.reduce(function (accumulator, currentValue) {
      return accumulator + Number(currentValue.price) * Number(currentValue.num);
    }, price);
  }

  // 单规格
  if (options !== null && options !== void 0 && options.length) {
    price = options.reduce(function (accumulator, currentValue) {
      return accumulator + Number(currentValue.price) * Number(currentValue.num);
    }, price);
  }
  return price * num;
};

/**
 * 获取商品原始总价
 * @description 当前总价计算基于商品数量为1
 * @param item 商品
 * @returns 商品原始总价
 */
export var getOriginTotalPrice = function getOriginTotalPrice(params) {
  var product = params.product,
    bundle = params.bundle,
    options = params.options,
    variant = params.variant;
  var num = (params === null || params === void 0 ? void 0 : params.num) || 1;
  var price = Number(product.original_price);

  // 组合规格
  if (variant) {
    price = Number(variant === null || variant === void 0 ? void 0 : variant.original_price);
  }
  if (isNaN(price) || price === 0) {
    return undefined;
  }

  // 套餐
  if (bundle !== null && bundle !== void 0 && bundle.length) {
    price = bundle.reduce(function (accumulator, currentValue) {
      return accumulator + Number(currentValue.price) * Number(currentValue.num);
    }, price);
  }

  // 单规格
  if (options !== null && options !== void 0 && options.length) {
    price = options.reduce(function (accumulator, currentValue) {
      return accumulator + Number(currentValue.price) * Number(currentValue.num);
    }, price);
  }
  return price * num;
};
export var handleCartItemOrigin = function handleCartItemOrigin(cartItem, eventDetail) {
  var productDetail = eventDetail.productDetail;
  var originData = (cartItem === null || cartItem === void 0 ? void 0 : cartItem._originData) || {};
  if (productDetail) {
    var bundle = productDetail.bundle,
      options = productDetail.options,
      product_variant_id = productDetail.product_variant_id;
    if (!originData.product) {
      originData.product = {};
    }
    if (product_variant_id) {
      originData.product.product_variant_id = product_variant_id;
    }
    if (bundle !== null && bundle !== void 0 && bundle.length) {
      originData.product.product_bundle = formatBundleToOrigin(bundle);
    }
    if (options !== null && options !== void 0 && options.length) {
      originData.product.product_option_item = formatOptionsToOrigin(options);
    }
  }
  console.log('originData>>>>>>', originData);
  return originData;
};
export var formatBundleToOrigin = function formatBundleToOrigin(bundle) {
  if (!(bundle !== null && bundle !== void 0 && bundle.length)) return [];
  return bundle.map(function (d) {
    return {
      bundle_group_id: d.group_id,
      bundle_id: d.id,
      bundle_product_id: (d === null || d === void 0 ? void 0 : d._bundle_product_id) || (d === null || d === void 0 ? void 0 : d.product_id),
      bundle_variant_id: d.bundle_variant_id,
      num: d.num,
      extension_id: d.extension_id,
      extension_type: d.extension_type,
      option: formatOptionsToOrigin(d.option)
    };
  });
};
export var formatOptionsToOrigin = function formatOptionsToOrigin(options) {
  if (!(options !== null && options !== void 0 && options.length)) {
    return [];
  }
  return options.map(function (d) {
    return {
      num: d.num,
      option_group_item_id: d.product_option_item_id,
      option_group_id: d.option_group_id
    };
  });
};