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 { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { h, Host } from '@stencil/core';
import { VegaDarkModeStyleController } from '../../../../helpers/theme/controllers/dark-mode-style-controller';
import { TextColorToolbarButtonSlimmer } from '../../../vega-rich-text-editor/slimmers/renderers/tools/text-color-toolbar-button-slimmer';
import { VegaRTEContent } from '../../../vega-rich-text-editor/dto/content-state';
import RTEBlockRendererFactory from '../../../vega-rich-text-editor/dto/renderers/renderer-factory';
import { vegaNonceManager } from '../../../../helpers/csp/vega-nonce-manager';
import stateEntityRenderingRegistry from '../../../vega-rich-text-editor/slimmers/controllers/state-entity-rendering-registry';
export class VegaRichTextContentRenderer extends VegaSlimmer {
  constructor() {
    super();
    VegaDarkModeStyleController.registerCustomDarkModeColor(TextColorToolbarButtonSlimmer.formatDarkModeColor());
  }
  componentWillLoad() {
    this.setRTEContent();
  }
  watchValueUpdate() {
    this.setRTEContent();
  }
  /**
   * Get the rich text content element
   *
   * @returns {Nullable<HTMLDivElement>} a div element
   */
  getRichTextContentRef() {
    return this.richTextContentRef;
  }
  render() {
    return (h(Host, null,
      this.extensionStyles ? (h("style", { nonce: vegaNonceManager.nonce, class: "rte-extension-styles" }, this.extensionStyles)) : null,
      this.renderRichTextContent()));
  }
  setRTEContent() {
    if (this.content && !(this.content instanceof VegaRTEContent)) {
      this.content = VegaRTEContent.fromJSON(this.content);
    }
  }
  renderRichTextContent() {
    const showPlaceholder = this.shouldShowPlaceholder();
    return (h("div", { ref: (el) => (this.richTextContentRef = el), class: { 'showPlaceholder': showPlaceholder, 'rich-text-content': true }, "data-placeholder": this.placeholder, contenteditable: this.contentEditable }, this.renderContent()));
  }
  renderContent() {
    var _a;
    const renderContext = { editable: this.contentEditable };
    this.injectRTERefIntoContext(renderContext);
    return (_a = this.content) === null || _a === void 0 ? void 0 : _a.children.map((block) => {
      const blockRenderer = RTEBlockRendererFactory.getBlockRenderer(block.type);
      if (blockRenderer) {
        return blockRenderer.render(block, renderContext, h);
      }
    });
  }
  shouldShowPlaceholder() {
    var _a;
    const blocks = (_a = this.content) === null || _a === void 0 ? void 0 : _a.children;
    if (blocks) {
      if (blocks.length > 1) {
        return false;
      }
      for (const block of blocks) {
        if (block.isNotEmpty()) {
          return false;
        }
      }
    }
    return true;
  }
  /**
   * Inject the reference of rich text editor into render context for the usage of extensions.
   *
   * @param {RTERenderContext} renderContext - The render context which may contain the host reference.
   */
  injectRTERefIntoContext(renderContext) {
    const content = this.content;
    if (content) {
      const ref = stateEntityRenderingRegistry.getDOMByEntity(content);
      // The ref will be null if the content is not rendered by VegaRichTextEditor, in that case we will not inject the ref into context to avoid potential error on extensions which use the ref.
      ref && (renderContext.host = ref);
    }
  }
}
__decorate([
  MapToComponentField()
], VegaRichTextContentRenderer.prototype, "placeholder", void 0);
__decorate([
  MapToComponentField()
], VegaRichTextContentRenderer.prototype, "contentEditable", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaRichTextContentRenderer.prototype, "content", void 0);
__decorate([
  MapToComponentField()
], VegaRichTextContentRenderer.prototype, "extensionStyles", void 0);
__decorate([
  MapToComponentMethod('componentWillLoad')
], VegaRichTextContentRenderer.prototype, "componentWillLoad", null);
__decorate([
  MapToComponentMethod('watchValueUpdate')
], VegaRichTextContentRenderer.prototype, "watchValueUpdate", null);
