import { h } from '@stencil/core';
import { RTENodeRenderer } from './node-renderer.abstract';
import { NodeAnnotationTypeEnum } from '../../annotations/node-annotation.abstract';
import { ZERO_WIDTH_SPACE } from '../../../constants/constant';
class RTETextNodeRenderer extends RTENodeRenderer {
  /**
   * Renders the text node wrapped in the given annotation chain starting from the
   * provided annotation level. Used by the grouped node renderer to render a node
   * inside an already-emitted outer wrapper.
   *
   * @param {RTETextNode} node - The text node to render.
   * @param {Nullable<InlineHtmlAnnotation>} annotation - Starting annotation, or null for text-only.
   * @returns {HTMLElement} The rendered element.
   */
  renderFromAnnotation(node, annotation) {
    const textNodeDom = this.renderText(node);
    return annotation ? this.renderWithWrapper(textNodeDom, annotation) : textNodeDom;
  }
  render(node) {
    return this.renderFromAnnotation(node, node.getAnnotationByType(NodeAnnotationTypeEnum.INLINE_HTML));
  }
  renderWithWrapper(textNodeDom, inlineHtmlNode) {
    const { htmlTag: HtmlTag, customStyle, customAttribute, customClass, } = inlineHtmlNode;
    const attrs = Object.assign(Object.assign({}, customAttribute === null || customAttribute === void 0 ? void 0 : customAttribute.render()), { style: customStyle === null || customStyle === void 0 ? void 0 : customStyle.renderStyle(), class: customClass === null || customClass === void 0 ? void 0 : customClass.renderClass().join(' ') });
    if (!inlineHtmlNode.child) {
      return h(HtmlTag, Object.assign({}, attrs), textNodeDom);
    }
    return h(HtmlTag, Object.assign({}, attrs), this.renderWithWrapper(textNodeDom, inlineHtmlNode.child));
  }
  renderText(node) {
    var _a, _b;
    const HtmlTag = ((_a = node.getAnnotationByType(NodeAnnotationTypeEnum.CODE)) === null || _a === void 0 ? void 0 : _a.code)
      ? 'code'
      : 'span';
    const attrs = Object.assign(Object.assign({}, (_b = node.getCustomAttributesAnnotation()) === null || _b === void 0 ? void 0 : _b.render()), { key: node.id, style: node.getStyles(), class: node.getClasses(), ref: (ref) => this.registerRef(ref, node) });
    return h(HtmlTag, Object.assign({}, attrs), node.text || ZERO_WIDTH_SPACE);
  }
}
export default new RTETextNodeRenderer();
