var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/Sales/Cart/utils.ts
var utils_exports = {};
__export(utils_exports, {
  formatBundleToOrigin: () => formatBundleToOrigin,
  formatOptionsToOrigin: () => formatOptionsToOrigin,
  getOriginTotalPrice: () => getOriginTotalPrice,
  getTotalPrice: () => getTotalPrice,
  handleCartItemOrigin: () => handleCartItemOrigin,
  handleProductInfo: () => handleProductInfo
});
module.exports = __toCommonJS(utils_exports);
var handleProductInfo = (eventDetail, num) => {
  var _a;
  const { productDetail, bookingOptionItem } = eventDetail;
  const normalProduct = bookingOptionItem == null ? void 0 : bookingOptionItem._originData;
  let totalPrice = Number((normalProduct == null ? void 0 : normalProduct.price) || "");
  let originTotal = Number((normalProduct == null ? void 0 : normalProduct.original_price) || "");
  let is_charge_tax = normalProduct == null ? void 0 : normalProduct.is_charge_tax;
  if (productDetail) {
    const { origin, bundle, options, product_variant_id } = productDetail;
    let variant = null;
    if (product_variant_id) {
      variant = (_a = origin == null ? void 0 : origin.variant) == null ? void 0 : _a.find(
        (item) => (item == null ? void 0 : item.id) === product_variant_id
      );
      is_charge_tax = variant == null ? void 0 : variant.is_charge_tax;
    }
    const params = { product: origin, variant, bundle, options, num };
    totalPrice = getTotalPrice(params);
    originTotal = getOriginTotalPrice(params);
  }
  return { totalPrice, originTotal, is_charge_tax };
};
var getTotalPrice = (params) => {
  const { product, bundle, options, variant } = params;
  const num = params.num || 1;
  let price = Number(product.price);
  if (variant) {
    price = Number(variant == null ? void 0 : variant.price);
  }
  if (bundle == null ? void 0 : bundle.length) {
    price = bundle.reduce((accumulator, currentValue) => {
      return accumulator + Number(currentValue.price) * Number(currentValue.num);
    }, price);
  }
  if (options == null ? void 0 : options.length) {
    price = options.reduce((accumulator, currentValue) => {
      return accumulator + Number(currentValue.price) * Number(currentValue.num);
    }, price);
  }
  return price * num;
};
var getOriginTotalPrice = (params) => {
  const { product, bundle, options, variant } = params;
  const num = (params == null ? void 0 : params.num) || 1;
  let price = Number(product.original_price);
  if (variant) {
    price = Number(variant == null ? void 0 : variant.original_price);
  }
  if (isNaN(price) || price === 0) {
    return void 0;
  }
  if (bundle == null ? void 0 : bundle.length) {
    price = bundle.reduce((accumulator, currentValue) => {
      return accumulator + Number(currentValue.price) * Number(currentValue.num);
    }, price);
  }
  if (options == null ? void 0 : options.length) {
    price = options.reduce((accumulator, currentValue) => {
      return accumulator + Number(currentValue.price) * Number(currentValue.num);
    }, price);
  }
  return price * num;
};
var handleCartItemOrigin = (cartItem, eventDetail) => {
  const { productDetail } = eventDetail;
  const originData = (cartItem == null ? void 0 : cartItem._originData) || {};
  if (productDetail) {
    const { bundle, options, product_variant_id } = productDetail;
    if (!originData.product) {
      originData.product = {};
    }
    if (product_variant_id) {
      originData.product.product_variant_id = product_variant_id;
    }
    if (bundle == null ? void 0 : bundle.length) {
      originData.product.product_bundle = formatBundleToOrigin(bundle);
    }
    if (options == null ? void 0 : options.length) {
      originData.product.product_option_item = formatOptionsToOrigin(options);
    }
  }
  console.log("originData>>>>>>", originData);
  return originData;
};
var formatBundleToOrigin = (bundle) => {
  if (!(bundle == null ? void 0 : bundle.length)) return [];
  return bundle.map((d) => {
    return {
      bundle_group_id: d.group_id,
      bundle_id: d.id,
      bundle_product_id: (d == null ? void 0 : d._bundle_product_id) || (d == null ? 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)
    };
  });
};
var formatOptionsToOrigin = (options) => {
  if (!(options == null ? void 0 : options.length)) {
    return [];
  }
  return options.map((d) => {
    return {
      num: d.num,
      option_group_item_id: d.product_option_item_id,
      option_group_id: d.option_group_id
    };
  });
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  formatBundleToOrigin,
  formatOptionsToOrigin,
  getOriginTotalPrice,
  getTotalPrice,
  handleCartItemOrigin,
  handleProductInfo
});
