import { Pipe, PipeTransform } from "@angular/core";
import { SobreDoenca } from "src/app/models/sobre-doenca/sobre-doenca.model";

@Pipe({ name: 'diseaseSearchFilter' })
export class DiseaseSearchFilterPipe implements PipeTransform {
    private _normalize(value: string) {
        return value.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
    }

    transform(diseases: SobreDoenca[], args?: string) {
        if (!diseases || diseases.length === 0) return [];
    
        if (!args) return diseases;

        const terms = this._normalize(args).split(' ');

        return diseases.filter((disease) => {
            let count = 0;
            
            const diseaseName = this._normalize(disease.nome);

            terms.forEach((term) => {
                if (diseaseName.includes(term)) count++;
            });

            return count === terms.length;
        });  
    }
}