import { Pipe, PipeTransform } from '@angular/core';
import { FilterByPipe } from 'ngx-pipes';
import { StringUtils } from '../../utils/string-utils';

@Pipe({
  name: 'ignoreAccents'
})
export class IgnoreAccentsPipe implements PipeTransform {
  transform<T>(valueArray: Array<T>, fields : Array<string>, searchValue : any, strict? : boolean): Array<T> {
    let l_array=[];
    let l_searchValue : string = StringUtils.removeAccents(searchValue).toLowerCase();
    if ((valueArray)&&(valueArray.length>0)&&(fields)&&(fields.length>0)) {
      l_array=valueArray.filter( value => {
        let l_result : boolean=false;
        for(let l_field of fields) {
          let l_objectValue : string=StringUtils.removeAccents(value[l_field]);
          if (l_objectValue) {
            l_objectValue=l_objectValue.toLowerCase();
            l_result=strict?l_objectValue===l_searchValue:l_objectValue.indexOf(l_searchValue)>-1;
            if (l_result) {
              break;
            }
          }
        }
        return l_result;
      })
    }
    return l_array;
  }

}