import { Command } from './Command';
import { Edge } from '@xyflow/react';

/**
 * Command to delete an edge from the canvas
 */
export class DeleteEdgeCommand implements Command {
    private edgeId: string;
    private deletedEdge: Edge | null = null;
    private setEdges: (updater: (edges: Edge[]) => Edge[]) => void;
    private getEdges: () => Edge[];

    constructor(
        edgeId: string,
        setEdges: (updater: (edges: Edge[]) => Edge[]) => void,
        getEdges: () => Edge[]
    ) {
        this.edgeId = edgeId;
        this.setEdges = setEdges;
        this.getEdges = getEdges;
    }

    execute(): void {
        const edges = this.getEdges();
        this.deletedEdge = edges.find(e => e.id === this.edgeId) || null;
        this.setEdges((edges) => edges.filter(e => e.id !== this.edgeId));
    }

    undo(): void {
        if (this.deletedEdge) {
            this.setEdges((edges) => [...edges, this.deletedEdge!]);
        }
    }

    getDescription(): string {
        return `Delete edge: ${this.edgeId}`;
    }
}
