import { Pipe, PipeTransform } from '@angular/core';
import { DateAdapter } from '../components/calendar/providers';
import { Task } from '../defs';

@Pipe({
  name: 'filterTasks',
})
export class FilterTasksPipe implements PipeTransform {
  constructor(private dateAdapter: DateAdapter) {}

  transform(tasks: Task[], type: 'future' | 'today' | 'overdue' = 'today'): Task[] {
    if (!Array.isArray(tasks)) {
      return [];
    }

    return tasks.filter((task) => {
      if (type === 'today') {
        return this.dateAdapter.equals(new Date(), task.date);
      }
      if (type === 'future') {
        return this.dateAdapter.isGreaterThan(new Date(), task.date);
      } else {
        return this.dateAdapter.isLessThan(new Date(), task.date);
      }
    });
  }
}
