import { generateUUID } from '../../../../../utils/misc';
import { BlockToRTEBlockStrategyAbstract } from '../../../slimmers/controllers/helper/element-to-dto-strategy/block-to-rte-block-strategy.abstract';
import { RTETableCellBlock } from '../dto/table-cell-block';
import { RTETableHeadCellBlock } from '../dto/table-head-cell-block';
import htmlElementToAnnotationGenerator from '../../../slimmers/controllers/helper/element-to-dto-strategy/annotation-handler/html-element-to-annotation-generator';
import { RTETableColorManager } from '../utils/table-color-manager';
import { RTE_TABLE_HEAD_DEFAULT_BACKGROUND_COLOR } from '../constants/styles.constants';
/**
 * Strategy to convert native HTML td/th element into table cell DTOs.
 */
export class TdToTableCellBlockStrategy extends BlockToRTEBlockStrategyAbstract {
  /**
   * @inheritDoc
   */
  canHandle(elementsArray, currentIndex) {
    const element = elementsArray[currentIndex];
    return element && (element.tagName === 'TD' || element.tagName === 'TH') ? 1 : 0;
  }
  /**
   * @inheritDoc
   */
  handle(elementsArray, options) {
    const element = elementsArray[0];
    const defaultChildren = [
      {
        id: generateUUID(),
        type: 'paragraph',
        annotations: {
          internalWrapper: options && !options.autoMatchFormat,
        },
        nodes: [
          {
            id: generateUUID(),
            type: 'text',
            annotations: {
              plainText: options && !options.autoMatchFormat,
            },
            text: '',
          },
        ],
      },
    ];
    if (element.tagName === 'TH') {
      return RTETableHeadCellBlock.from({
        id: generateUUID(),
        type: 'table-head-cell',
        annotations: this.getTableHeadCellAnnotations(element, options),
        children: defaultChildren,
      }, options);
    }
    return RTETableCellBlock.from({
      id: generateUUID(),
      type: 'table-cell',
      annotations: htmlElementToAnnotationGenerator.generate(RTETableCellBlock, element, options),
      children: defaultChildren,
    }, options);
  }
  /**
   * @inheritDoc
   */
  appendChildBlocks(currentBlock, childBlocks) {
    currentBlock.children = childBlocks.map((block) => {
      block.parent = currentBlock;
      return block;
    });
  }
  /**
   * @inheritDoc
   */
  shouldProceedToElementChildren() {
    return true;
  }
  /**
   * Generates annotations for a table head cell element.
   *
   * @param {HTMLElement} element The HTML element representing the table head.
   * @param {VegaRTETransformOptions} options Optional transformation options.
   * @returns {VegaRTETableHeadCellBlock['annotations']} The annotations for the table head element.
   */
  getTableHeadCellAnnotations(element, options) {
    const result = htmlElementToAnnotationGenerator.generate(RTETableHeadCellBlock, element, options);
    if (result && result.tableCell) {
      // If the background color matches the default, remove it from the annotation to avoid unnecessary styling
      const colorKey = RTETableColorManager.getColorKey(result.tableCell.backgroundColor, true);
      if (colorKey === RTE_TABLE_HEAD_DEFAULT_BACKGROUND_COLOR.key) {
        delete result.tableCell.backgroundColor;
        // Delete from customStyle as well if it exists to prevent redundant default color styling
        result.customStyle && delete result.customStyle.backgroundColor;
      }
    }
    return result;
  }
}
