import { RTEBlock } from './block.abstract';
import { RTEImageNode } from '../nodes/image-node';
import { ModifyContentActionType } from '../actions/modify-content-action.abstract';
import { generateUUID } from '../../../../utils/misc';
import { ActionHandleStrategyRegistry } from '../action-handle-strategies/action-handle-strategy-registry';
import { BlockMergeNodesStrategy } from '../action-handle-strategies/modify-content-strategies/block-merge-nodes-strategy';
import { BlockReplaceNodesStrategy } from '../action-handle-strategies/modify-content-strategies/block-replace-nodes-strategy';
import { BlockSplitWithImageNodeStrategy } from '../action-handle-strategies/modify-content-strategies/block-split-with-image-node-strategy';
import { ImageInsertLineBreakStrategy } from '../action-handle-strategies/modify-content-strategies/image-insert-line-break-strategy';
import { ImageInsertLineBreakWithBlocksStrategy } from '../action-handle-strategies/modify-content-strategies/image-insert-line-break-with-blocks-strategy';
import { ImageBlockInsertImageStrategy } from '../action-handle-strategies/modify-content-strategies/image-block-insert-image-strategy';
import { HorizontalAlignmentAnnotationAction } from '../actions/horizontal-alignment-annotation-action';
import { BlockUpdateHorizontalAlignmentStrategy } from '../action-handle-strategies/apply-annotation-strategies/block-update-horizontal-alignment-strategy';
/**
 * Image block
 */
export class RTEImageBlock extends RTEBlock {
  constructor(id) {
    super(id);
    this.dtoName = RTEImageBlock.dtoName;
    this.type = 'image';
    this.children = [];
  }
  /**
   * Converts a VegaRTEImageBlock object into an RTEImageBlock object by mapping nodes and creating annotations.
   *
   * @param {VegaRTEImageBlock} block - The block object to be converted.
   * @param {VegaRTETransformOptions} options - Optional transformation options.
   * @returns {RTEImageBlock} An instance of `RTEImageBlock`
   */
  static from(block, options = { autoMatchFormat: true }) {
    const imageBlock = new RTEImageBlock(block.id);
    const { annotations } = block;
    imageBlock.children = block.nodes.map((image) => RTEImageNode.from(image, imageBlock, options));
    if (annotations) {
      super.convertAnnotationsToMap(imageBlock.annotationMap, annotations);
    }
    return imageBlock;
  }
  /**
   * @inheritDoc
   */
  toJSON() {
    return Object.assign(Object.assign({}, super.toJSON()), { type: 'image', nodes: this.children.map((node) => node.toJSON()) });
  }
  /**
   * @inheritDoc
   */
  toHtml(options) {
    const attrStr = super.generateAttributeString(options);
    const children = this.children.map((node) => node.toHtml(options)).join('');
    return super.shouldRenderAsInternalWrapper()
      ? children
      : [`<div${attrStr}>`, children, `</div>`].join('');
  }
  /**
   * @inheritDoc
   */
  isNotEmpty() {
    return this.children.length > 0 && this.children.some((node) => node.url.length > 0);
  }
  /**
   * @inheritDoc
   */
  getLastNode() {
    return this.children[this.children.length - 1];
  }
  /**
   * Create a new image block.
   *
   * @returns {RTEImageBlock} - The empty image block.
   */
  createNewImageBlock() {
    return new RTEImageBlock(generateUUID());
  }
  /**
   * @inheritDoc
   */
  clone(parent) {
    const clonedBlock = new RTEImageBlock(this.id);
    clonedBlock.children = this.children.map((node) => node.clone(clonedBlock));
    clonedBlock.annotationMap = super.cloneAnnotations();
    clonedBlock.parent = parent;
    return clonedBlock;
  }
}
(() => {
  ActionHandleStrategyRegistry.register(ModifyContentActionType.MERGE_TWO_BLOCKS_NODES, RTEImageBlock.name, new BlockMergeNodesStrategy());
  ActionHandleStrategyRegistry.register(ModifyContentActionType.REPLACE_CHILD_NODES, RTEImageBlock.name, new BlockReplaceNodesStrategy());
  ActionHandleStrategyRegistry.register(ModifyContentActionType.SPLIT_BLOCK_WITH_NODE, RTEImageBlock.name, new BlockSplitWithImageNodeStrategy());
  ActionHandleStrategyRegistry.register(ModifyContentActionType.LINE_BREAK_SINGLE_BLOCK, RTEImageBlock.name, new ImageInsertLineBreakStrategy());
  ActionHandleStrategyRegistry.register(ModifyContentActionType.LINE_BREAK_MULTIPLE_BLOCKS, RTEImageBlock.name, new ImageInsertLineBreakWithBlocksStrategy());
  ActionHandleStrategyRegistry.register(ModifyContentActionType.INSERT_IMAGE_TO_BLOCK, RTEImageBlock.name, new ImageBlockInsertImageStrategy());
  ActionHandleStrategyRegistry.register(HorizontalAlignmentAnnotationAction.name, RTEImageBlock.name, new BlockUpdateHorizontalAlignmentStrategy());
})();
RTEImageBlock.dtoName = 'RTEImageBlock';
