import { Component, Event, h, Host, Method, Prop } from '@stencil/core';
export class DvnRichEditorHeadingBlock {
  constructor() {
    this.isContentCleared = false;
  }
  componentShouldUpdate(newValue) {
    return this.input.innerHTML !== newValue.text;
  }
  async focusOnInput() {
    this.input.focus();
    placeCaretAtEnd(this.input);
  }
  render() {
    const Tag = this.type || 'h1';
    return (h(Host, null,
      h(Tag, { ref: el => this.input = el, id: `${Tag}-${this.id}`, key: `${Tag}-${this.id}`, contenteditable: true, onInput: (e) => {
          const text = e.target.innerHTML;
          this.change.emit({
            id: this.id,
            value: {
              text,
            }
          });
          if (text.length === 0) {
            // 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);
          }
        }, onKeyDown: (e) => {
          if (e.key === 'Enter') {
            e.preventDefault();
            this.enterKeyDown.emit(e);
          }
          else 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.value.text.length === window.getSelection().getRangeAt(0).endOffset) {
              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 (window.getSelection().getRangeAt(0).endOffset === 0) {
              this.arrowUp.emit(e);
            }
          }
        } }, this.value.text)));
  }
  static get is() { return "dvn-rich-editor-heading-block"; }
  static get originalStyleUrls() { return {
    "$": ["./dvn-rich-editor-heading-block.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["dvn-rich-editor-heading-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
    },
    "type": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "ComponentType",
        "resolved": "\"h1\" | \"h2\" | \"h3\" | \"text\"",
        "references": {
          "ComponentType": {
            "location": "import",
            "path": "@dvn/alaca/components/dvn-rich-editor/constants"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "type",
      "reflect": false
    },
    "value": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "{ text: string, type: 'h1' | 'h2' | 'h3' }",
        "resolved": "{ text: string; type: \"h1\" | \"h2\" | \"h3\"; }",
        "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": "enterKeyDown",
      "name": "enterKeyDown",
      "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": []
      }
    }
  }; }
}
function placeCaretAtEnd(el) {
  el.focus();
  if (typeof window.getSelection != 'undefined' && typeof document.createRange != 'undefined') {
    const range = document.createRange();
    range.selectNodeContents(el);
    range.collapse(false);
    const sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(range);
  }
  else if (typeof document.body.createTextRange != 'undefined') {
    const textRange = document.body.createTextRange();
    textRange.moveToElementText(el);
    textRange.collapse(false);
    textRange.select();
  }
}
