import { RemoveChildrenAction } from '../../actions/remove-child-action';
import { ActionHandleStrategy } from '../action-handle-strategy.abstract';
/**
 * Remove the node from the text block or image block strategy.
 */
export class RemoveChildrenStrategy extends ActionHandleStrategy {
  /**
   * Remove the node or block from the parent.
   * Remove the block if the block children is empty after delete the current element.
   *
   * @param {RemoveChildrenAction} action - The remove child action instance.
   * @param {RTEBlock | VegaRTEContent} target - The parent block of the node that will be removed.
   */
  handleAction(action, target) {
    const arrayFixed = target.children;
    target.children = arrayFixed.filter((node) => node !== action.entityToBeRemoved);
    if (target.children.length === 0 && 'parent' in target && target.parent) {
      if (!target.isListItemBlock()) {
        target.parent.apply(new RemoveChildrenAction(target));
      }
      else if (!target.nestList || !target.nestList.length) {
        target.parent.apply(new RemoveChildrenAction(target));
      }
    }
  }
}
