import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ProvidersService } from '../providers.service';


/**************************  
 Created by Ajith A Pradeep
 Created on 21/03/2022
 **************************/

@Injectable({
  providedIn: 'root'
})
export class ErrorTranslateService {

  helpPrimaryData: any = {};
  helpSecondaryData: any = {};

  environment: any;
  primaryLangauge: any="en";

  constructor(
    private httpClient: HttpClient,
    private conn: ProvidersService
  ) {
    this.environment = this.conn.environment;
    this.init();
  }

  async init() {
    await this.getPrimarySecondryLangauge();
  }

  async getPrimarySecondryLangauge(): Promise<void> {
    this.helpPrimaryData = await this.getErrorJsonData(this.primaryLangauge);
  }

  translate(code: string): string {
    return this.first(code) ;
  }

  first(code: string): string {
    switch (this.primaryLangauge) {
      case "": return;
      default:
        return this.helpPrimaryData[code] ? (this.helpPrimaryData[code]) : code;
    }
  }

  
  private async getErrorJsonData(lang: string): Promise<any> {
    if (!lang || lang.trim() == "") return {};
    return await this.httpClient.get(`assets/i18n/error/${lang}.json`).toPromise();
  }

}
