/**
 * Generates an array containing each number from 0 up to and excluding n in sequence.
 * Works like Python's range() function when using a single argument.
 */
export function range(n: number): number[] {
  return Array.from(Array(n).keys());
}

/**
 * Converts a given number of milliseconds into a human-readable string representation of time.
 *
 * @param {number} milliseconds - The number of milliseconds to be formatted.
 * @param {number}
 * @returns {string} The formatted time string in years, months, days, hours, minutes, and seconds.
 */
export function formatTime(milliseconds: number, precision: number = 3): string {
  const seconds = Math.round(milliseconds / 1000);

  if (seconds <= 0) {
    return '0 seconds';
  }

  const years = Math.floor(seconds / (365 * 24 * 60 * 60));
  const months = Math.floor((seconds % (365 * 24 * 60 * 60)) / (30 * 24 * 60 * 60));
  const days = Math.floor((seconds % (30 * 24 * 60 * 60)) / (24 * 60 * 60));
  const hours = Math.floor((seconds % (24 * 60 * 60)) / (60 * 60));
  const minutes = Math.floor((seconds % (60 * 60)) / 60);
  const remainingSeconds = seconds % 60;

  const timeParts = [];
  if (years > 0) {
    timeParts.push(`${years} year${years > 1 ? 's' : ''}`);
  }
  if (months > 0) {
    timeParts.push(`${months} month${months > 1 ? 's' : ''}`);
  }
  if (days > 0) {
    timeParts.push(`${days} day${days > 1 ? 's' : ''}`);
  }
  if (hours > 0) {
    timeParts.push(`${hours} hour${hours > 1 ? 's' : ''}`);
  }
  if (minutes > 0) {
    timeParts.push(`${minutes} minute${minutes > 1 ? 's' : ''}`);
  }
  if (remainingSeconds > 0) {
    timeParts.push(`${remainingSeconds} second${remainingSeconds > 1 ? 's' : ''}`);
  }

  return timeParts.slice(0, precision).join(', ');
}
