import { cleanObject } from '../../../../../utils/object';
import { BlockAnnotation } from '../../../dto/annotations/block-annotation.abstract';
import { RTETableColorAnnotation } from './table-color-annotation';
export var RTETableExtensionAnnotationTypeEnum;
(function (RTETableExtensionAnnotationTypeEnum) {
  RTETableExtensionAnnotationTypeEnum["TABLE"] = "EXTENSION_TABLE";
  RTETableExtensionAnnotationTypeEnum["CELL"] = "EXTENSION_TABLE_CELL";
  RTETableExtensionAnnotationTypeEnum["CAPTION"] = "EXTENSION_TABLE_CAPTION";
  RTETableExtensionAnnotationTypeEnum["COLOR"] = "EXTENSION_TABLE_COLOR";
})(RTETableExtensionAnnotationTypeEnum || (RTETableExtensionAnnotationTypeEnum = {}));
/**
 * Class representing a table annotation in the rich text editor.
 */
export class RTETableAnnotation extends BlockAnnotation {
  constructor({ backgroundColor, border, width, height, alignment, borderCollapse } = {}, useInternalStyle = true) {
    super();
    this.type = RTETableExtensionAnnotationTypeEnum.TABLE;
    this.useInternalStyle = useInternalStyle;
    this.backgroundColor = backgroundColor;
    this.border = border;
    this.width = width;
    this.height = height;
    this.alignment =
      useInternalStyle && alignment === undefined ? RTETableAnnotation.DEFAULT_ALIGNMENT : alignment;
    this.backgroundColorAnnotation = new RTETableColorAnnotation(backgroundColor);
    this.borderColorAnnotation = new RTETableColorAnnotation(border === null || border === void 0 ? void 0 : border.color);
    this.borderCollapse =
      useInternalStyle && borderCollapse === undefined
        ? RTETableAnnotation.DEFAULT_BORDER_COLLAPSE
        : borderCollapse;
  }
  /**
   * @inheritDoc
   */
  static from(value, useInternalStyle = true) {
    return [RTETableExtensionAnnotationTypeEnum.TABLE, new RTETableAnnotation(value, useInternalStyle)];
  }
  /**
   * Get the default styles for the table annotation.
   *
   * @returns {AnnotationStyle} The default styles for the table annotation.
   */
  static getDefaultStyles() {
    return {
      borderCollapse: RTETableAnnotation.DEFAULT_BORDER_COLLAPSE,
      margin: RTETableAnnotation.DEFAULT_ALIGNMENT_STYLE_VALUE,
    };
  }
  /**
   * 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();
  }
  /**
   * @inheritDoc
   */
  clone() {
    return new RTETableAnnotation({
      backgroundColor: this.backgroundColor,
      border: this.border,
      width: this.width,
      height: this.height,
      alignment: this.alignment,
      borderCollapse: this.borderCollapse,
    }, this.useInternalStyle);
  }
  /**
   * @inheritDoc
   */
  toJSON() {
    return {
      table: cleanObject({
        backgroundColor: this.backgroundColor,
        border: this.border,
        width: this.width,
        height: this.height,
        alignment: this.alignment,
        borderCollapse: this.borderCollapse,
      }),
    };
  }
  /**
   * @inheritDoc
   */
  renderStyle() {
    const backgroundColor = this.backgroundColorAnnotation.renderStyle().color;
    const tableStyles = Object.assign({ border: this.borderStr, width: this.width, height: this.height, borderCollapse: this.borderCollapse }, this.renderAlignmentStyle());
    backgroundColor && (tableStyles.backgroundColor = backgroundColor);
    return cleanObject(tableStyles);
  }
  /**
   * @inheritDoc
   */
  renderClass() {
    return 'v-rte--extension-table';
  }
  /**
   * Generates the alignment styles for the table.
   *
   * @returns {Record<string, string>} The alignment styles.
   */
  renderAlignmentStyle() {
    if (!this.alignment)
      return {};
    return {
      margin: this.alignment === 'center'
        ? RTETableAnnotation.DEFAULT_ALIGNMENT_STYLE_VALUE
        : this.alignment === 'left'
          ? '0'
          : '0 0 0 auto',
    };
  }
}
RTETableAnnotation.DEFAULT_BORDER_COLLAPSE = 'collapse';
RTETableAnnotation.DEFAULT_ALIGNMENT = 'center';
RTETableAnnotation.DEFAULT_ALIGNMENT_STYLE_VALUE = '0 auto';
