import { NgModule } from '@angular/core';

import { TranslateModule, TranslateLoader } from '@ngx-translate/core';

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

import { environment } from '../environments/environment';

export function createTranslateLoader(nativeService: NativeService) {
  let prefix: string;

  switch (nativeService.type()) {
    case NativeType.Browser:
      prefix = '/assets/i18n/';
      break;
    case NativeType.Electron:
      prefix = environment.production
        ? '/dist/ucap-webmessenger-app/assets/i18n/'
        : '/projects/ucap-webmessenger-app/src/assets/i18n/';

      break;

    default:
      break;
  }

  return nativeService.getTranslateLoader(prefix, '.json');
}

@NgModule({
  imports: [
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: createTranslateLoader,
        deps: [UCAP_NATIVE_SERVICE]
      }
    })
  ],
  exports: []
})
export class AppTranslateModule {}
