import { RTEBlock } from './block.abstract';
import { HTML_VOID_TAGS } from '../../constants/constant';
/**
 * HTML Block
 */
export class RTEHtmlBlock extends RTEBlock {
  constructor(id, htmlTag) {
    super(id);
    this.dtoName = RTEHtmlBlock.dtoName;
    this.type = 'html-block';
    this.children = [];
    this.htmlTag = htmlTag;
  }
  /**
   * Converts a VegaRTEHtmlBlock to an RTEHtmlBlock.
   *
   * @param {VegaRTEHtmlBlock} block - The VegaRTEHtmlBlock to convert.
   * @param {VegaRTETransformOptions} [options] - Optional transformation options.
   * @returns {RTEHtmlBlock} The converted RTEHtmlBlock.
   */
  static from(block, options) {
    const htmlBlock = new RTEHtmlBlock(block.id, block.htmlTag);
    const { annotations } = block;
    if (annotations) {
      super.convertAnnotationsToMap(htmlBlock.annotationMap, annotations);
    }
    htmlBlock.children = this.generateBlockChildren(block.children, options, htmlBlock);
    return htmlBlock;
  }
  /**
   * @inheritDoc
   */
  clone(parent) {
    const clonedBlock = new RTEHtmlBlock(this.id, this.htmlTag);
    clonedBlock.children = this.children.map((child) => child.clone(clonedBlock));
    clonedBlock.annotationMap = super.cloneAnnotations();
    clonedBlock.parent = parent;
    return clonedBlock;
  }
  /**
   * @inheritDoc
   */
  getLastNode() {
    return this.children[this.children.length - 1].getLastNode();
  }
  /**
   * @inheritDoc
   */
  isNotEmpty() {
    // Any RTEHtmlBlock — whether void (hr, br), childless (canvas, iframe),
    // or with children — represents real user content. Only the default empty
    // text paragraph should make the editor report as empty.
    return true;
  }
  /**
   * @inheritDoc
   */
  toHtml(options) {
    const BlockTag = this.htmlTag;
    const attrStr = super.generateAttributeString(options);
    // Handle void/self-closing tags (hr, input, col, etc.)
    // Uses HTML5 syntax without self-closing slash for consistency with img and br tags
    if (HTML_VOID_TAGS.has(this.htmlTag)) {
      return `<${BlockTag}${attrStr}>`;
    }
    // Handle normal tags with children
    return [
      `<${BlockTag}${attrStr}>`,
      this.children.map((block) => block.toHtml(options)).join(''),
      `</${BlockTag}>`,
    ].join('');
  }
  /**
   * @inheritDoc
   */
  toJSON() {
    return Object.assign(Object.assign({}, super.toJSON()), { id: this.id, type: this.type, htmlTag: this.htmlTag, children: this.children.map((child) => child.toJSON()) });
  }
}
RTEHtmlBlock.dtoName = 'RTEHtmlBlock';
