import { isNonNullable } from '../../../../../../types/type-guard';
import { generateUUID } from '../../../../../../utils/misc';
import { RTEListItemBlock } from '../../../../dto/blocks/list-item-block';
import { BlockToRTEBlockStrategyAbstract } from './block-to-rte-block-strategy.abstract';
/** Transform child element of UL element which are not LI element to list item block DTO */
export class ULNotLiChildToRTEListItemBlockStrategy 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) {
    const element = elementsArray[currentIndex];
    const parent = element.parentElement;
    const isLiNode = element.nodeName !== 'LI';
    // while child is empty text node like newline or empty string, we should not covert it.
    const isEmptyNode = element.nodeType === Node.TEXT_NODE && element.textContent.trim() === '';
    // The node must be in list item.
    const isInList = isNonNullable(parent) && ['UL', 'OL'].includes(parent.nodeName);
    return isLiNode && !isEmptyNode && isInList ? 1 : 0;
  }
  /**
   * Parse to a list item block.
   *
   * @param {HTMLElement[]} elementsArray - Current elements.
   * @param {VegaRTETransformOptions} [options] - Options for transformation.
   * @returns {Nullable<RTEContentBlock>} - List item block.
   */
  handle(elementsArray, options) {
    const nodes = this.generateChildNodes(elementsArray, {}, options);
    return RTEListItemBlock.from({
      id: generateUUID(),
      type: 'list-item',
      nodes: nodes,
      annotations: {},
    }, 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;
  }
}
