import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { MiniMessageClickPayload } from './mini-message.types';

/**
 * Because <mini-message> renders itself recursively for `extra` children, a plain
 * @Output() would need to be manually re-emitted at every level. Instead, every
 * instance injects this single service (provided once at the root usage) and pushes
 * clicks through it. Subscribe to `clicks$` wherever you render the top-level component.
 */
@Injectable()
export class MiniMessageInteractionService {
  private readonly clickSubject = new Subject<MiniMessageClickPayload>();
  readonly clicks$ = this.clickSubject.asObservable();

  emitClick(payload: MiniMessageClickPayload): void {
    this.clickSubject.next(payload);
  }
}
