import { Command } from './Command';
import { AppNodeType } from '../types/node';
import { XYPosition } from '@xyflow/react';

/**
 * Command to move a node
 */
export class MoveNodeCommand implements Command {
    private nodeId: string;
    private oldPosition: XYPosition;
    private newPosition: XYPosition;
    private setNodes: (updater: (nodes: AppNodeType[]) => AppNodeType[]) => void;

    constructor(
        nodeId: string,
        oldPosition: XYPosition,
        newPosition: XYPosition,
        setNodes: (updater: (nodes: AppNodeType[]) => AppNodeType[]) => void
    ) {
        this.nodeId = nodeId;
        this.oldPosition = oldPosition;
        this.newPosition = newPosition;
        this.setNodes = setNodes;
    }

    execute(): void {
        this.setNodes((nodes) => 
            nodes.map(n => 
                n.id === this.nodeId
                    ? { ...n, position: this.newPosition }
                    : n
            )
        );
    }

    undo(): void {
        this.setNodes((nodes) => 
            nodes.map(n => 
                n.id === this.nodeId
                    ? { ...n, position: this.oldPosition }
                    : n
            )
        );
    }

    getDescription(): string {
        return `Move node: ${this.nodeId}`;
    }
}
