import { cleanObject } from '../../../../../utils/object';
import { RTETableExtensionAnnotationTypeEnum } from './table-annotation';
import { BlockAnnotation } from '../../../dto/annotations/block-annotation.abstract';
import { RTETableColorAnnotation } from './table-color-annotation';
/**
 * Class representing a table cell annotation in the rich text editor.
 */
export class RTETableCellAnnotation extends BlockAnnotation {
  constructor({ backgroundColor, padding, border, width, height, textAlign, verticalAlign, minWidth, boxSizing, } = {}, useInternalStyle = true) {
    super();
    this.type = RTETableExtensionAnnotationTypeEnum.CELL;
    this.useInternalStyle = useInternalStyle;
    this.backgroundColor = backgroundColor;
    this.width = width;
    this.height = height;
    this.border =
      useInternalStyle && border === undefined ? RTETableCellAnnotation.DEFAULT_BORDER : border;
    this.padding =
      useInternalStyle && padding === undefined ? RTETableCellAnnotation.DEFAULT_PADDING : padding;
    this.textAlign =
      useInternalStyle && textAlign === undefined
        ? RTETableCellAnnotation.DEFAULT_HORIZONTAL_TEXT_ALIGN
        : textAlign;
    this.verticalAlign =
      useInternalStyle && verticalAlign === undefined
        ? RTETableCellAnnotation.DEFAULT_VERTICAL_TEXT_ALIGN
        : verticalAlign;
    this.minWidth =
      useInternalStyle && minWidth === undefined
        ? RTETableCellAnnotation.DEFAULT_CELL_MIN_WIDTH
        : minWidth;
    this.boxSizing =
      useInternalStyle && boxSizing === undefined
        ? RTETableCellAnnotation.DEFAULT_BOX_SIZING
        : boxSizing;
  }
  /**
   * @inheritDoc
   */
  static from(cellAnnotation, useInternalStyle) {
    return [
      RTETableExtensionAnnotationTypeEnum.CELL,
      new RTETableCellAnnotation(cellAnnotation, useInternalStyle),
    ];
  }
  /**
   * Get the default styles for the table annotation.
   *
   * @param {boolean} isHexColor - Whether to return color values as hex codes.
   * @returns {AnnotationStyle} The default styles for the table annotation.
   */
  static getDefaultStyles(isHexColor = true) {
    return {
      padding: RTETableCellAnnotation.DEFAULT_PADDING,
      textAlign: RTETableCellAnnotation.DEFAULT_HORIZONTAL_TEXT_ALIGN,
      verticalAlign: RTETableCellAnnotation.DEFAULT_VERTICAL_TEXT_ALIGN,
      minWidth: RTETableCellAnnotation.DEFAULT_CELL_MIN_WIDTH,
      boxSizing: RTETableCellAnnotation.DEFAULT_BOX_SIZING,
      border: `${RTETableCellAnnotation.DEFAULT_BORDER.style} ${RTETableCellAnnotation.DEFAULT_BORDER.width} ${isHexColor
        ? RTETableCellAnnotation.DEFAULT_BORDER.color
        : RTETableCellAnnotation.DEFAULT_BORDER_RGB_COLOR}`,
    };
  }
  /**
   * Generates the CSS border string from the border properties.
   *
   * @returns {Nullable<string>} The CSS border string.
   */
  get borderStr() {
    if (!this.border)
      return null;
    return `${this.border.style || ''} ${this.border.width || ''} ${this.borderColorAnnotation.renderStyle().color}`.trim();
  }
  /**
   * Sets the background color using a color key.
   *
   * @param {Nullable<string>} colorKey - The key representing the background color.
   */
  set backgroundColor(colorKey) {
    this._backgroundColor = colorKey;
    this.backgroundColorAnnotation = new RTETableColorAnnotation(colorKey);
  }
  /**
   * Gets the background color value.
   *
   * @returns {Nullable<string>} The background color value.
   */
  get backgroundColor() {
    return this._backgroundColor;
  }
  /**
   * Sets the border properties and updates the border color annotation accordingly.
   *
   * @param {Nullable<VegaRTETableBorderType>} border - The border properties to set.
   */
  set border(border) {
    this._border = border;
    this.borderColorAnnotation = new RTETableColorAnnotation(border === null || border === void 0 ? void 0 : border.color);
  }
  /**
   * Gets the border properties.
   *
   * @returns {Nullable<VegaRTETableBorderType>} The border properties.
   */
  get border() {
    return this._border;
  }
  /**
   * @inheritDoc
   */
  clone() {
    return new RTETableCellAnnotation({
      backgroundColor: this.backgroundColor,
      padding: this.padding,
      border: this.border,
      width: this.width,
      height: this.height,
      textAlign: this.textAlign,
      verticalAlign: this.verticalAlign,
      minWidth: this.minWidth,
      boxSizing: this.boxSizing,
    }, this.useInternalStyle);
  }
  /**
   * @inheritDoc
   */
  toJSON() {
    return {
      tableCell: cleanObject({
        backgroundColor: this.backgroundColor,
        padding: this.padding,
        border: this.border,
        width: this.width,
        height: this.height,
        textAlign: this.textAlign,
        verticalAlign: this.verticalAlign,
        minWidth: this.minWidth,
        boxSizing: this.boxSizing,
      }),
    };
  }
  /**
   * @inheritDoc
   */
  renderStyle(options) {
    const backgroundColor = this.backgroundColorAnnotation.renderStyle().color;
    const style = {
      padding: this.padding,
      border: this.borderStr,
      width: this.width,
      height: this.height,
      textAlign: this.textAlign,
      verticalAlign: this.verticalAlign,
      minWidth: this.minWidth,
      boxSizing: this.boxSizing,
    };
    if (backgroundColor) {
      style.backgroundColor = backgroundColor;
    }
    if (!(options === null || options === void 0 ? void 0 : options.standalone)) {
      return cleanObject(this.removeDefaultValues(style), { cleanEmptyString: true });
    }
    return cleanObject(style, { cleanEmptyString: true });
  }
  /**
   * @inheritDoc
   */
  renderClass() {
    return null;
  }
  /**
   * Removes default values from the style object to avoid redundancy.
   *
   * @param {Record<string, Nullable<string>>} style - The style object to clean.
   * @returns {Record<string, Nullable<string>>} The cleaned style object.
   */
  removeDefaultValues(style) {
    const cleanedStyle = Object.assign({}, style);
    if (cleanedStyle.padding === RTETableCellAnnotation.DEFAULT_PADDING) {
      delete cleanedStyle.padding;
    }
    if (cleanedStyle.border ===
      `${RTETableCellAnnotation.DEFAULT_BORDER.style} ${RTETableCellAnnotation.DEFAULT_BORDER.width} ${RTETableCellAnnotation.DEFAULT_BORDER.color}`) {
      delete cleanedStyle.border;
    }
    if (cleanedStyle.textAlign === RTETableCellAnnotation.DEFAULT_HORIZONTAL_TEXT_ALIGN) {
      delete cleanedStyle.textAlign;
    }
    if (cleanedStyle.verticalAlign === RTETableCellAnnotation.DEFAULT_VERTICAL_TEXT_ALIGN) {
      delete cleanedStyle.verticalAlign;
    }
    if (cleanedStyle.minWidth === RTETableCellAnnotation.DEFAULT_CELL_MIN_WIDTH) {
      delete cleanedStyle.minWidth;
    }
    if (cleanedStyle.boxSizing === RTETableCellAnnotation.DEFAULT_BOX_SIZING) {
      delete cleanedStyle.boxSizing;
    }
    return cleanedStyle;
  }
}
RTETableCellAnnotation.DEFAULT_PADDING = '12px';
RTETableCellAnnotation.DEFAULT_BORDER = {
  style: 'solid',
  width: '1px',
  color: '#ddd',
};
RTETableCellAnnotation.DEFAULT_BORDER_RGB_COLOR = 'rgb(221, 221, 221)'; // Equivalent RGB for #ddd
RTETableCellAnnotation.DEFAULT_HORIZONTAL_TEXT_ALIGN = 'left';
RTETableCellAnnotation.DEFAULT_VERTICAL_TEXT_ALIGN = 'middle';
RTETableCellAnnotation.DEFAULT_CELL_MIN_WIDTH = '55px';
RTETableCellAnnotation.DEFAULT_BOX_SIZING = 'border-box';
