import { Pipe, PipeTransform } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

@Pipe({
  name: 'dynamicTranslate'
})
export class DynamicTranslatePipe implements PipeTransform {

  constructor(private translateService: TranslateService) {

  }

  transform(value: any, ...args: any[]): any {
    let lang = this.translateService.currentLang ? this.translateService.currentLang : this.translateService.defaultLang;

    let traductions;
    if (typeof args[0].param === 'string') {
      traductions = JSON.parse(args[0].param);
    } else {
      traductions = args[0].param;
    }

    if (traductions && traductions[lang]) {
      return traductions[lang];
    } else {
      return value;

    }
  }

}
