import { provideHttpClient } from '@angular/common/http';
import {
  ApplicationConfig,
  inject,
  isDevMode,
  provideAppInitializer,
  provideBrowserGlobalErrorListeners,
} from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideTransloco, TranslocoService } from '@jsverse/transloco';
import { forkJoin, lastValueFrom } from 'rxjs';
import { availableLangs } from '../../transloco.config';
import { routes } from './app.routes';
import { provideAurora } from './aurora.provider';
import { TranslocoHttpLoader } from './transloco-loader';

export const appConfig: ApplicationConfig = {
  providers: [
    provideBrowserGlobalErrorListeners(),
    provideRouter(routes),
    provideHttpClient(),
    provideTransloco({
      config: {
        availableLangs: [...availableLangs],
        // TODO: set when we have iam account management ready
        defaultLang: 'es',
        reRenderOnLangChange: true,
        prodMode: !isDevMode(),
        scopes: { keepCasing: true },
      },
      loader: TranslocoHttpLoader,
    }),

    // Eagerly load the default language and navigation scope at app startup
    provideAppInitializer(() => {
      const transloco = inject(TranslocoService);
      const lang = transloco.getDefaultLang();
      return lastValueFrom(forkJoin([transloco.load(lang), transloco.load(`navigation/${lang}`)]));
    }),

    /* #region customizations */
    provideAurora(),
    /* #endregion customizations */
  ],
};
