import { RTETextNode } from '../nodes/text-node';
import { RTEListBlock } from './list-block';
import { ModifyContentActionType } from '../actions/modify-content-action.abstract';
import { AppendChildrenAction } from '../actions/append-child-nodes-action';
import { generateUUID } from '../../../../utils/misc';
import { RTETextBlock } from './text-block';
import { ActionHandleStrategyRegistry } from '../action-handle-strategies/action-handle-strategy-registry';
import { BlockInsertLineBreakStrategy } from '../action-handle-strategies/modify-content-strategies/block-insert-line-break-strategy';
import { BlockInsertLineBreakWithBlocksStrategy } from '../action-handle-strategies/modify-content-strategies/block-insert-line-break-with-blocks-strategy';
import { ListItemRemoveNestListStrategy } from '../action-handle-strategies/modify-content-strategies/list-item-remove-nest-list-strategy';
import { BlockSplitWithTextNodeStrategy } from '../action-handle-strategies/modify-content-strategies/block-split-with-text-node-strategy';
import { BlockReplaceNodesStrategy } from '../action-handle-strategies/modify-content-strategies/block-replace-nodes-strategy';
import { ListItemReplaceNestListStrategy } from '../action-handle-strategies/modify-content-strategies/list-item-replace-nest-list-strategy';
import { BlockMergeNodesStrategy } from '../action-handle-strategies/modify-content-strategies/block-merge-nodes-strategy';
import { ListItemInsertImageStrategy } from '../action-handle-strategies/modify-content-strategies/list-item-insert-image-strategy';
import { isNonNullable } from '../../../../types/type-guard';
import { TextStyleAnnotationAction } from '../actions/text-style-annotation-action';
import { BlockUpdateTextStyleStrategy } from '../action-handle-strategies/apply-annotation-strategies/block-update-text-style-strategy';
import { HorizontalAlignmentAnnotationAction } from '../actions/horizontal-alignment-annotation-action';
import { BlockUpdateHorizontalAlignmentStrategy } from '../action-handle-strategies/apply-annotation-strategies/block-update-horizontal-alignment-strategy';
/**
 * List item block
 */
export class RTEListItemBlock extends RTETextBlock {
  constructor(id, nestList, options) {
    super(id, 'list-item', options);
    this.dtoName = RTEListItemBlock.dtoName;
    /**
     * TODO: Refactor the list item properties children and nestList, merge two properties together.
     *
     * 1. Update the type of `children` to include `RTEListBlock` as a possible type.
     * 2. The list block children should text block array and nest list block array, The text block should wrap a `li` tag when rendering.
     */
    this.nestList = [];
    this.nestList = nestList;
  }
  /**
   * Converts a VegaRTEListItemBlock object to an RTEListItemBlock
   *
   * @param {VegaRTEListBlock} block - Converts a `VegaRTEListItemBlock` object into an `RTEListItemBlock`.
   * @param {VegaRTETransformOptions} options - Optional transformation options.
   * @returns {RTEListItemBlock} Return an instance of `RTEListItemBlock`
   */
  static from(block, options = { autoMatchFormat: true }) {
    const listItemBlock = new RTEListItemBlock(block.id, undefined, options);
    listItemBlock.children = block.nodes.map((richText) => RTETextNode.from(richText, listItemBlock, options));
    if (block.children) {
      listItemBlock.nestList = block.children.map((child) => {
        const listBlock = RTEListBlock.from(child);
        listBlock.parent = listItemBlock;
        return listBlock;
      });
    }
    const { annotations } = block;
    if (annotations) {
      Object.entries(annotations).forEach(([type, value]) => {
        const item = this.createAnnotationEntity(type, value);
        if (isNonNullable(item)) {
          listItemBlock.annotationMap.set(...item);
        }
      });
    }
    return listItemBlock;
  }
  /**
   * @inheritDoc
   */
  toJSON() {
    var _a;
    return Object.assign(Object.assign({}, super.toJSON()), { type: 'list-item', children: (_a = this.nestList) === null || _a === void 0 ? void 0 : _a.map((block) => block.toJSON()) });
  }
  /**
   * @inheritDoc
   */
  toHtml(options) {
    const attrStr = super.generateAttributeString(options);
    return [`<li${attrStr}>`, this.getChildrenHtml(options), `</li>`].join('');
  }
  /**
   * @inheritDoc
   */
  cloneWithNodes(nodes) {
    const block = new RTEListItemBlock(generateUUID());
    block.children = [];
    block.apply(new AppendChildrenAction(nodes));
    return block;
  }
  /**
   * @inheritDoc
   */
  getLastNode() {
    if (this.nestList && this.nestList.length > 0) {
      const childListBlock = this.nestList[this.nestList.length - 1];
      return childListBlock.getLastNode();
    }
    return super.getLastNode();
  }
  /**
   * Create a new list item block.
   *
   * @returns {RTEListItemBlock} - A list item block.
   */
  createNewListItem() {
    return new RTEListItemBlock(generateUUID());
  }
  /**
   * @inheritDoc
   */
  clone(parent) {
    var _a;
    const block = new RTEListItemBlock(this.id);
    block.children = this.children.map((node) => node.clone(block));
    block.annotationMap = super.cloneAnnotations();
    block.nestList = (_a = this.nestList) === null || _a === void 0 ? void 0 : _a.map((child) => child.clone(block));
    block.parent = parent;
    return block;
  }
}
(() => {
  ActionHandleStrategyRegistry.register(TextStyleAnnotationAction.name, RTEListItemBlock.name, new BlockUpdateTextStyleStrategy());
  ActionHandleStrategyRegistry.register(HorizontalAlignmentAnnotationAction.name, RTEListItemBlock.name, new BlockUpdateHorizontalAlignmentStrategy());
  ActionHandleStrategyRegistry.register(ModifyContentActionType.LINE_BREAK_SINGLE_BLOCK, RTEListItemBlock.name, new BlockInsertLineBreakStrategy());
  ActionHandleStrategyRegistry.register(ModifyContentActionType.LINE_BREAK_MULTIPLE_BLOCKS, RTEListItemBlock.name, new BlockInsertLineBreakWithBlocksStrategy());
  ActionHandleStrategyRegistry.register(ModifyContentActionType.DELETE_NEST_LIST, RTEListItemBlock.name, new ListItemRemoveNestListStrategy());
  ActionHandleStrategyRegistry.register(ModifyContentActionType.SPLIT_BLOCK_WITH_NODE, RTEListItemBlock.name, new BlockSplitWithTextNodeStrategy());
  ActionHandleStrategyRegistry.register(ModifyContentActionType.REPLACE_CHILD_NODES, RTEListItemBlock.name, new BlockReplaceNodesStrategy());
  ActionHandleStrategyRegistry.register(ModifyContentActionType.REPLACE_NEST_LIST, RTEListItemBlock.name, new ListItemReplaceNestListStrategy());
  ActionHandleStrategyRegistry.register(ModifyContentActionType.MERGE_TWO_BLOCKS_NODES, RTEListItemBlock.name, new BlockMergeNodesStrategy());
  ActionHandleStrategyRegistry.register(ModifyContentActionType.INSERT_IMAGE_TO_BLOCK, RTEListItemBlock.name, new ListItemInsertImageStrategy());
})();
RTEListItemBlock.dtoName = 'RTEListItemBlock';
