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

export type PluralForms = Partial<Record<Intl.LDMLPluralRule, string>>

const _pr = memoize((locale: string) => new Intl.PluralRules(locale))

export function plural(locale: string, count: number, forms: PluralForms): string {
  const rule = _pr(locale).select(count)
  const tpl = forms[rule] ?? forms.other ?? ''
  return tpl.replace(/\{count\}/g, String(count))
}
