import { generateUUID } from '../../../../../utils/misc';
import { ElementToDTOStrategy } from '../../../slimmers/controllers/helper/element-to-dto-strategy/element-to-dto-strategy.abstract';
import { RTETableBlock } from '../dto/table-block';
import htmlElementToAnnotationGenerator from '../../../slimmers/controllers/helper/element-to-dto-strategy/annotation-handler/html-element-to-annotation-generator';
/**
 * Strategy to convert a native HTML table element into an RTETableBlock DTO.
 */
export class TableToTableBlockStrategy extends ElementToDTOStrategy {
  /**
   * @inheritDoc
   */
  canHandle(elementsArray, currentIndex) {
    const element = elementsArray[currentIndex];
    return element && element.tagName === 'TABLE' ? 1 : 0;
  }
  /**
   * @inheritDoc
   */
  handle(elementsArray, options) {
    return RTETableBlock.from({
      id: generateUUID(),
      type: 'table-block',
      annotations: htmlElementToAnnotationGenerator.generate(RTETableBlock, elementsArray[0], options),
      children: [],
    }, options);
  }
  /**
   * @inheritDoc
   */
  appendChildBlocks(currentBlock, childBlocks) {
    currentBlock.children = childBlocks.map((block) => {
      block.parent = currentBlock;
      return block;
    });
  }
  /**
   * @inheritDoc
   */
  shouldProceedToElementChildren() {
    return true;
  }
}
