import { RdDTextEditor } from "../apps/rdd-text-roll-editor.js"
import { SYSTEM_RDD } from "../constants.js"

export class RdDJournalSheet extends foundry.applications.sheets.journal.JournalEntryPageProseMirrorSheet {

  static register() {
    foundry.applications.apps.DocumentSheetConfig.unregisterSheet(JournalEntryPage, "core", foundry.appv1.sheets.JournalTextPageSheet)
    foundry.applications.apps.DocumentSheetConfig.registerSheet(JournalEntryPage, SYSTEM_RDD, RdDJournalSheet, {
      types: ["text"],
      makeDefault: true,
    })
  }

  async _prepareContentContext(context, options) {
    if ( this.isView ) {
      context.text = { ...this.page.text }
      context.text.enriched = await RdDTextEditor.enrichHTML(context.text.content, this.page)
    }
  }

  async _onRender(context, options) {
    await super._onRender(context, options)
    const el = this.element
    el.querySelectorAll(".roll-text").forEach(btn =>
      btn.addEventListener("click", event => RdDTextEditor.rollText(event, this.actor))
    )
    el.querySelectorAll(".chat-roll-text").forEach(btn =>
      btn.addEventListener("click", event => RdDTextEditor.chatRollText(event))
    )
  }
}
