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/Summary/utils.ts
var utils_exports = {};
__export(utils_exports, {
  calculateDeposit: () => calculateDeposit,
  calculatePriceDetails: () => calculatePriceDetails,
  calculateSubtotal: () => calculateSubtotal,
  calculateTaxFee: () => calculateTaxFee,
  formatCartsToSummaryModule: () => formatCartsToSummaryModule
});
module.exports = __toCommonJS(utils_exports);
var import_decimal = __toESM(require("decimal.js"));
var calculateSubtotal = (items) => {
  if (!(items == null ? void 0 : items.length)) {
    return "0.00";
  }
  const subtotal = items.reduce((sum, item) => {
    return sum.plus(item.total || 0);
  }, new import_decimal.default(0));
  return subtotal.toFixed(2);
};
var formatCartsToSummaryModule = (items) => {
  if (!Array.isArray(items) || !(items == null ? void 0 : items.length)) return [];
  return items.map((item) => {
    const { selectedBundle, selectedOptions, ...rest } = item;
    return {
      ...rest,
      bundle: selectedBundle || [],
      option: selectedOptions || []
    };
  });
};
var calculateTaxFee = (shopInfo, items, summaryModule) => {
  if (!(items == null ? void 0 : items.length)) {
    return "0.00";
  }
  const { is_price_include_tax, tax_rate } = shopInfo || {};
  console.log("shopInfo>>>", is_price_include_tax, tax_rate);
  const subtotal = new import_decimal.default(calculateSubtotal(items));
  const totalTax = summaryModule.getTaxforUtils(
    items,
    {
      tax_rate: Number(tax_rate),
      is_price_include_tax: Number(is_price_include_tax || 0)
    },
    subtotal
  );
  const totalTaxFee = totalTax == null ? void 0 : totalTax.tax;
  return totalTaxFee;
};
var calculatePriceDetails = (shopInfo, items, summaryModule) => {
  const subtotal = new import_decimal.default(calculateSubtotal(items));
  const _carts = formatCartsToSummaryModule(items);
  const surcharge = summaryModule.getSurchargeforUtils(_carts);
  const surchargeFee = new import_decimal.default((surcharge == null ? void 0 : surcharge.surchargeAmount) || 0);
  const totalTaxFee = new import_decimal.default(
    calculateTaxFee(shopInfo, _carts, summaryModule)
  );
  const taxRate = new import_decimal.default((shopInfo == null ? void 0 : shopInfo.tax_rate) || 0).div(100);
  let total = (shopInfo == null ? void 0 : shopInfo.is_price_include_tax) ? subtotal : subtotal.plus(totalTaxFee);
  total = total.plus(surchargeFee);
  const deposit = calculateDeposit(items);
  return {
    subtotal: subtotal.toFixed(2),
    total: total.toFixed(2),
    taxTitle: shopInfo == null ? void 0 : shopInfo.tax_title,
    totalTaxFee: totalTaxFee.toFixed(2),
    isPriceIncludeTax: shopInfo == null ? void 0 : shopInfo.is_price_include_tax,
    taxRate: taxRate.toNumber(),
    surcharge,
    deposit
  };
};
var calculateDeposit = (items) => {
  if (!(items == null ? void 0 : items.length)) {
    return void 0;
  }
  const protocols = [];
  let hasDeposit = false;
  const total = items.reduce((sum, item) => {
    var _a, _b, _c;
    if (item == null ? void 0 : item.deposit) {
      hasDeposit = true;
      const cartItemTotalPrice = new import_decimal.default(((_a = item == null ? void 0 : item.deposit) == null ? void 0 : _a.total) || 0);
      (_c = (_b = item == null ? void 0 : item.deposit) == null ? void 0 : _b.protocols) == null ? void 0 : _c.forEach((protocol) => {
        if (protocols.findIndex((p) => p.id === protocol.id) === -1) {
          protocols.push(protocol);
        }
      });
      return sum.plus(cartItemTotalPrice);
    }
    return sum;
  }, new import_decimal.default(0));
  if (hasDeposit) {
    return { total: total.toFixed(2), protocols, hasDeposit };
  }
  return void 0;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  calculateDeposit,
  calculatePriceDetails,
  calculateSubtotal,
  calculateTaxFee,
  formatCartsToSummaryModule
});
