import { memoize } from '@platform-modules/util/memo'

const _nf = memoize(
  (locale: string, options?: Intl.NumberFormatOptions) => new Intl.NumberFormat(locale, options),
)

const _dtf = memoize(
  (locale: string, options?: Intl.DateTimeFormatOptions) => new Intl.DateTimeFormat(locale, options),
)

export function formatNumber(locale: string, value: number, options?: Intl.NumberFormatOptions): string {
  return _nf(locale, options).format(value)
}

export function formatDate(locale: string, value: Date | number, options?: Intl.DateTimeFormatOptions): string {
  return _dtf(locale, options).format(value)
}

export function formatCurrency(locale: string, value: number, currency: string, options?: Intl.NumberFormatOptions): string {
  return _nf(locale, { style: 'currency', ...options, currency }).format(value)
}
