import {BrowserModule, BrowserTransferStateModule} from '@angular/platform-browser';
import {APP_INITIALIZER, NgModule} from '@angular/core';
import {HttpClientModule} from '@angular/common/http';
import {RouterModule} from '@angular/router';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';

import {CoreModule, Endpoint} from 'm2-core';
import {AppComponent} from './app.component';
import {PluginLoaderService} from './services/plugin-loader/plugin-loader.service';
import {ClientPluginLoaderService} from './services/plugin-loader/client-plugin-loader.service';
import {PluginsConfigProvider} from './services/plugins-config.provider';
import {TransferStateService} from './services/transfer-state.service';
import {environment} from '../environments/environment';
import {PluginsLoadingDemoComponent} from './plugins-loading-demo/plugins-loading-demo.component';


const ENDPOINT = environment.production ? null : new Endpoint('http', 'ws', '127.0.0.1', 8080);

@NgModule({
    declarations: [AppComponent, PluginsLoadingDemoComponent],
    imports: [
        HttpClientModule,
        BrowserModule.withServerTransition({appId: 'serverApp'}),
        BrowserTransferStateModule,
        BrowserAnimationsModule,
        CoreModule,
        RouterModule.forRoot([])
    ],
    providers: [
        {provide: PluginLoaderService, useClass: ClientPluginLoaderService},
        PluginsConfigProvider,
        {
            provide: APP_INITIALIZER,
            useFactory: (provider: PluginsConfigProvider) => () =>
                provider
                    .loadConfig()
                    .toPromise()
                    .then(config => (provider.config = config)),
            multi: true,
            deps: [PluginsConfigProvider]
        }
    ],
    bootstrap: [AppComponent]
})
export class AppModule {

    constructor(
        transferStateService: TransferStateService
    ) {
    }

}
