import { generateUUID } from '../../../../../../utils/misc';
import { RTEListBlock } from '../../../../dto/blocks/list-block';
import { RTEListItemBlock } from '../../../../dto/blocks/list-item-block';
import { BlockToRTEBlockStrategyAbstract } from './block-to-rte-block-strategy.abstract';
import htmlElementToAnnotationGenerator from './annotation-handler/html-element-to-annotation-generator';
/** Transform ol element to list block DTO */
export class OlToRTEListBlockStrategy extends BlockToRTEBlockStrategyAbstract {
  /**
   * 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 === 'OL' ? 1 : 0;
  }
  /**
   * Parse to a number list block.
   *
   * @param {HTMLElement[]} elementsArray - Current elements.
   * @param {VegaRTETransformOptions} [options] - Options for transformation.
   * @returns {Nullable<RTEContentBlock>} - Number list block.
   */
  handle(elementsArray, options) {
    if (elementsArray[0].childNodes.length === 0)
      return null;
    return RTEListBlock.from({
      id: generateUUID(),
      type: 'number-list',
      blocks: [],
      annotations: htmlElementToAnnotationGenerator.generate(RTEListBlock, elementsArray[0], options),
    }, options);
  }
  /**
   * Append child list item blocks to current list block.
   *
   * @param {RTEListBlock} currentBlock - Current list block.
   * @param {RTEContentBlock[]} childBlocks - Current list item blocks.
   */
  appendChildBlocks(currentBlock, childBlocks) {
    currentBlock.children = childBlocks.filter((block) => block instanceof RTEListItemBlock);
    childBlocks.forEach((block) => (block.parent = currentBlock));
  }
  /**
   * Should continue process to child elements.
   *
   * @returns {boolean} - boolean.
   */
  shouldProceedToElementChildren() {
    return true;
  }
}
