import { AppendChildrenAction } from '../../actions/append-child-nodes-action';
import { InsertChildrenAfterAction } from '../../actions/insert-children-after-block';
import { RemoveChildrenAction } from '../../actions/remove-child-action';
import { ActionHandleStrategy } from '../action-handle-strategy.abstract';
/**
 * Convert the selected blocks to list strategy.
 */
export class BlockTransformToListStrategy extends ActionHandleStrategy {
  /**
   * This method will create a empty list block and append all selected blocks as list item block.
   *
   * @param {TransformListAction} action - The transform list action instance.
   * @param {RTETextBlock} target - The first item of the selected block.
   */
  handleAction(action, target) {
    const contentState = target.parent;
    const bulletList = contentState.createList(action.listType);
    const selectedBlocks = action.selectedBlocks;
    const listItems = selectedBlocks.map((block) => contentState.toListItem(block));
    bulletList.apply(new AppendChildrenAction(listItems));
    contentState.apply(new InsertChildrenAfterAction(action.selectedBlocks[0], bulletList));
    selectedBlocks.forEach((block) => {
      block.parent.apply(new RemoveChildrenAction(block));
    });
  }
}
