import { useReactFlow } from "../../../../xyflow/react.js";
import { useCallback, useEffect, useRef } from "react";
import { waypointsEqual } from "../waypoints.js";
function useEdgeWaypointWriter(edgeId, onChange) {
    const { setEdges } = useReactFlow();
    const onChangeRef = useRef(onChange);
    useEffect(()=>{
        onChangeRef.current = onChange;
    });
    return useCallback((next)=>{
        const consumer = onChangeRef.current;
        if (consumer) return void consumer(next);
        setEdges((edges)=>{
            let changed = false;
            const updated = edges.map((edge)=>{
                if (edge.id !== edgeId) return edge;
                const current = edge.data?.waypoints ?? [];
                if (waypointsEqual(current, next)) return edge;
                changed = true;
                return {
                    ...edge,
                    data: {
                        ...edge.data,
                        waypoints: next
                    }
                };
            });
            return changed ? updated : edges;
        });
    }, [
        edgeId,
        setEdges
    ]);
}
export { useEdgeWaypointWriter };
