import { APP_INITIALIZER, ApplicationConfig, importProvidersFrom } from '@angular/core';
import { provideRouter } from '@angular/router';

import { routes } from './app.routes';
import { AppStarterService } from './app-starter.service';

import { TranslateModule } from '@ngx-translate/core';

import { HTTP_INTERCEPTORS, provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import {
    CachePreventionInterceptor,
    CorsSecurityInterceptor,
    CsrfPreventionInterceptor,
    EuLoginSessionTimeoutHandlingInterceptor,
    CoreModule as EuiCoreModule,
    translateConfig,
    EUI_CONFIG_TOKEN,
} from '@eui/core';

import { appConfig as euiAppConfig} from '../config';
import { environment } from '../environments/environment';

export const appConfig: ApplicationConfig = {
    providers: [
        importProvidersFrom(EuiCoreModule.forRoot()),
        importProvidersFrom(TranslateModule.forRoot(translateConfig)),
        provideHttpClient(withInterceptorsFromDi()),
        {
            provide: EUI_CONFIG_TOKEN,
            useValue: { euiAppConfig, environment }
        },
        {
            // Sets the withCredentials on Ajax Request to send the JSESSIONID cookie to another domain.
            // This is necessary when a request is being made to another domain that is protected by EU Login.
            provide: HTTP_INTERCEPTORS,
            useClass: CorsSecurityInterceptor,
            multi: true,
        },
        {
            // When the authentication session is invalid, we need to re-authenticate. The browser refreshes the current URL,
            // and lets the EU Login client redirect to the official EU Login page.
            provide: HTTP_INTERCEPTORS,
            useClass: EuLoginSessionTimeoutHandlingInterceptor,
            multi: true,
        },
        {
            // Adds HTTP header to each Ajax request that ensures the request is set by a piece of JavaScript code in the application.
            // This prevents dynamically-loaded content from forging a request in the name of the currently logged-in user.
            // Be aware that this assumes that cross-site scripting (XSS) is already put in place, (default setting in Angular).
            provide: HTTP_INTERCEPTORS,
            useClass: CsrfPreventionInterceptor,
            multi: true,
        },
        {
            // Asks the intermediate proxies not to return a cache copy of the resource.
            // In matter of fact forces each server in the chain to validate the freshness of the resource.
            provide: HTTP_INTERCEPTORS,
            useClass: CachePreventionInterceptor,
            multi: true,
        },

        AppStarterService,
        // {
        //     provide: APP_INITIALIZER,
        //     useFactory: (appStarterService: AppStarterService) => () => new Promise<void>((resolve) => {
        //         appStarterService.start().subscribe(() => resolve());
        //     }),
        //     deps: [AppStarterService],
        //     multi: true
        // },
        provideRouter(routes),
    ],
};
