"use strict";
const _ = require("lodash");
const api = require("./api-BygscvSz.js");
const common = require("./common-B47boGYI.js");
function calculateAdditionalDiscountCrossValue(sourceValue, sourceField, total) {
  if (total <= 0 || sourceValue <= 0) return 0;
  if (sourceField === "percentage") {
    return Number((total * (sourceValue / 100)).toFixed(2));
  }
  return Number((sourceValue / total * 100).toFixed(2));
}
const calculateEntries = (items, expenses, additionalDiscountOn, additionalDiscountPercentage, providedAdditionalDiscountAmount, roundOff, roundOffAmount, promotionAmount) => {
  let totalQuantity = 0, totalDiscount = 0, totalTaxAmount = 0, totalAmountExcludingAll = 0, totalAmountIncludingAll = 0, additionalDiscountAmount = 0, totalGrossAmount = 0;
  if (items.length) {
    items.forEach((item) => {
      totalQuantity += Number((item == null ? void 0 : item.quantity) || 0);
      totalDiscount += Number((item == null ? void 0 : item.discount_amount) || 0);
      totalAmountIncludingAll += Number(
        item.total_amount || 0
      );
      totalTaxAmount += Number(item.tax_amount);
      totalAmountExcludingAll += Number(item.quantity || 0) * Number(item.rate || 0);
      totalGrossAmount += Number(item.gross_amount || 0);
    });
  }
  if (expenses == null ? void 0 : expenses.length) {
    expenses.forEach((item) => {
      totalQuantity += 1;
      totalDiscount += Number(item.discount_amount || 0);
      totalAmountIncludingAll += Number(
        item.total_amount || 0
      );
      totalTaxAmount += Number(item.tax_amount);
      totalAmountExcludingAll += 1 * Number(item.rate);
      totalGrossAmount += Number(item.gross_amount || 0);
    });
  }
  let finalAmount = totalAmountIncludingAll;
  if (additionalDiscountOn) {
    if (additionalDiscountOn === "Net Amount") {
      if (additionalDiscountPercentage && Number(additionalDiscountPercentage) !== 0) {
        additionalDiscountAmount = totalAmountIncludingAll * (additionalDiscountPercentage / 100);
      } else {
        additionalDiscountAmount = providedAdditionalDiscountAmount || 0;
      }
    } else if (additionalDiscountOn === "Gross Amount") {
      if (additionalDiscountPercentage && Number(additionalDiscountPercentage) !== 0) {
        additionalDiscountAmount = totalGrossAmount * (additionalDiscountPercentage / 100);
      } else {
        additionalDiscountAmount = providedAdditionalDiscountAmount || 0;
      }
    }
  }
  finalAmount = additionalDiscountOn === "Gross Amount" ? Number(totalAmountIncludingAll) - Number(additionalDiscountAmount) : Number(totalAmountIncludingAll) - Number(additionalDiscountAmount);
  if (Number(promotionAmount)) {
    finalAmount = finalAmount - Number(promotionAmount);
  }
  const actualTotalAmount = finalAmount;
  if (roundOff && roundOffAmount && Number(roundOffAmount)) {
    finalAmount = Math.round(finalAmount / roundOffAmount) * roundOffAmount;
  }
  const roundOffDifference = Number(finalAmount) - Number(actualTotalAmount);
  const totalItemsExpenseDiscount = totalDiscount;
  totalDiscount += Number(additionalDiscountAmount);
  return {
    totalQuantity,
    totalDiscount,
    totalItemsExpenseDiscount,
    totalTaxAmount,
    totalAmountExludingAll: totalAmountExcludingAll,
    totalAmountIncludingAll,
    additinoalDiscountAmount: additionalDiscountAmount,
    totalGrossAmount,
    actualTotalAmount,
    roundOffDifference,
    finalAmount
  };
};
const loadedModules = /* @__PURE__ */ new Set();
function nestTranslation(flatObject) {
  const nested = {};
  const keys = Object.keys(flatObject);
  const namespaceKeys = /* @__PURE__ */ new Set();
  keys.forEach((key) => {
    keys.forEach((otherKey) => {
      if (otherKey.startsWith(key + ".")) {
        namespaceKeys.add(key);
      }
    });
  });
  keys.forEach((key) => {
    if (!namespaceKeys.has(key)) {
      _.set(nested, key, flatObject[key]);
    }
  });
  return nested;
}
async function loadModuleTranslations(moduleNames, i18nInstance) {
  var _a;
  const modulesToLoad = (moduleNames == null ? void 0 : moduleNames.filter((m) => !loadedModules.has(m))) || [];
  if (!modulesToLoad.length) return;
  const filters = `(module.in=[${modulesToLoad == null ? void 0 : modulesToLoad.join(",")}])`;
  const response = await api.getV1LanguagesTranslations({
    filters,
    limit: 9999,
    ...common.getToken()
  });
  const data = ((_a = response == null ? void 0 : response.data) == null ? void 0 : _a.translations) || [];
  const resources = {};
  data.forEach((module2) => {
    var _a2;
    (_a2 = module2 == null ? void 0 : module2.fields) == null ? void 0 : _a2.forEach((item) => {
      for (const lang in item.translation) {
        if (!resources[lang]) {
          resources[lang] = {};
        }
        resources[lang][item.slug] = item.translation[lang];
      }
    });
  });
  if (i18nInstance) {
    Object.keys(resources).forEach((lang) => {
      const nested = nestTranslation(resources[lang]);
      i18nInstance.addResourceBundle(lang, "translation", nested, true, true);
    });
  }
  modulesToLoad.forEach((module2) => {
    loadedModules.add(module2);
  });
}
exports.calculateAdditionalDiscountCrossValue = calculateAdditionalDiscountCrossValue;
exports.calculateEntries = calculateEntries;
exports.loadModuleTranslations = loadModuleTranslations;
//# sourceMappingURL=translations-QALR4VUa.js.map
