import { APP_INITIALIZER, ApplicationConfig, LOCALE_ID } from '@angular/core';
import { Title } from '@angular/platform-browser';
//import { provideRouter, withComponentInputBinding, withDebugTracing } from '@angular/router';
import { provideRouter, withViewTransitions } from '@angular/router';
import { provideAnimations } from '@angular/platform-browser/animations';
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';
import { provideCharts, withDefaultRegisterables } from 'ng2-charts';
import { EMPTY } from "rxjs";

registerLocaleData(localeFr);

import { routes } from './app.routes';
import { apiInterceptor, tokenInterceptor, errorInterceptor } from "@interceptors";
import { JwtService, UserService } from "@services";

export function initAuth(jwtService: JwtService, userService: UserService) {
    return () => (jwtService.getToken() ? userService.getCurrentUser() : EMPTY);
}

export const appConfig: ApplicationConfig = {
    providers: [
        //provideRouter(routes, withComponentInputBinding(), withDebugTracing()),
        provideRouter(routes, withViewTransitions()),
        provideHttpClient(
            withInterceptors([apiInterceptor, tokenInterceptor, errorInterceptor])
        ),
        provideAnimations(),
        provideCharts(withDefaultRegisterables()),
        Title,
        { provide: LOCALE_ID, useValue: 'fr-FR' },
        {
            provide: APP_INITIALIZER,
            useFactory: initAuth,
            deps: [JwtService, UserService],
            multi: true,
        }
    ]
};
