// Reactive i18n layer. The locale is a rune, so any component that calls t()
// re-renders when the locale changes. The catalog + lookup are pure (see catalog.ts).

import { translate, type Locale, type MessageKey } from './catalog';

export { errorKey, localeFrom } from './catalog';
export type { Locale, MessageKey };

export const i18n = $state<{ locale: Locale }>({ locale: 'en' });

export function setLocale(locale: Locale): void {
  i18n.locale = locale;
}

export function t(key: MessageKey, params?: Record<string, string | number>): string {
  return translate(i18n.locale, key, params);
}
