import { AnnotationGeneratorStrategyAbstract } from '../../../slimmers/controllers/helper/element-to-dto-strategy/annotation-handler/annotation-generator-strategy.abstract';
import { RTETableColorManager } from '../utils/table-color-manager';
/**
 * Base class for table annotation handlers with common extraction methods.
 */
export class TableAnnotationHandlerBase extends AnnotationGeneratorStrategyAbstract {
  /**
   * Extract background color from element.
   *
   * @param {HTMLElement} element - Table element.
   * @returns {Nullable<string>} Background color value.
   */
  getBackgroundColor(element) {
    if (element.style.backgroundColor) {
      return (RTETableColorManager.getColorKey(element.style.backgroundColor) ||
        element.style.backgroundColor);
    }
    return null;
  }
  /**
   * Extract border properties from element.
   *
   * @param {HTMLElement} element - Table element.
   * @returns {Nullable<VegaRTETableBorderType>} Border properties.
   */
  getBorder(element) {
    const style = element.style;
    // Check individual border properties
    const borderStyle = style.borderStyle;
    const borderWidth = style.borderWidth;
    const borderColor = style.borderColor;
    if (borderColor || borderStyle || borderWidth) {
      const updatedBorderColor = borderColor === 'initial' ? '' : borderColor;
      return {
        style: borderStyle === 'initial' ? '' : borderStyle,
        width: borderWidth === 'initial' ? '' : borderWidth,
        color: RTETableColorManager.getColorKey(updatedBorderColor) || updatedBorderColor,
      };
    }
    return null;
  }
  /**
   * Extract width from element.
   *
   * @param {HTMLElement} element - Table element.
   * @returns {Nullable<string>} Width value.
   */
  getWidth(element) {
    if (element.style.width) {
      return element.style.width;
    }
    // Check for width attribute
    const widthAttr = element.getAttribute('width');
    if (widthAttr) {
      // If it's just a number, assume pixels
      return /^\d+$/.test(widthAttr) ? `${widthAttr}px` : widthAttr;
    }
    return null;
  }
  /**
   * Extract height from element.
   *
   * @param {HTMLElement} element - Table element.
   * @returns {Nullable<string>} Height value.
   */
  getHeight(element) {
    if (element.style.height) {
      return element.style.height;
    }
    // Check for height attribute
    const heightAttr = element.getAttribute('height');
    if (heightAttr) {
      // If it's just a number, assume pixels
      return /^\d+$/.test(heightAttr) ? `${heightAttr}px` : heightAttr;
    }
    return null;
  }
}
