import { Pipe, PipeTransform } from '@angular/core';
import { DomainTaskCalendarService, ProcessingStatusList } from '@domain/task-calendar';
import { DisplayInfoDTO } from '@swagger/eis';

@Pipe({
  name: 'filterStatus',
})
export class FilterStatusPipe implements PipeTransform {
  constructor(private domainTaskCalendarService: DomainTaskCalendarService) {}

  transform(items: DisplayInfoDTO[], includes: ProcessingStatusList = [], excludes: ProcessingStatusList = []): DisplayInfoDTO[] {
    return items?.filter((item) => {
      const processingStatus = this.domainTaskCalendarService.getProcessingStatusList(item);
      let isIncluded = false;
      let isExcluded = false;
      for (const include of includes) {
        isIncluded = processingStatus.includes(include);
        // Early Exit
        if (!isIncluded) {
          return false;
        }
      }
      for (const exclude of excludes) {
        isExcluded = !processingStatus.includes(exclude);
      }
      return isIncluded && !isExcluded;
    });
  }
}
