import { generateUUID } from '../../../../../utils/misc';
import { RTEAnnotationStyle } from '../../../dto/annotations/annotation-style';
import { RTEBlock } from '../../../dto/blocks/block.abstract';
import { RTETextBlock } from '../../../dto/blocks/text-block';
/**
 * RTE Function Block
 */
export class RTEFunctionBlock extends RTEBlock {
  constructor(id) {
    super(id);
    this.dtoName = RTEFunctionBlock.dtoName;
    this.type = 'function-block';
    this.children = [];
  }
  /**
   * Creates an RTEFunctionBlock from a VegaFunctionBlock.
   *
   * @param {VegaFunctionBlock} block - The custom block to convert.
   * @param {VegaRTETransformOptions} [options] - Optional transformation options.
   * @returns {RTEFunctionBlock} The resulting RTEFunctionBlock.
   */
  static from(block, options) {
    const functionBlock = new RTEFunctionBlock(block.id);
    const defaultAnnotation = { customClass: ['statement'] };
    let { annotations } = block;
    annotations = annotations
      ? Object.assign(Object.assign({}, annotations), defaultAnnotation) : defaultAnnotation;
    super.convertAnnotationsToMap(functionBlock.annotationMap, annotations);
    functionBlock.children = this.generateBlockChildren(block.children, options, functionBlock);
    return functionBlock;
  }
  /**
   * Converts the RTEFunctionBlock and its children to an HTML string.
   *
   * @returns {string} The HTML string representation of the function block.
   */
  toHtml() {
    const childrenHtml = this.children
      .map((child) => {
      if (child instanceof RTETextBlock) {
        const attrStr = this.getFunctionContentStyle(child);
        return `<div class="statementelement"${attrStr}>${child.getChildrenHtml()}</div>`;
      }
      else {
        return child.toHtml();
      }
    })
      .join('');
    return `<div class="statement">${childrenHtml}</div>`;
  }
  /**
   * @inheritDoc
   */
  clone(parent) {
    const clonedBlock = new RTEFunctionBlock(generateUUID());
    clonedBlock.parent = parent;
    clonedBlock.children = this.children.map((child) => {
      const clonedChild = child.clone(clonedBlock);
      return clonedChild;
    });
    clonedBlock.annotationMap = super.cloneAnnotations();
    return clonedBlock;
  }
  /**
   * @inheritDoc
   */
  getLastNode() {
    return this.children[this.children.length - 1].getLastNode();
  }
  /**
   * @inheritDoc
   */
  isNotEmpty() {
    return this.children.length > 0;
  }
  /**
   * @inheritDoc
   */
  toJSON() {
    return Object.assign(Object.assign({}, super.toJSON()), { id: this.id, type: this.type, children: this.children.map((child) => child.toJSON()) });
  }
  /**
   * Get the style string for function content block, excluding default paragraph styles.
   *
   * @param {RTETextBlock} block - The text block to extract styles from
   * @returns {string} - The style attribute string
   */
  getFunctionContentStyle(block) {
    const blockAnnotationStyle = new RTEAnnotationStyle(block);
    let styles = blockAnnotationStyle.toCssText({ standalone: true });
    const textStyleAnnotation = block.getAnnotationByType('TEXT_STYLE');
    if (textStyleAnnotation && textStyleAnnotation.textStyle === 'paragraph') {
      const defaultParagraphStyles = blockAnnotationStyle.toCssText(undefined, undefined, textStyleAnnotation.renderStyle({ standalone: true }));
      styles = styles.replace(defaultParagraphStyles, '').trim();
    }
    return styles ? ` style="${styles}"` : '';
  }
}
RTEFunctionBlock.dtoName = 'RTEFunctionBlock';
