import { NgModule, APP_INITIALIZER, Type } from '@angular/core';

import { HttpClient } from '@angular/common/http';

import { UCAP_NATIVE_SERVICE, NativeService } from '@ucap-webmessenger/native';

import { RESOLVERS } from './resolvers';
import { SERVICES } from './services';
import { AppService } from './services/app.service';

import { environment } from '../environments/environment';
import { BrowserNativeService } from '@ucap-webmessenger/native-browser';
import { ElectronNativeService } from '@ucap-webmessenger/native-electron';

export function initializeApp(
  appService: AppService,
  nativeService: NativeService
) {
  return (): Promise<any> => {
    return appService.postInit();
  };
}

// export function nativeServiceFactory(httpClient: HttpClient) {
//   if ('browser' === environment.runtime) {
//     return import('@ucap-webmessenger/native-browser').then(
//       m => new m.BrowserNativeService(httpClient)
//     );
//   } else {
//     return import('@ucap-webmessenger/native-electron').then(
//       m => new m.ElectronNativeService()
//     );
//   }
// }

@NgModule({
  imports: [],
  exports: [],
  providers: [
    {
      provide: UCAP_NATIVE_SERVICE,
      // useFactory: nativeServiceFactory,
      useClass:
        'browser' === environment.runtime
          ? BrowserNativeService
          : ElectronNativeService,
      deps: [HttpClient],
    },
    ...SERVICES,
    ...RESOLVERS,
    {
      provide: APP_INITIALIZER,
      useFactory: initializeApp,
      deps: [AppService, UCAP_NATIVE_SERVICE],
      multi: true,
    },
  ],
})
export class AppProviderModule {}
