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

@Pipe({
  name: 'highlightMentions',
  standalone: true
})
export class HighlightMentionsPipe implements PipeTransform {

  transform(text: string, mentions: PostList['mentions']): string {
    if (text && mentions?.length) {
      mentions.forEach((mention) => {
        const regex = new RegExp(`@${mention.name}`, 'g');
        text = text.replace(
          regex,
          `<span class="mention-${mention.type}" data-id="${mention.id}">@${mention.name}</span>`
        );
      });
    }
    return text;
  }

}
