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

import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { I18nService as BaseI18nService } from "@bitwarden/common/platform/services/i18n.service";

import eng from "../../../locales/en/messages.json";

class PreloadedEnglishI18nService extends BaseI18nService {
  constructor() {
    super("en", "", () => {
      return Promise.resolve(eng);
    });
  }
}

function i18nInitializer(i18nService: I18nService): () => Promise<void> {
  return async () => {
    await (i18nService as any).init();
  };
}

// This is a helper I18nService implementation that loads the english `message.json` eliminating
//  the need for fetching them dynamically. It should only be used within storybook.
@NgModule({
  providers: [
    {
      provide: I18nService,
      useClass: PreloadedEnglishI18nService,
    },
    {
      provide: APP_INITIALIZER,
      useFactory: i18nInitializer,
      deps: [I18nService],
      multi: true,
    },
  ],
})
export class PreloadedEnglishI18nModule {}
