import { TableAnnotationHandlerBase } from './table-annotation-handler-base';
import { cleanObject } from '../../../../../utils/object';
/**
 * Handler for table annotation parsing from HTML elements.
 */
export class TableAnnotationHandler extends TableAnnotationHandlerBase {
  /**
   * @inheritDoc
   */
  canHandle(targetDto) {
    return targetDto.dtoName === 'RTETableBlock';
  }
  /**
   * Handle annotation extraction from table element.
   *
   * @param {HTMLElement} element - Current table element.
   * @returns {VegaRTEAnnotations} - Extracted table annotation.
   */
  handle(element) {
    const tableAnnotation = cleanObject({
      backgroundColor: this.getBackgroundColor(element),
      border: this.getBorder(element),
      width: this.getWidth(element),
      height: this.getHeight(element),
      alignment: this.getAlignment(element),
    });
    return Object.keys(tableAnnotation).length > 0
      ? { table: tableAnnotation }
      : {};
  }
  /**
   * Extract alignment from element.
   *
   * @param {HTMLElement} element - Table element.
   * @returns {Nullable<VegaRTETableAlignment>} Alignment value.
   */
  getAlignment(element) {
    // Check for class-based alignment
    const classList = element.classList;
    if (classList.contains('v-rte--extension-table-align-left')) {
      return 'left';
    }
    else if (classList.contains('v-rte--extension-table-align-center')) {
      return 'center';
    }
    else if (classList.contains('v-rte--extension-table-align-right')) {
      return 'right';
    }
    // Check margin-based alignment
    const style = element.style;
    const marginLeft = style.marginLeft;
    const marginRight = style.marginRight;
    if (marginLeft === 'auto' && marginRight === 'auto') {
      return 'center';
    }
    else if (marginLeft === '0' || marginLeft === '0px') {
      return 'left';
    }
    else if (marginRight === '0' || marginRight === '0px') {
      return 'right';
    }
    // Check for align attribute (deprecated but still used)
    const alignAttr = element.getAttribute('align');
    if (alignAttr && ['left', 'center', 'right'].includes(alignAttr)) {
      return alignAttr;
    }
    return null;
  }
}
