var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/Sales/Cart/utils.ts
var utils_exports = {};
__export(utils_exports, {
  calculateProductDeposit: () => calculateProductDeposit,
  formatBundleToOrigin: () => formatBundleToOrigin,
  formatOptionsToOrigin: () => formatOptionsToOrigin,
  getOriginTotalPrice: () => getOriginTotalPrice,
  getPrice: () => getPrice,
  getProductDeposit: () => getProductDeposit,
  getTotalPrice: () => getTotalPrice,
  handleCartItemOrigin: () => handleCartItemOrigin,
  handleProductDeposit: () => handleProductDeposit,
  handleProductInfo: () => handleProductInfo
});
module.exports = __toCommonJS(utils_exports);
var import_decimal = __toESM(require("decimal.js"));
var handleProductInfo = (eventDetail, num) => {
  var _a;
  const { productDetail, bookingOptionItem } = eventDetail;
  const normalProduct = bookingOptionItem == null ? void 0 : bookingOptionItem._originData;
  let price = Number((normalProduct == null ? void 0 : normalProduct.price) || 0);
  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 };
    price = getPrice(params);
    totalPrice = getTotalPrice(params);
    originTotal = getOriginTotalPrice(params);
  }
  return { price, totalPrice, originTotal, is_charge_tax };
};
var getPrice = (params) => {
  const { product, variant } = params;
  let price = Number(product.price);
  if (variant) {
    price = Number(variant == null ? void 0 : variant.price);
  }
  return price;
};
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
    };
  });
};
var handleProductDeposit = (cartItem, eventDetail, num) => {
  var _a;
  const { productDetail, bookingOptionItem } = eventDetail;
  const normalProduct = bookingOptionItem == null ? void 0 : bookingOptionItem._originData;
  if (!productDetail && normalProduct) {
    const { custom_deposit_data } = normalProduct;
    if ((custom_deposit_data == null ? void 0 : custom_deposit_data.has_deposit) == 1) {
      return getProductDeposit({ cartItem, product: normalProduct, num });
    } else {
      return null;
    }
  }
  if (!productDetail) {
    return null;
  }
  const { origin, bundle, options, product_variant_id } = productDetail || {};
  let deposit = null;
  const product = origin;
  if (product_variant_id) {
    product.product_variant_id = product_variant_id;
  }
  if (((_a = origin == null ? void 0 : origin.custom_deposit_data) == null ? void 0 : _a.has_deposit) == 1) {
    deposit = getProductDeposit({ cartItem, product, bundle, options, num });
  }
  return deposit;
};
var getProductDeposit = (params) => {
  var _a, _b;
  const { cartItem, product, bundle, options } = params;
  const num = (params == null ? void 0 : params.num) || 1;
  if (((_a = product == null ? void 0 : product.custom_deposit_data) == null ? void 0 : _a.has_deposit) == 1) {
    let total = new import_decimal.default(0);
    const {
      deposit_fixed,
      deposit_percentage,
      deposit_policy_data,
      self_deposit_policy_ids
    } = (product == null ? void 0 : product.custom_deposit_data) || {};
    const allProtocols = deposit_policy_data || [];
    let productHasDeposit = false;
    const protocolIds = [];
    if (typeof deposit_fixed === "string" && typeof deposit_percentage === "string") {
      const { depositTotal, result } = calculateProductDeposit({
        depositData: product == null ? void 0 : product.custom_deposit_data,
        total: (cartItem == null ? void 0 : cartItem.total) || 0,
        num
      });
      if (result) {
        total = depositTotal;
        protocolIds.push(...self_deposit_policy_ids || []);
        productHasDeposit = true;
      }
    } else {
      if (product == null ? void 0 : product.product_variant_id) {
        const variantProduct = (_b = product == null ? void 0 : product.variant) == null ? void 0 : _b.find(
          (item) => (item == null ? void 0 : item.id) === (product == null ? void 0 : product.product_variant_id)
        );
        if (variantProduct) {
          const depositData = variantProduct == null ? void 0 : variantProduct.custom_deposit_data;
          const { depositTotal, result } = calculateProductDeposit({
            depositData,
            total: (cartItem == null ? void 0 : cartItem.total) || 0,
            num
          });
          if (result) {
            total = depositTotal;
            protocolIds.push(...(depositData == null ? void 0 : depositData.self_deposit_policy_ids) || []);
            productHasDeposit = true;
          }
        }
      }
      if (bundle == null ? void 0 : bundle.length) {
        total = bundle.reduce((accumulator, currBundleProduct) => {
          const depositData = currBundleProduct == null ? void 0 : currBundleProduct.custom_deposit_data;
          const { depositTotal, result } = calculateProductDeposit({
            depositData,
            total: currBundleProduct == null ? void 0 : currBundleProduct.price,
            num: (currBundleProduct == null ? void 0 : currBundleProduct.num) || 1
          });
          if (result) {
            protocolIds.push(...(depositData == null ? void 0 : depositData.self_deposit_policy_ids) || []);
            productHasDeposit = true;
            return accumulator.plus(depositTotal);
          }
          return accumulator;
        }, new import_decimal.default(0));
      }
    }
    if (productHasDeposit) {
      const ids = new Set(protocolIds);
      const protocols = allProtocols.filter((item) => ids.has(item.id));
      return { total: total.toNumber(), protocols };
    }
    return null;
  }
  return null;
};
var calculateProductDeposit = (params) => {
  const { depositData, total, num } = params;
  if (!depositData) {
    return { result: false, depositTotal: new import_decimal.default(0) };
  }
  const { deposit_fixed, deposit_percentage } = depositData || {};
  if (typeof deposit_fixed === "string" && typeof deposit_percentage === "string") {
    const depositFixed = new import_decimal.default(deposit_fixed || 0);
    const depositPercent = new import_decimal.default(deposit_percentage || 0);
    const productTotalPrice = new import_decimal.default(total || 0);
    const deposit = depositFixed.plus(depositPercent.times(productTotalPrice));
    return { depositTotal: deposit.times(num), result: true };
  }
  return { result: false, depositTotal: new import_decimal.default(0) };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  calculateProductDeposit,
  formatBundleToOrigin,
  formatOptionsToOrigin,
  getOriginTotalPrice,
  getPrice,
  getProductDeposit,
  getTotalPrice,
  handleCartItemOrigin,
  handleProductDeposit,
  handleProductInfo
});
