import { i18n } from '@lingui/core';
import { en, vi } from 'make-plural/plurals';
import { LOCALE_KEY } from './consts';

export const DEFAULT_LOCALE = 'en';

i18n.loadLocaleData({
  en: { plurals: en },
  vi: { plurals: vi },
});

export async function dynamicActivate(locale: string) {
  const messages = await import(`./locales/${locale}/messages.ts`).then(
    (p) => p.messages,
  );
  i18n.load(locale, messages);
  i18n.activate(locale);
}

export function getLocale() {
  try {
    const localeStorage = localStorage.getItem(LOCALE_KEY);
    return JSON.parse(localeStorage || '') as string;
  } catch (error) {
    return DEFAULT_LOCALE;
  }
}
