import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'tableFilter',
  standalone: true
})
export class TableFilterPipe implements PipeTransform {
  transform(items: any[], searchText: string, filterProperties: string[]): any[] {
    if (!items) return [];
    if (!searchText.trim()) return items;

    searchText = searchText.toLowerCase().trim();

    return items.filter(item => {
      return filterProperties.some(prop => {
        const value = this.getNestedProperty(item, prop)?.toString().toLowerCase();
        return value?.includes(searchText);
      });
    });
  }
  
  private getNestedProperty(obj: any, path: string): any {
    return path.split('.').reduce((o, p) => o?.[p], obj);
  }
}