import { ActionHandleStrategy } from '../action-handle-strategy.abstract';
/**
 * The delete node content contains some common functions that used for image block delete the image node or text block delete text node.
 */
export class BlockDeleteNodeContentStrategy extends ActionHandleStrategy {
  /**
   * Returns the previous node relative to the current node in a rich text editor structure.
   *
   * @param {RTENode} currentNode - An object of type `RTENode`.
   * @returns {Nullable<RTENode>} the previous block last node
   */
  getPreviousNode(currentNode) {
    const currentBlock = currentNode.parent;
    const currentIndex = currentBlock.children.indexOf(currentNode);
    if (currentIndex > 0) {
      return currentBlock.children[currentIndex - 1];
    }
    else if (currentBlock.parent) {
      const previousBlock = this.getPreviousBlock(currentBlock);
      if (previousBlock) {
        return this.getBlockLastNode(previousBlock);
      }
    }
  }
  /**
   * Returns the next node relative to the current node in a rich text editor structure.
   *
   * @param {RTENode} currentNode - An object of type `RTENode`.
   * @returns {Nullable<RTENode>} the next block first node
   */
  getNextNode(currentNode) {
    const currentBlock = currentNode.parent;
    const currentIndex = currentBlock.children.indexOf(currentNode);
    if (currentBlock.children[currentIndex + 1]) {
      return currentBlock.children[currentIndex + 1];
    }
    else if (currentBlock.parent) {
      const nextBlock = this.getNextBlock(currentBlock);
      if (nextBlock) {
        return this.getBlockFirstNode(nextBlock);
      }
    }
  }
  /**
   * Determines if a given node is the first node within the current state.
   *
   * @param {RTENode} currentNode - An object representing a node in a rich text editor (RTE).
   * @returns {boolean} True or false;
   */
  isContentFirstNode(currentNode) {
    const currentBlock = currentNode.parent;
    const currentIndex = currentBlock.children.indexOf(currentNode);
    const parent = currentBlock.parent;
    if (currentBlock.isListItemBlock()) {
      const listParent = parent.parent;
      return (currentIndex === 0 &&
        Array.isArray(parent.children) &&
        parent.children[0] === currentBlock &&
        listParent['type'] !== 'list-item' &&
        Array.isArray(listParent.children) &&
        listParent.children[0] === parent);
    }
    else {
      return (currentIndex === 0 && Array.isArray(parent.children) && parent.children[0] === currentBlock);
    }
  }
  /**
   * TODO: The getPreviousBlock add nest list support
   * This function retrieves the previous block in a rich text editor, considering different scenarios such as being in a bullet list.
   *
   * @param {RTEBlock} currentBlock - Used to retrieve the previous block relative to the given `currentBlock` within a rich text editor context.
   * @returns {Nullable<RTEBlock>} The previous block if existing
   */
  getPreviousBlock(currentBlock) {
    if (currentBlock && currentBlock.parent && currentBlock.parent.children) {
      const currentBlockIndex = currentBlock.parent.children.indexOf(currentBlock);
      if (currentBlockIndex > 0) {
        return currentBlock.parent.children[currentBlockIndex - 1];
      }
      else if (this.isRTEBlock(currentBlock.parent) && currentBlockIndex === 0) {
        return this.getPreviousBlock(currentBlock.parent);
      }
    }
  }
  /**
   * Returns the last node within a given RTE block, handling different block types accordingly.
   *
   * @param {RTEBlock} currentBlock - Returns the last node within that block.
   * @returns {Nullable<RTENode>} Returns the last node of the given `currentBlock` object.
   */
  getBlockLastNode(currentBlock) {
    return currentBlock.getLastNode();
  }
  /**
   * Returns the first node within a given RTE block, handling different block types accordingly.
   *
   * @param {RTEBlock} currentBlock - Returns the first node within that block.
   * @returns {Nullable<RTENode>} Returns the first node of the given `currentBlock` object.
   */
  getBlockFirstNode(currentBlock) {
    if (currentBlock.type === 'number-list' || currentBlock.type === 'bullet-list') {
      if (currentBlock.children) {
        const firstListItem = currentBlock.children[0];
        return this.getBlockFirstNode(firstListItem);
      }
    }
    else {
      if (currentBlock.children) {
        return currentBlock.children[0];
      }
    }
  }
  /**
   * This function retrieves the next block in a rich text editor, considering different scenarios such as being in a bullet list.
   *
   * @param {RTEBlock} currentBlock - Used to retrieve the next block relative to the given `currentBlock` within a rich text editor context.
   * @returns {Nullable<RTEBlock>} The next block if existing
   */
  getNextBlock(currentBlock) {
    if (currentBlock && currentBlock.parent && currentBlock.parent.children) {
      const currentBlockIndex = currentBlock.parent.children.indexOf(currentBlock);
      if (currentBlock.parent.children[currentBlockIndex + 1]) {
        return currentBlock.parent.children[currentBlockIndex + 1];
      }
      else if (currentBlock.type === 'list-item' &&
        currentBlock.parent.children.length === currentBlockIndex + 1) {
        return this.getNextBlock(currentBlock.parent);
      }
    }
  }
  /**
   * Checks if the given instance is not the root content block.
   *
   * @param {RTEBlock | VegaRTEContent} block - The instance to check.
   * @returns {boolean} True if the block is not the root content block, false otherwise.
   */
  isRTEBlock(block) {
    return block && block.dtoName !== 'VegaRTEContent';
  }
}
