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

@Pipe({
  name: 'truncate',
  standalone: true
})
export class TruncatePipe implements PipeTransform {
  transform(
    value: string,
    limit: number = 25,
    completeWords: boolean = false,
    ellipsis: string = '...'
  ): string {
    if (!value) return '';
    if (value.length <= limit) return value;

    if (completeWords) {
      // substring(0, limit) instead of substr(0, limit)
      limit = value.substring(0, limit).lastIndexOf(' ');
    }

    return value.length > limit
      ? value.substring(0, limit) + ellipsis
      : value;
  }
}
