import { Pipe, PipeTransform } from '@angular/core';
@Pipe({  name: 'filter', pure: false})
export class SearchPipe implements PipeTransform {
  transform(items: any[], searchText: string): any {
    if (!items || !searchText) {
      return items;
    }
    return items.filter(function (item) {
      return Object.values(item).map(function (value) {
        return String(value);
      }).find(function (value) {
        return value.toString().toLowerCase().includes(searchText.toString().toLowerCase());
      });
    });
    // return items.filter(item => JSON.stringify(item).toLowerCase().indexOf(searchText.toLowerCase()) !== -1);
  }
}

@Pipe({ name: 'searchbytitle', pure: false })
export class SearchByNamePipe implements PipeTransform {
  transform(request: any[], colname: string, searchText: any): any {

    if (!request || !searchText) {
      return request;
    } else {
      // if (colname == 'formname-documentid') {
      //  var col = colname.split('-');
      //  return request.filter((item) => {
      //    return item[col[0]].toLowerCase().includes(searchText) || item[col[1]].toLowerCase().includes(searchText)
      //    //return request.filter(function (req) {

      //    //  return req.requesttype.toLowerCase().includes(searchText);
      //  })
      // }
      // else {
      return request.filter((item) => {
        return item[colname].toString().toLowerCase().includes(searchText.toString().toLowerCase());
        // return request.filter(function (req) {

        //  return req.requesttype.toLowerCase().includes(searchText);
      });
      // }
    }
  }
}

@Pipe({ name: 'orderBy' })
export class orderBy implements PipeTransform {
  transform(obj: any, orderFields: string, order: any): any {
    obj.forEach(function (currentField) {
      var orderType = 'ASC';
      
      
      if (order == true) {
        orderType = 'DESC';
      }
      obj.sort(function (a, b) {
        
        
        if (a[orderFields] != '' && a[orderFields] != null&&(typeof(a[orderFields]) == 'string') && b[orderFields] != '' 
        && b[orderFields] != null&&((typeof(b[orderFields]) == 'string'))) {         
          
          if (orderType === 'ASC') {
            if (a[orderFields].toLowerCase() < b[orderFields].toLowerCase()) return -1;
            if (a[orderFields].toLowerCase() > b[orderFields].toLowerCase()) return 1;
            return 0;
          } else {
            if (a[orderFields].toLowerCase() < b[orderFields].toLowerCase()) return 1;
            if (a[orderFields].toLowerCase() > b[orderFields].toLowerCase()) return -1;
            return 0;
          }
        } else {
          return -1;
        }

      });

    });
    return obj;
  }
}

@Pipe({ name: 'sidemenusearch', pure: false })
export class sidemenusearch implements PipeTransform {
  transform(request: any[], searchText: any): any {
    if (!request || !searchText) {
      return request;
    } else {
      return request.filter((item) => {
        var childd = item.children.filter((child) => {
          return child['name'].toString().toLowerCase().includes(searchText.toString().toLowerCase());
        });
        return (item['name'].toString().toLowerCase().includes(searchText.toString().toLowerCase()) || (childd.length > 0));

      });

    }
  }
}