import { Component, Event, h, Host, Method, Prop } from '@stencil/core';
export class DvnRichEditor {
  constructor() {
    this.isContentCleared = false;
    this.getValue = () => this.quill.root.innerHTML;
    this.onTextChange = () => {
      if (this.quill.getLength() === 1) {
        // set flag after a while. onKeyDown callback on host should ignore the
        // first key down because it would be the same event that clears the content
        // we want to delete this component once it's blank and user presses backspace
        setTimeout(() => this.isContentCleared = true, 100);
        // hide the overlay menu if the text is cleared
        // 1 means it's empty. See: https://quilljs.com/docs/api/#getlength
        this.overlayMenu.hide();
      }
      this.change.emit({
        id: this.id,
        value: {
          text: this.getValue(),
        }
      });
    };
    this.onTextSelectionChange = (range) => {
      if (!range) {
        // cursor is not in the editor
        this.overlayMenu.hide();
        return;
      }
      if (range.length == 0) {
        // cursor is on a single position, no text is selected
        this.overlayMenu.hide();
        return;
      }
      // a text is selected
      const existingFormat = this.quill.getFormat(range);
      this.overlayMenu.show(range, existingFormat);
    };
  }
  componentDidLoad() {
    var _a;
    // @ts-ignore
    this.quill = new Quill(`#editor-${this.id}`, {
      placeholder: (_a = this.value) === null || _a === void 0 ? void 0 : _a.placeholder,
    });
    this.quill.on('text-change', this.onTextChange);
    this.quill.on('selection-change', this.onTextSelectionChange);
    this.quill.clipboard.dangerouslyPasteHTML(this.value.text);
  }
  async focusOnInput() {
    this.quill.setSelection(this.quill.getLength(), 0, 'silent');
  }
  async getRawValue() {
    return this.quill.getText();
  }
  render() {
    return (h(Host, { onKeyDown: (e) => {
        if (e.key === 'Backspace' && this.isContentCleared) {
          this.lastBackspaceKeyDown.emit(e);
        }
        else if (e.key === 'ArrowDown') {
          // focus on the next component if the cursor is at the end and down arrow is pressed
          if (this.quill.getSelection().index + 1 === this.quill.getLength()) {
            this.arrowDown.emit(e);
          }
        }
        else if (e.key === 'ArrowUp') {
          // focus on the previous component if the cursor is at the beginning and up arrow is pressed
          if (this.quill.getSelection().index === 0) {
            this.arrowUp.emit(e);
          }
        }
      } },
      h("div", { id: `editor-${this.id}` }),
      h("dvn-rich-editor-overlay-menu", { ref: (el) => this.overlayMenu = el, onOverlayMenuAction: (range, action, value) => {
          if (action === 'link') {
            this.quill.formatText(range.index, range.length, action, value);
          }
          else {
            const existingFormat = this.quill.getFormat(range);
            this.quill.format(action, !((existingFormat === null || existingFormat === void 0 ? void 0 : existingFormat[action]) || false));
          }
        } })));
  }
  static get is() { return "dvn-rich-editor-text-block"; }
  static get originalStyleUrls() { return {
    "$": ["dvn-rich-editor-text-block.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["dvn-rich-editor-text-block.css"]
  }; }
  static get properties() { return {
    "id": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "id",
      "reflect": false
    },
    "value": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "{ placeholder: string, text: string }",
        "resolved": "{ placeholder: string; text: string; }",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get events() { return [{
      "method": "change",
      "name": "change",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "lastBackspaceKeyDown",
      "name": "lastBackspaceKeyDown",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "arrowDown",
      "name": "arrowDown",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "arrowUp",
      "name": "arrowUp",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
  static get methods() { return {
    "focusOnInput": {
      "complexType": {
        "signature": "() => Promise<void>",
        "parameters": [],
        "references": {
          "Promise": {
            "location": "global"
          }
        },
        "return": "Promise<void>"
      },
      "docs": {
        "text": "",
        "tags": []
      }
    },
    "getRawValue": {
      "complexType": {
        "signature": "() => Promise<any>",
        "parameters": [],
        "references": {
          "Promise": {
            "location": "global"
          }
        },
        "return": "Promise<any>"
      },
      "docs": {
        "text": "",
        "tags": []
      }
    }
  }; }
}
