/**
 * locale.ts — stored-locale to BCP-47 formatting-tag expander.
 * (hebrew-locale-dates spec 117)
 *
 * The DB stores `user_preferences.locale` as 'he' | 'en' (UI language).
 * Intl.* APIs need a regional BCP-47 tag that encodes formatting conventions
 * (date separator, currency symbol placement, digit grouping).
 *
 * English-in-Israel always expands to 'en-IL' — Israeli Gregorian ('.'-separated)
 * and ₪ currency — NEVER to 'en-US'.
 */

/** UI-language locale values stored in user_preferences.locale. */
export type StoredLocale = 'he' | 'en'

/** BCP-47 regional formatting tags used by Intl.*. */
export type FormattingLocaleTag = 'he-IL' | 'en-IL'

/**
 * Map from stored UI-language value to BCP-47 regional formatting tag.
 * Reference this record in formatters instead of string literals.
 */
export const FORMATTING_LOCALE: Record<StoredLocale, FormattingLocaleTag> = {
  he: 'he-IL',
  en: 'en-IL',
} as const

/**
 * Expand the stored UI-language value to a BCP-47 regional formatting tag.
 *
 * - `'he'`  → `'he-IL'`  (Hebrew, Israeli regional: '.' separator, ₪ right-side)
 * - `'en'`  → `'en-IL'`  (English, Israeli regional: '.' separator, ₪ currency)
 * - `null` / `undefined` → `'en-IL'` (matches NULL locale semantics in DB)
 *
 * Never returns 'en-US'. All Israeli tenants use Israeli Gregorian conventions.
 *
 * @example
 * toFormattingLocale('he')   // 'he-IL'
 * toFormattingLocale('en')   // 'en-IL'
 * toFormattingLocale(null)   // 'en-IL'
 */
export function toFormattingLocale(
  storedLocale: StoredLocale | null | undefined,
): FormattingLocaleTag {
  return storedLocale === 'he' ? 'he-IL' : 'en-IL'
}
