import { NgModule, APP_INITIALIZER } from '@angular/core';
import { Title } from '@angular/platform-browser';

import {
  L10nConfig,
  L10nLoader,
  LocalizationModule,
  LocaleValidationModule,
  StorageStrategy,
  ProviderType
} from 'angular-l10n';

const l10nConfig: L10nConfig = {
  locale: {
      languages: [
          { code: 'en', dir: 'ltr' },
          { code: 'kr', dir: 'ltr' },
      ],
      defaultLocale: { languageCode: 'en', countryCode: 'US' },
      storage: StorageStrategy.Cookie,
      cookieExpiration: 30
  },
  translation: {
      providers: [
          { type: ProviderType.Static, prefix: './assets/translations/of-' }
      ],
      caching: true,
      composedKeySeparator: '.',
      missingValue: 'No key',
      i18nPlural: true
  }
};

export function initL10n(l10nLoader: L10nLoader): Function {
  return () => l10nLoader.load();
}

@NgModule({
  imports: [
    LocalizationModule.forRoot(l10nConfig),
    LocaleValidationModule.forRoot()
  ],
  exports: [
    LocalizationModule,
    LocaleValidationModule,
  ],
  providers: [
    Title,
    {
      provide: APP_INITIALIZER,
      useFactory: initL10n,
      deps: [L10nLoader],
      multi: true
    }
  ],
})
export class AppL10NModule { }
