var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { Fragment, h, Host } from '@stencil/core';
import { VegaSlimmer, MapToComponentField, MapToComponentMethod } from 'vega-slimmer/core';
import { createEnterKeyHandlerToTriggerClick } from '../../../../utils/accessibility';
import ChangeManager from '../../../../helpers/change-manager/change-manager';
import domNodeSubjectFactory from '../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
import { VegaInternalComponentDidRender } from '../../../../helpers/event-manager/event-id/vega-internal-event-id';
export class VegaRichTextEditorRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.renderHintMessage = () => {
      return h("vega-text", null, this.hint);
    };
    this.renderTextContent = () => {
      return (h("div", { class: {
          'rich-text-editor-container': true,
          'disabled': this.disabled,
          'vega-hidden': this.sourceView,
        }, style: { minHeight: `${this.rows * 24}px` }, onClick: this.richTextContentFocus, role: "textbox", tabIndex: !this.disabled ? -1 : 0, onKeyDown: createEnterKeyHandlerToTriggerClick(), ref: (node) => (this.richTextContainerRef = node) },
        h("vega-rich-text-content", { ref: (ref) => (this.richTextContentRef = ref), content: this.value, extensions: this.extensions }),
        this.renderCharacterCounter()));
    };
    this.renderCharacterCounter = () => {
      return (h("div", { class: {
          'text-editor-counter': true,
          'counter-exceed-max-length': false,
        }, style: { display: 'none' } },
        h("span", null, "0"),
        h("span", null, "/100")));
    };
    this.renderErrorMessage = () => {
      return h("div", { class: "vega-error vega-hidden" });
    };
    this.richTextContentFocus = () => {
      void this.richTextContentRef.vegaFocus();
    };
  }
  /**
   * Component lifecycle - [componentDidRender]{@link VegaRichTextEditor.componentDidRender}
   */
  componentDidRender() {
    ChangeManager.notify(domNodeSubjectFactory.getSubject(this.host, VegaInternalComponentDidRender), {});
  }
  /**
   * Returns the HTMLElement reference of the rich text container.
   *
   * @returns {HTMLDivElement} An HTMLElement representing the rich text container is being returned.
   */
  getRichTextContainerRef() {
    return this.richTextContainerRef;
  }
  /**
   * Returns the HTMLElement reference of the rich text content area.
   *
   * @returns {Nullable<HTMLVegaRichTextContentElement>} An HTMLVegaRichTextContentElement is being returned.
   */
  getRichTextContentRef() {
    return this.richTextContentRef;
  }
  /**
   * Returns the HTMLElement reference of the rich text outermost layer.
   *
   * @returns {HTMLVegaFlexElement} An HTMLVegaFlexElement is being returned.
   */
  getRichTextWrapperRef() {
    return this.richTextWrapperRef;
  }
  /**
   * Returns the rich text editor component editable element
   *
   * @returns {Nullable<HTMLDivElement>} The div element which has property contenteditable
   */
  getRTEEditableElementRef() {
    if (this.richTextContentRef && this.richTextContentRef.shadowRoot) {
      return this.richTextContentRef.shadowRoot.querySelector('.rich-text-content');
    }
  }
  render() {
    return h(Host, null, this.renderRichTextEditor());
  }
  renderRichTextEditor() {
    return (h("vega-flex", { direction: "col", gap: "0", ref: (flex) => (this.richTextWrapperRef = flex) },
      this.renderLabel(),
      this.hint && this.renderHintMessage(),
      this.toolbarRenderer.render(),
      this.renderContent(),
      this.renderErrorMessage()));
  }
  renderLabel() {
    return (this.label && (h("vega-field-label", { onClick: this.richTextContentFocus, isFieldRequired: this.required, disabled: this.disabled, label: this.label })));
  }
  renderContent() {
    return (h(Fragment, null,
      this.renderTextContent(),
      this.richTextEditorSourceViewRenderer.render()));
  }
}
__decorate([
  MapToComponentField()
], VegaRichTextEditorRenderer.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaRichTextEditorRenderer.prototype, "toolbarRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaRichTextEditorRenderer.prototype, "label", void 0);
__decorate([
  MapToComponentField()
], VegaRichTextEditorRenderer.prototype, "required", void 0);
__decorate([
  MapToComponentField()
], VegaRichTextEditorRenderer.prototype, "hint", void 0);
__decorate([
  MapToComponentField()
], VegaRichTextEditorRenderer.prototype, "disabled", void 0);
__decorate([
  MapToComponentField()
], VegaRichTextEditorRenderer.prototype, "rows", void 0);
__decorate([
  MapToComponentField()
], VegaRichTextEditorRenderer.prototype, "placeholder", void 0);
__decorate([
  MapToComponentField()
], VegaRichTextEditorRenderer.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaRichTextEditorRenderer.prototype, "sourceView", void 0);
__decorate([
  MapToComponentField()
], VegaRichTextEditorRenderer.prototype, "toolbarItems", void 0);
__decorate([
  MapToComponentField()
], VegaRichTextEditorRenderer.prototype, "sourceEditConfig", void 0);
__decorate([
  MapToComponentField()
], VegaRichTextEditorRenderer.prototype, "allowSourceEdit", void 0);
__decorate([
  MapToComponentField()
], VegaRichTextEditorRenderer.prototype, "extensions", void 0);
__decorate([
  MapToComponentField()
], VegaRichTextEditorRenderer.prototype, "richTextEditorSourceViewRenderer", void 0);
__decorate([
  MapToComponentMethod('componentDidRender')
], VegaRichTextEditorRenderer.prototype, "componentDidRender", null);
