import { formatCurrency, formatNumber } from '@carto/ps-utils'
import { formatterTypes } from './const'
import type { Formatters, Value } from './types'

export const FORMATTERS: Formatters = {
  [formatterTypes.currency]: {
    type: formatterTypes.currency,
    format: (value: Value, locale?: string) => {
      return formatCurrency(value as number, locale ?? 'en-US')
    },
  },
  [formatterTypes.number]: {
    type: formatterTypes.number,
    format: (value: Value, locale?: string) => {
      return formatNumber(value as number, locale ?? 'en-US')
    },
  },
  [formatterTypes.default]: {
    type: formatterTypes.default,
    format: (value: Value, locale?: string) => {
      return value.toLocaleString(locale)
    },
  },
}
