import { RTEBlock } from '../../../dto/blocks/block.abstract';
import { RTETableCellAnnotation } from '../annotations/table-cell-annotation';
import { RTETableExtensionAnnotationTypeEnum } from '../annotations/table-annotation';
/**
 * Class representing a table cell block in the rich text editor.
 */
export class RTETableCellBlock extends RTEBlock {
  constructor(id, options) {
    super(id);
    this.type = 'table-cell';
    this.children = [];
    const { autoMatchFormat } = options || { autoMatchFormat: true };
    if (autoMatchFormat) {
      this.annotationMap.set(RTETableExtensionAnnotationTypeEnum.CELL, new RTETableCellAnnotation());
    }
  }
  /**
   * @inheritDoc
   */
  static from(block, options) {
    var _a;
    const tableCellBlock = new RTETableCellBlock(block.id, options);
    super.convertAnnotationsToMap(tableCellBlock.annotationMap, Object.assign({}, block.annotations));
    if ((_a = block.annotations) === null || _a === void 0 ? void 0 : _a.tableCell) {
      tableCellBlock.annotationMap.set(...RTETableCellAnnotation.from(block.annotations.tableCell, options === null || options === void 0 ? void 0 : options.autoMatchFormat));
    }
    tableCellBlock.children = super.generateBlockChildren(block.children, options, tableCellBlock);
    return tableCellBlock;
  }
  /**
   * @inheritDoc
   */
  clone(parent) {
    const clonedBlock = new RTETableCellBlock(this.id);
    clonedBlock.children = this.children.map((child) => child.clone(clonedBlock));
    clonedBlock.parent = parent;
    clonedBlock.annotationMap = super.cloneAnnotations();
    return clonedBlock;
  }
  /**
   * @inheritDoc
   */
  getLastNode() {
    return this.children[this.children.length - 1].getLastNode();
  }
  /**
   * @inheritDoc
   */
  isNotEmpty() {
    return this.children.length > 0;
  }
  /**
   * Get the rowSpan of the table cell
   *
   * @returns {number} - rowSpan value
   */
  get rowSpan() {
    const rowSpan = this.getCustomAttributeByName('rowspan');
    return !isNaN(Number(rowSpan)) ? Math.max(Number(rowSpan), 1) : 1;
  }
  /**
   * Get the colSpan of the table cell
   *
   * @returns {number} - colSpan value
   */
  get colSpan() {
    const colSpan = this.getCustomAttributeByName('colspan');
    return !isNaN(Number(colSpan)) ? Math.max(Number(colSpan), 1) : 1;
  }
  /**
   * @inheritDoc
   */
  toHtml(options) {
    const childrenHtml = this.children.map((child) => child.toHtml(options)).join('');
    const attrStr = super.generateAttributeString(options);
    return `<td${attrStr}>${childrenHtml}</td>`;
  }
  /**
   * @inheritDoc
   */
  toJSON() {
    return Object.assign(Object.assign({}, super.toJSON()), { type: this.type, children: this.children.map((child) => child.toJSON()) });
  }
  /**
   * Get custom attribute by name
   *
   * @param {string} attrName - attribute name
   * @returns {Nullable<string>} - attribute value
   */
  getCustomAttributeByName(attrName) {
    const customAttrAnnotation = this.getCustomAttributesAnnotation();
    if (customAttrAnnotation) {
      const customAttributes = customAttrAnnotation.toJSON()['customAttribute'] || {};
      return customAttributes[attrName];
    }
  }
}
