var turndownService = new TurndownService()

export class StarWarsStyleJournalSheet extends JournalSheet {
  static get defaultOptions() {
    return mergeObject(super.defaultOptions, {
      classes: [...(super.defaultOptions.classes || []), 'asc-starwars'],
    });
  }
  async _render(force, options) {
    await super._render(force, {...options, collapsed:true});
  }


}

export class StarWarsTextPageSheet extends JournalTextPageSheet {
  static get defaultOptions() {
    return mergeObject(super.defaultOptions, {
      classes: [...(super.defaultOptions.classes || []), 'asc-starwars'],
      width: 600,
      height: 400,
      resizable: true,
    });
  }
  activateListeners(html) {
    super.activateListeners(html);
    // Ensure the button is not duplicated
    if (html.find(".asc-copy-button").length > 0) return;

    // Create the button element
    const button = $(`<div class="asc-copy"><button><i class="fas fa-clipboard"></i>Copy</button></div>`);

    // Insert it inside the .journal-header
    const journal_header = html.find(".journal-header aside")
    journal_header.append(button);

    // Attach event listener
    button.click(this._onCopyButtonClick.bind(this));
  }

  _onCopyButtonClick(event) {
    event.preventDefault();
    var editorContent = this.element.find(".editor-content").html();

    // Replace linked items
    editorContent = editorContent.replace(/@\w+\[.+\]\{(.*?)\}/g,"$1")
    
    const markdownContent = turndownService.turndown(editorContent)

    // Copy to clipboard
    navigator.clipboard.writeText(markdownContent)
    .then(()=>ui.notifications.info("Journal content copied as Markdown!"));
  }
}