import { Controller } from '@hotwired/stimulus';

export default class extends Controller<HTMLElement> {
   static targets = ['content', 'readMore'];

   declare contentTarget: HTMLElement;
   declare readMoreTarget: HTMLTemplateElement;
   declare hasContentTarget: boolean;
   declare hasReadMoreTarget: boolean;

   connect() {
      if (!this.hasContentTarget || !this.hasReadMoreTarget) return;

      const lastParagraph = this.contentTarget.querySelector('p:last-of-type');
      if (!lastParagraph) return;

      const clone = this.readMoreTarget.content.cloneNode(true);
      lastParagraph.append(' ', clone);
   }
}
