import { Injector } from '@angular/core';
import { Location} from '@angular/common';
import { Router,UrlSerializer } from '@angular/router';
import { AuthConfig, OAuthService, OAuthErrorEvent, OAuthStorage  } from 'angular-oauth2-oidc';
import { AppConfig } from "./app.config";
import {ItemTypeService} from '../services/itemtype.service';
import { IAuthconfigFactory } from './authconfigFactory';



export function appConfigFactory(injector:Injector, appConfig: AppConfig, oauthService: OAuthService, authconfigFactory:IAuthconfigFactory,authStorage:OAuthStorage,itemtypeService:ItemTypeService): () => Promise<any> {
  return (): Promise<any> => {
    return new Promise<void>((resolve,reject) => {
      appConfig.load().then(() => {
        itemtypeService.load(appConfig);
        oauthService.configure(authconfigFactory.getAuthConfig(appConfig));
        oauthService.setStorage(authStorage);
        oauthService.setupAutomaticSilentRefresh();
      }).then(() => oauthService.loadDiscoveryDocument()
      ).then(() => resolve()).catch(() => reject());
    });
  }
}
