import {Pipe, PipeTransform} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import {SCSetting} from '@openstapps/core';
import {ThingTranslatePipe} from '../../translation/thing-translate.pipe';
import {ThingTranslateService} from '../../translation/thing-translate.service';

/**
 * Translates a setting value (into the display value in current language)
 */
@Pipe({
  name: 'settingValueTranslate',
})
export class SettingTranslatePipe implements PipeTransform {
  constructor(
    private readonly translate: TranslateService,
    private readonly thingTranslate: ThingTranslateService,
  ) {}

  transform(setting: SCSetting) {
    const thingTranslatePipe = new ThingTranslatePipe(
      this.translate,
      this.thingTranslate,
    );
    const translatedSettingValues = thingTranslatePipe.transform(
      'values',
      setting,
    );

    return translatedSettingValues
      ? translatedSettingValues[
          setting.values?.indexOf(setting.value as string) as number
        ]
      : undefined;
  }
}
