import { generateUUID } from '../../../../../utils/misc';
import { RTELogicControlBlock } from '../dto/logic-control-block';
import { BlockToRTEBlockStrategyAbstract } from '../../../slimmers/controllers/helper/element-to-dto-strategy/block-to-rte-block-strategy.abstract';
/**
 * Strategy to convert a div with class 'statementsyntax' into an RTELogicControlBlock DTO.
 */
export class DivToLogicControlBlockStrategy extends BlockToRTEBlockStrategyAbstract {
  constructor() {
    super(...arguments);
    this.blockClassName = 'statementsyntax';
  }
  /**
   * @inheritDoc
   */
  canHandle(elementsArray, currentIndex) {
    const element = elementsArray[currentIndex];
    return element &&
      element.nodeType === Node.ELEMENT_NODE &&
      element.classList.contains(this.blockClassName) &&
      this.isFunctionBlockChild(element)
      ? 1
      : 0;
  }
  /**
   * @inheritDoc
   */
  handle(elementsArray) {
    const element = elementsArray[0];
    return RTELogicControlBlock.from({
      id: generateUUID(),
      type: 'logic-control-block',
      statementText: element.innerText,
    });
  }
  /**
   * The logic control block does not have child blocks to append. so this method is intentionally left empty.
   */
  // eslint-disable-next-line @typescript-eslint/no-empty-function
  appendChildBlocks() { }
  /**
   * @inheritDoc
   */
  shouldProceedToElementChildren() {
    return false;
  }
  /**
   * Checks if the given element is a child of a function block
   *
   * @param {HTMLElement} element - The HTML element to check
   * @returns {boolean} True if the element is a child of a function block, false otherwise
   */
  isFunctionBlockChild(element) {
    return !!(element.parentElement && element.parentElement.classList.contains('statement'));
  }
}
