'use strict';

/**
 * Simplifies time amount converting large numeric values to small with larger unit, so it is easier to read
 * @param amount amount of time
 * @param unit time unit
 * @returns simplified time amount
 */
export function simplifyTimeAmount(amount: number, unit: 'ms' | 's' | 'm'): TimeAmount {
  if (unit === 'ms') {
    if (amount < 1000) {
      return {amount, unit, description: `${amount} ${amount === 1 ? 'millisecond' : 'milliseconds'}`};
    }
    amount /= 1000;
    unit = 's';
  }
  if (unit === 's') {
    if (amount < 60) {
      return {amount, unit, description: `${amount} ${amount === 1 ? 'second' : 'seconds'}`};
    }
    amount /= 60;
    unit = 'm';
  }
  return {amount, unit, description: `${amount} ${amount === 1 ? 'minute' : 'minutes'}`};
}

/** Time amount */
export type TimeAmount = {
  /** Amount */
  amount: number;
  /** Time unit */
  unit: 'ms' | 's' | 'm';
  /** Time amount text description */
  description: string;
};
