import { RTEFilterStylesStrategy, } from '../../../../dto/filter-styles-strategies/filter-styles-strategy.abstract';
import { RTETableCellAnnotation } from '../../annotations/table-cell-annotation';
/**
 * A strategy defines how to filter and refill styles for table cell elements.
 */
export class TableCellFilterStylesStrategy extends RTEFilterStylesStrategy {
  constructor(cellType = 'td') {
    super();
    this.stylesToBeFiltered = RTETableCellAnnotation.getDefaultStyles();
    this.elementTagName = '';
    this.stylesWithRGBColor = RTETableCellAnnotation.getDefaultStyles(false);
    this.cellBlockClassName = '';
    this.elementTagName = cellType;
    this.cellBlockClassName = cellType === 'td' ? 'RTETableCellBlock' : 'RTETableHeadCellBlock';
  }
  /**
   * @inheritDoc
   */
  canFilter(blockOrNodeInstance) {
    return this.isTableCell(blockOrNodeInstance.constructor.name);
  }
  /**
   * The table cell border color can be represented in different formats (e.g., hex, rgb), so we need to ensure that we can handle both cases.
   *
   * @inheritDoc
   */
  filterAndDeleteStyles(fullStyles, classNames) {
    this.convertDefaultBorderColorFromRGBToHex(fullStyles);
    return super.filterAndDeleteStyles(fullStyles, classNames);
  }
  /**
   * @inheritDoc
   */
  canRefill(blockOrNodeClassName, element) {
    return (this.isTableCell(blockOrNodeClassName) && element.nodeName === this.elementTagName.toUpperCase());
  }
  /**
   * @inheritDoc
   */
  toCssText() {
    return this.buildCssTextForStyles(this.elementTagName, this.classNameToBeAdded);
  }
  /**
   * Checks if the given class name corresponds to a table cell block.
   *
   * @param {string} className - The class name to check.
   * @returns {boolean} - True if it's a table cell block, false otherwise.
   */
  isTableCell(className) {
    return className === this.cellBlockClassName;
  }
  /**
   * Converts the default border color from RGB to HEX if necessary.
   *
   * @param {Nullable<RTEFilterStyles>} styles - The styles object to be modified.
   */
  convertDefaultBorderColorFromRGBToHex(styles) {
    const defaultRGBBorder = this.stylesWithRGBColor.border;
    if (styles && styles.border && styles.border === defaultRGBBorder) {
      styles.border = this.stylesToBeFiltered.border; // Use the hex color from stylesToBeFiltered
    }
  }
}
