import { Command } from './Command';
import { AppNodeType } from '../types/node';

/**
 * Command to add a node to the canvas
 */
export class AddNodeCommand implements Command {
    private node: AppNodeType;
    private setNodes: (updater: (nodes: AppNodeType[]) => AppNodeType[]) => void;

    constructor(
        node: AppNodeType,
        setNodes: (updater: (nodes: AppNodeType[]) => AppNodeType[]) => void
    ) {
        this.node = node;
        this.setNodes = setNodes;
    }

    execute(): void {
        this.setNodes((nodes) => [...nodes, this.node]);
    }

    undo(): void {
        this.setNodes((nodes) => nodes.filter(n => n.id !== this.node.id));
    }

    getDescription(): string {
        return `Add node: ${this.node.id}`;
    }
}
