import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
import { MiniMessageComponent } from './mini-message.types';
import { miniMessageToHtml } from './mini-message-html.util';

/**
 * <div [innerHTML]="componentJson | miniMessage"></div>
 *
 * Simpler than <mini-message> but static: no obfuscation animation, and clicks on
 * non-URL actions (run_command, etc.) need to be wired up yourself via a (click) handler
 * on the container using event delegation, reading data-click-action / data-click-value
 * off event.target.
 */
@Pipe({
  name: 'miniMessage',
  standalone: true,
})
export class MiniMessagePipe implements PipeTransform {
  constructor(private readonly sanitizer: DomSanitizer) {}

  transform(node: MiniMessageComponent | null | undefined): SafeHtml {
    if (!node) return '';
    return this.sanitizer.bypassSecurityTrustHtml(miniMessageToHtml(node));
  }
}
