import { L10nConfig } from 'angular-l10n';
import { AccountService } from 'src/app/services/account/account.service';

export interface ILanguageConfig {
  languageCode: string;
  countryCode: string;
  displayName: string;
  textFlow: string;
}

export const languageConfigs: ILanguageConfig[] = [
  { languageCode: 'en', countryCode: 'EN', displayName: 'English', textFlow: 'ltr' },
  { languageCode: 'de', countryCode: 'DE', displayName: 'Deutsch', textFlow: 'ltr' },
  { languageCode: 'fr', countryCode: 'FR', displayName: 'Français', textFlow: 'ltr' },
  { languageCode: 'nb', countryCode: 'NB', displayName: 'Norsk', textFlow: 'ltr' },
  { languageCode: 'zh', countryCode: 'ZH', displayName: '中文', textFlow: 'ltr' },
  { languageCode: 'ja', countryCode: 'JA', displayName: '日本語', textFlow: 'ltr' },
  { languageCode: 'ru', countryCode: 'RU', displayName: 'Pусский', textFlow: 'ltr' },
];

export const l10nConfig: L10nConfig = {
  format: 'language-region',
  providers: [{ name: 'app', asset: `../i18n`, options: { version: '1.0.0' } }],
  cache: true,
  keySeparator: '.',
  defaultLocale: { language: getDefaultLanguage() },
  schema: [
    { locale: { language: 'en', currency: 'USD' }, dir: 'ltr', text: 'United States' },
    { locale: { language: 'de', currency: 'EUR' }, dir: 'ltr', text: 'Deutschland' },
  ],
};

// Checking whether the prefered language found in local-storage is valid
function getDefaultLanguage(): string {
  const languagePreference = localStorage.getItem(AccountService.DEFAULT_LOCALE_STORAGE_KEY);
  const isValid = languageConfigs.some(c => c.languageCode === languagePreference);
  return isValid ? languagePreference : languageConfigs[0].languageCode;
}
