const CURRENCY_FORMATTER = new Intl.NumberFormat(undefined, {
  currency: "EUR",
  style: "currency",
})
export function formatCurrency (number: number): string {
  return CURRENCY_FORMATTER.format(number)
}

const NUMBER_FORMATTER = new Intl.NumberFormat(undefined)
export function formatNumber (number: number): string {
  return NUMBER_FORMATTER.format(number)
}

const COMPACT_NUMBER_FORMATTER = new Intl.NumberFormat(undefined, {
  notation: "compact"
})
export function formatCompactNumber (number: number): string {
  return COMPACT_NUMBER_FORMATTER.format(number)
}

type DivisionsT = {
  amount: number
  name:
      "year" | "years" |
      "quarter" | "quarters" |
      "month" | "months" |
      "week" | "weeks" |
      "day" | "days" |
      "hour" | "hours" |
      "minute" | "minutes" |
      "second" | "seconds"
}
const DIVISTIONS: DivisionsT[] = [
  { amount: 60, name: "seconds" },
  { amount: 60, name: "minutes" },
  { amount: 24, name: "hours" },
  { amount: 7, name: "days" },
  { amount: 4.34524, name: "weeks" },
  { amount: 12, name: "months" },
  { amount: Number.POSITIVE_INFINITY, name: "years" },
]
const RELATIVE_DATE_FORMATTER = new Intl.RelativeTimeFormat(undefined, {
  numeric: "auto"
})
export function formatRelativeDate(toDate: Date, fromDate: Date = new Date()) {
  let duration = (toDate.getSeconds() - fromDate.getSeconds()) / 1000
  
  for (let i = 0; i <= DIVISTIONS.length; i++) {
    const division = DIVISTIONS[i]
    if (Math.abs(duration) < division.amount) {
      return RELATIVE_DATE_FORMATTER.format(Math.round(duration), division.name)
    }
    duration /= division.amount
  }
}
