import { createI18n } from 'vue-i18n';
import { deepMerge } from './utilities/objectUtils';

export function loadBaseLocaleMessages() {
  const context = require.context(
    './locales',
    true,
    /[A-Za-z0-9-_,\s]+\.json$/i,
  );
  const messages = {};
  context.keys().forEach((key) => {
    const match = key.match(/([A-Za-z0-9-_]+)\.json$/i);
    if (!match) return;
    const locale = match[1];
    const mod = context(key);
    messages[locale] = mod && mod.default ? mod.default : mod;
  });
  return messages;
}

export function loadEnvLocaleMessages(envName) {
  if (!envName) return {};
  const envMessages = {};
  const envLocales = require.context(
    './env/locales',
    true,
    /[A-Za-z0-9-_,\s]+\.json$/i,
  );
  const vendorRoot = String(envName).split('-')[0];
  const candidates =
    vendorRoot && vendorRoot !== envName ? [vendorRoot, envName] : [envName];
  candidates.forEach((candidate) => {
    envLocales.keys().forEach((key) => {
      if (!key.includes(`/${candidate}/`)) return;
      const localeMatch = key.match(/([A-Za-z0-9-_]+)\.json$/i);
      if (!localeMatch) return;
      const locale = localeMatch[1];
      const mod = envLocales(key);
      const bundle = mod && mod.default ? mod.default : mod;
      envMessages[locale] = deepMerge(envMessages[locale] || {}, bundle);
    });
  });
  return envMessages;
}

export function createI18nInstance(
  envName,
  locale,
  loadEnv = loadEnvLocaleMessages,
  loadBase = loadBaseLocaleMessages,
) {
  const base = loadBase();
  const env = loadEnv(envName);
  const messages = { ...base };
  Object.keys(env).forEach((loc) => {
    messages[loc] = deepMerge(base[loc] || {}, env[loc]);
  });

  const addAlias = (alias, target) => {
    if (!messages[alias] && messages[target])
      messages[alias] = messages[target];
  };
  addAlias('en', 'en-US');
  addAlias('ru', 'ru-RU');
  addAlias('zh', 'zh-CN');
  addAlias('ka', 'ka-GE');

  const normalize = (val) => {
    if (!val) return undefined;
    const s = String(val);
    if (s === 'en') return 'en-US';
    if (s === 'ru') return 'ru-RU';
    if (s === 'zh') return 'zh-CN';
    if (s === 'ka') return 'ka-GE';
    return s;
  };

  return createI18n({
    locale: normalize(locale),
    // Locales that don't exist will fallback to English
    fallbackLocale: 'en-US',
    // Falling back to fallbackLocale generates two console warnings
    // Silent fallback suppresses console warnings when using fallback
    silentFallbackWarn: true,
    messages,
    globalInjection: false,
    legacy: false,
  });
}

const envName = process.env.VUE_APP_ENV_NAME;
// Get default locale from local storage
const stored = window.localStorage.getItem('storedLanguage');
export default createI18nInstance(envName, stored);
