import {JOURNAL_ENTRY_PAGE_FORMATS} from "@common/constants.mjs";
import JournalEntryPageCodeMirrorSheet from "./journal-entry-page-code-mirror-sheet.mjs";

/**
 * An Application responsible for displaying a single text-type JournalEntryPage Document, and editing it with a
 * Markdown editor.
 * @extends JournalEntryPageCodeMirrorSheet
 */
export default class JournalEntryPageMarkdownSheet extends JournalEntryPageCodeMirrorSheet {
  /** @override */
  static DEFAULT_OPTIONS = {
    window: {
      icon: "fa-brands fa-markdown"
    }
  };

  /** @inheritDoc */
  static EDIT_PARTS = {
    header: super.EDIT_PARTS.header,
    content: {
      classes: ["flex1", "flexcol"],
      template: "templates/journal/pages/markdown/edit.hbs"
    },
    footer: super.EDIT_PARTS.footer
  };

  /* -------------------------------------------- */
  /*  Properties                                  */
  /* -------------------------------------------- */

  /** @override */
  static format = JOURNAL_ENTRY_PAGE_FORMATS.MARKDOWN;

  /* -------------------------------------------- */
  /*  Rendering                                   */
  /* -------------------------------------------- */

  /** @inheritDoc */
  async _prepareContentContext(context, options) {
    await super._prepareContentContext(context, options);
    if ( !this.isView ) context.markdownFormat = JOURNAL_ENTRY_PAGE_FORMATS.MARKDOWN;
  }

  /* -------------------------------------------- */
  /*  Form Submission                             */
  /* -------------------------------------------- */

  /** @inheritDoc */
  _prepareSubmitData(event, form, formData, updateData) {
    const submitData = super._prepareSubmitData(event, form, formData, updateData);
    // Do not persist the markdown conversion if the contents have not been edited.
    if ( !this._isEditorDirty() ) {
      delete submitData.text.markdown;
      delete submitData.text.format;
    }
    return submitData;
  }
}
