import { RTETextNode } from '../../../../dto/nodes/text-node';
import htmlElementToAnnotationGenerator from './annotation-handler/html-element-to-annotation-generator';
import { ElementToDTOStrategy } from './element-to-dto-strategy.abstract';
import { TextStyleAnnotationHandler } from './annotation-handler/text-style-annotation-handler';
import ElementToDTOClassStrategyManager from '../../../../../../helpers/rte-manager/element-dto-class-strategy-manager';
import { isHTMLElement } from '../../../../../../utils/ui';
/** Abstract class for block html element to RTE block dto */
export class BlockToRTEBlockStrategyAbstract extends ElementToDTOStrategy {
  constructor() {
    super(...arguments);
    this.annotationHandler = new TextStyleAnnotationHandler();
  }
  /**
   * Generate child nodes of block element, this will inherit all annotations from parent to text node and
   *   flat the child nodes.
   *
   * Example:
   *   <li>
   *     <u><i>text1</i></u>
   *     <span></span>
   *     <span style='font-weight: bold;'><span>text2</span></span>
   *     text3
   *   </li>
   * will tranform to:
   *   <li>
   *     <span>text1</span> // with underline and italic annotations
   *     <span></span>      // empty span
   *     <span>text2</span> // with bold annotations
   *     <span>text3</span> // no annotations
   *   </li>
   *
   * @param {HTMLElement[]} elements - Child elements.
   * @param {VegaRTETextAnnotations} annotations - Parent annotations.
   * @param {VegaRTETransformOptions} [options] - Options for transformation.
   * @returns {VegaRTETextNode[]} - Text node DTO.
   */
  generateChildNodes(elements, annotations, options) {
    const childNodes = [];
    const disableAutoMatchFormat = !!(options && options.autoMatchFormat === false);
    const parentAnnotations = Object.assign({}, annotations);
    elements.forEach((child) => {
      const strategy = ElementToDTOClassStrategyManager.getElementToNodeStrategies().find((item) => item.canHandle([child], 0, options) > 0);
      let textNodeJson = null;
      if (strategy) {
        textNodeJson = strategy.handle([child], options, parentAnnotations);
      }
      if (textNodeJson) {
        childNodes.push(textNodeJson);
      }
      else if (isHTMLElement(child) && child.childNodes.length > 0) {
        const curAnnotations = htmlElementToAnnotationGenerator.generate(RTETextNode, child, Object.assign({ parentAnnotations: disableAutoMatchFormat
            ? {
              inlineHtml: parentAnnotations.inlineHtml,
              link: parentAnnotations.link,
              code: parentAnnotations.code,
            }
            : annotations }, options));
        childNodes.push(...this.generateChildNodes(Array.from(child.childNodes), curAnnotations, options));
      }
    });
    return childNodes;
  }
  /**
   * Get the node type of element. The element type is determined in the following
   * order: "data-type" attribute, node name, font size and class name.
   *
   * @param {HTMLElement} element - element.
   * @returns {VegaRTETextBlockType} - Block type.
   */
  matchTextBlockType(element) {
    var _a;
    return (_a = this.annotationHandler.handle(element).textStyle) !== null && _a !== void 0 ? _a : 'paragraph';
  }
}
