import { createI18n } from 'vue-i18n'
import {setHtmlPageLang} from './helper'
import {lang as en}  from './lang/en-us'
import {lang as da}  from './lang/da-dk'
import {lang as pt}  from './lang/pt-pt'
import {lang as de}  from './lang/dn-de'
import {lang as st}  from './lang/st-st'
export const loadLang = () => {
  const modules: Record<string, any> = import.meta.glob('./lang/*.ts', { eager: true });
  const langs: Record<string, any> = {};
  
  for (const path in modules) {
    const name = path.replace(/(\.\/lang\/|\.ts)/g, '');
    if (name == 'lang-base') {
      continue
    }
    langs[name] = modules[path].lang;
  }
  return langs;
}

export const setLang = (locale?: string) => {
  if (locale) {
    localStorage.setItem('lang', locale);
  }
  i18n.global.locale.value = locale || localStorage.getItem('lang') || '';
  setHtmlPageLang(i18n.global.locale.value)
}

export const i18n = createI18n({
  globalInjection: true,
  legacy: false,
  locale: import.meta.env.VITE_DEFAULT_LANG,
  fallbackLocale: import.meta.env.VITE_DEFAULT_LANG,
  fallbackWarn: false,
  messages: {
    'en-us': en,
    'da-dk': da,
    'pt-pt': pt,
    'dn-de': de,
    'st-st': st
  }
});
