import { createI18n } from 'vue-i18n';
import en from './en';
import fr from './fr';

export type AppLocale = 'en' | 'fr';

const STORAGE_KEY = 'arboretum.locale';

function initialLocale(): AppLocale {
  const saved = localStorage.getItem(STORAGE_KEY);
  return saved === 'fr' || saved === 'en' ? saved : 'en';
}

export const i18n = createI18n({
  legacy: false,
  locale: initialLocale(),
  fallbackLocale: 'en',
  messages: { en, fr },
});

export function setLocale(locale: AppLocale): void {
  i18n.global.locale.value = locale;
  localStorage.setItem(STORAGE_KEY, locale);
}
