import { Injectable, inject } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

import translationsEn from '../../../../public/i18n/en.json';
import { DEFAULT_APP_LOCALE, resolveAppLocale } from './app-i18n.rules';

@Injectable({ providedIn: 'root' })
export class AppI18nService {
  private readonly translate = inject(TranslateService);

  initialize(locale?: string | null): void {
    const resolvedLocale = resolveAppLocale(locale);

    this.translate.setTranslation(DEFAULT_APP_LOCALE, translationsEn);
    this.translate.setFallbackLang(DEFAULT_APP_LOCALE);
    this.translate.use(resolvedLocale);
  }

  instant(key: string, params?: Record<string, unknown>): string {
    return this.translate.instant(key, params);
  }
}
