import { Observable } from 'rxjs';
import { HttpHeaders, HttpClient, HttpParams } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { L10nProvider, L10nTranslationLoader } from 'angular-l10n';

@Injectable()
export class HttpTranslationLoader implements L10nTranslationLoader {
  private http = inject(HttpClient, { optional: true });

  private headers = new HttpHeaders({ 'Content-Type': 'application/json' });

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  public get(language: string, provider: L10nProvider): Observable<{ [key: string]: any }> {
    const url = `${provider.asset}/${language}.json`;
    const options = {
      headers: this.headers,
      params: new HttpParams().set('v', provider.options.version),
    };
    return this.http.get(url, options);
  }
}
