import { generateUUID } from '../../../../../../utils/misc';
import { RTEImageBlock } from '../../../../dto/blocks/image-block';
import { isNonNullable } from '../../../../../../types/type-guard';
import { ElementToRTEImageBlockStrategyAbstract } from './element-to-rte-image-block-strategy.abstract';
import htmlElementToAnnotationGenerator from './annotation-handler/html-element-to-annotation-generator';
import { RTEImageNode } from '../../../../dto/nodes/image-node';
/** Transform vega-rich-text-image-editor element to image block DTO */
export class VegaImageToRTEImageBlockStrategy extends ElementToRTEImageBlockStrategyAbstract {
  /**
   * Can be handle.
   *
   * @param {HTMLElement[]} elementsArray - Current elements.
   * @param {number} currentIndex - Current element index.
   * @returns {number} - can be handled element count.
   */
  canHandle(elementsArray, currentIndex) {
    return elementsArray[currentIndex].tagName === 'VEGA-RICH-TEXT-IMAGE-EDITOR' ? 1 : 0;
  }
  /**
   * Parse to a image block.
   *
   * @param {HTMLElement[]} elementsArray - Current elements.
   * @param {VegaRTETransformOptions} [options] - Options for transformation.
   * @returns {Nullable<RTEContentBlock>} - Image block.
   */
  handle(elementsArray, options) {
    const element = elementsArray[0];
    const image = element.querySelector('img');
    if (isNonNullable(image)) {
      return RTEImageBlock.from({
        id: generateUUID(),
        type: 'image',
        nodes: [
          {
            id: generateUUID(),
            type: 'image',
            url: image.src,
            annotations: htmlElementToAnnotationGenerator.generate(RTEImageNode, element, options),
          },
        ],
        annotations: htmlElementToAnnotationGenerator.generate(RTEImageBlock, element.parentElement, options),
      }, options);
    }
  }
  /* istanbul ignore next */
  /**
   * Append child blocks.
   */
  // eslint-disable-next-line @typescript-eslint/no-empty-function
  appendChildBlocks() { }
  /**
   * Should continue process to child elements.
   *
   * @returns {boolean} - boolean.
   */
  shouldProceedToElementChildren() {
    return false;
  }
}
