import { TableAnnotationHandlerBase } from './table-annotation-handler-base';
import { cleanObject } from '../../../../../utils/object';
/**
 * Handler for table cell annotation parsing from HTML elements.
 */
export class TableCellAnnotationHandler extends TableAnnotationHandlerBase {
  /**
   * @inheritDoc
   */
  canHandle(targetDto) {
    return targetDto.name === 'RTETableCellBlock';
  }
  /**
   * Handle annotation extraction from table cell element.
   *
   * @param {HTMLElement} element - Current table cell element.
   * @returns {VegaRTEAnnotations} - Extracted table cell annotation.
   */
  handle(element) {
    const cellAnnotation = cleanObject({
      backgroundColor: this.getBackgroundColor(element),
      padding: this.getPadding(element),
      border: this.getBorder(element),
      width: this.getWidth(element),
      height: this.getHeight(element),
      textAlign: this.getTextAlign(element),
      verticalAlign: this.getVerticalAlign(element),
      minWidth: this.getMinWidth(element),
      boxSizing: this.getBoxSizing(element),
    });
    return Object.keys(cellAnnotation).length > 0
      ? { tableCell: cellAnnotation }
      : {};
  }
  /**
   * Extract minimum width from element.
   *
   * @param {HTMLElement} element - Table cell element.
   * @returns {Nullable<string>} Minimum width value.
   */
  getMinWidth(element) {
    if (element.style.minWidth) {
      return element.style.minWidth;
    }
    return null;
  }
  /**
   * Extract box sizing from element.
   *
   * @param {HTMLElement} element - Table cell element.
   * @returns {Nullable<string>} Box sizing value.
   */
  getBoxSizing(element) {
    if (element.style.boxSizing) {
      return element.style.boxSizing;
    }
    return null;
  }
  /**
   * Extract padding from element.
   *
   * @param {HTMLElement} element - Table cell element.
   * @returns {Nullable<string>} Padding value.
   */
  getPadding(element) {
    if (element.style.padding) {
      return element.style.padding;
    }
    return null;
  }
  /**
   * Extract text alignment from element.
   *
   * @param {HTMLElement} element - Table cell element.
   * @returns {Nullable<VegaRTETableTextAlignType>} Text alignment value.
   */
  getTextAlign(element) {
    // Check inline style
    if (element.style.textAlign) {
      const align = element.style.textAlign;
      if (['left', 'center', 'right', 'justify'].includes(align)) {
        return align;
      }
    }
    // Check for align attribute (deprecated but still used)
    const alignAttr = element.getAttribute('align');
    if (alignAttr && ['left', 'center', 'right', 'justify'].includes(alignAttr)) {
      return alignAttr;
    }
    return null;
  }
  /**
   * Extract vertical alignment from element.
   *
   * @param {HTMLElement} element - Table cell element.
   * @returns {Nullable<VegaRTETableVerticalAlignType>} Vertical alignment value.
   */
  getVerticalAlign(element) {
    // Check inline style
    if (element.style.verticalAlign) {
      const align = element.style.verticalAlign;
      if (['top', 'middle', 'bottom'].includes(align)) {
        return align;
      }
    }
    return null;
  }
}
