import { Pipe, PipeTransform } from '@angular/core';
import { CourseInstructorData } from '../structures/course-instructor-data';

@Pipe({
  name: 'instructorNames'
})
export class InstructorNamesPipe implements PipeTransform {

  transform(instructors: CourseInstructorData[] | null, role: number = 0) {
    // pokud je hodnota null, uzivatel ke jmenum instruktoru vubec nema pristup
    if(instructors == null) {
      return '';
    }
    // pokud je role 0, vypisuji vsechny schvalene instruktory
    if(role == 0) {
      var printable = instructors.filter(instructor => { return instructor.state == 'aktivni'; } );
    } else {
      var printable = instructors.filter(instructor => { return instructor.roleId == role && instructor.state == 'aktivni'; } );
    }
    // pokud je pole prazdne, nejsou na akci prihlaseni zadni instruktori v pozadovane roli
    if(printable.length == 0) {
      return '-';
    } else {
      var result = '';
      printable.forEach(instructor => {
        result += instructor.instructorName + ', ';
      });
      return result.slice(0, -2); 
    }
  }

}
