/**
 * Formats a number as a locale-specific amount string.
 * Uses Argentine Spanish locale for consistent formatting.
 *
 * @param value - The number to format
 * @returns The locale-formatted string
 *
 * @example
 * formatAmount(1234.56) // returns "1.234,56" (in es-AR locale)
 */
export const formatAmount = (value: number): string => {
  return value.toLocaleString('es-AR');
};

export const roundNumber = (value: number) => Math.round(value);

export const formatPercentage = (value: number) => `${value}%`;

export const distributeRoundedPercentages = (values: number[]): number[] => {
  const total = values.reduce((sum, v) => sum + v, 0);
  if (total === 0) return values.map(() => 0);

  const percentages = values.map(v => (v / total) * 100);
  const floored = percentages.map(Math.floor);
  const remainders = percentages.map((p, i) => p - floored[i]);

  let leftover = 100 - floored.reduce((sum, v) => sum + v, 0);

  const indices = remainders
    .map((r, i) => ({ r, i }))
    .sort((a, b) => b.r - a.r)
    .map(({ i }) => i);

  for (let k = 0; k < leftover; k++) {
    floored[indices[k]]++;
  }

  return floored;
};
