import { ModuleWithProviders, NgModule, Optional, SkipSelf, APP_INITIALIZER } from '@angular/core';
import { CommonModule } from '@angular/common';
import { throwIfAlreadyLoaded } from './module-import-guard';
import { CookieService } from 'ngx-cookie-service';

import {
  AnalyticsService,
  LayoutService,
  PlayerService,
  StateService,
} from './utils';
import { LoginApiService, initializeLoginSvc } from '../frontcraft/services/login-api';

const DATA_SERVICES = [  
  {provide: LoginApiService, useClass: LoginApiService},
  {provide: CookieService, useClass: CookieService},
  {
    provide: APP_INITIALIZER,
    useFactory: initializeLoginSvc,
    deps: [LoginApiService, CookieService],
    multi: true
  }
];

export const NB_CORE_PROVIDERS = [
  ...DATA_SERVICES,

  AnalyticsService,
  LayoutService,
  PlayerService,
  StateService,
  CookieService,
];

@NgModule({
  imports: [
    CommonModule,
  ],
  exports: [
  ],
  declarations: [],
})
export class CoreModule {
  constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
    throwIfAlreadyLoaded(parentModule, 'CoreModule');
  }

  static forRoot(): ModuleWithProviders {
    return <ModuleWithProviders>{
      ngModule: CoreModule,
      providers: [
        ...NB_CORE_PROVIDERS,
      ],
    };
  }
}
