"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    useWaypointEditor: ()=>useWaypointEditor
});
const react_cjs_namespaceObject = require("../../../../xyflow/react.cjs");
const external_react_namespaceObject = require("react");
const external_geometry_cjs_namespaceObject = require("../geometry.cjs");
const external_waypoints_cjs_namespaceObject = require("../waypoints.cjs");
const external_useEdgeWaypointWriter_cjs_namespaceObject = require("./useEdgeWaypointWriter.cjs");
function useWaypointEditor(args) {
    const { edgeId, waypoints, pathPoints, enabled, onChange } = args;
    const { screenToFlowPosition } = (0, react_cjs_namespaceObject.useReactFlow)();
    const writeWaypoints = (0, external_useEdgeWaypointWriter_cjs_namespaceObject.useEdgeWaypointWriter)(edgeId, onChange);
    const [dragState, setDragState] = (0, external_react_namespaceObject.useState)({
        type: 'none'
    });
    const dragStartRef = (0, external_react_namespaceObject.useRef)(null);
    const rafRef = (0, external_react_namespaceObject.useRef)(null);
    const waypointsRef = (0, external_react_namespaceObject.useRef)(waypoints);
    const pathPointsRef = (0, external_react_namespaceObject.useRef)(pathPoints);
    (0, external_react_namespaceObject.useEffect)(()=>{
        waypointsRef.current = waypoints;
        pathPointsRef.current = pathPoints;
    });
    const updateWaypoints = (0, external_react_namespaceObject.useCallback)((next)=>{
        if ((0, external_waypoints_cjs_namespaceObject.waypointsEqual)(waypointsRef.current, next)) return;
        writeWaypoints(next);
    }, [
        writeWaypoints
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (!enabled || 'none' === dragState.type) return;
        const handleMouseMove = (e)=>{
            if (!dragStartRef.current) return;
            if (null !== rafRef.current) cancelAnimationFrame(rafRef.current);
            rafRef.current = requestAnimationFrame(()=>{
                const start = screenToFlowPosition(dragStartRef.current, {
                    snapToGrid: false
                });
                const current = screenToFlowPosition({
                    x: e.clientX,
                    y: e.clientY
                }, {
                    snapToGrid: false
                });
                const delta = {
                    x: current.x - start.x,
                    y: current.y - start.y
                };
                if ('waypoint' === dragState.type) {
                    const initial = dragState.initialWaypoints[dragState.waypointIndex];
                    if (!initial) return;
                    updateWaypoints((0, external_waypoints_cjs_namespaceObject.moveWaypoint)(dragState.initialWaypoints, dragState.waypointIndex, {
                        x: initial.x + delta.x,
                        y: initial.y + delta.y
                    }));
                } else if ('segment' === dragState.type) updateWaypoints((0, external_waypoints_cjs_namespaceObject.moveSegmentByDelta)(dragState.initialWaypoints, dragState.segment, delta, dragState.initialPathPoints));
            });
        };
        const handleMouseUp = ()=>{
            if (null !== rafRef.current) {
                cancelAnimationFrame(rafRef.current);
                rafRef.current = null;
            }
            setDragState({
                type: 'none'
            });
            dragStartRef.current = null;
        };
        window.addEventListener('mousemove', handleMouseMove);
        window.addEventListener('mouseup', handleMouseUp);
        return ()=>{
            window.removeEventListener('mousemove', handleMouseMove);
            window.removeEventListener('mouseup', handleMouseUp);
            if (null !== rafRef.current) cancelAnimationFrame(rafRef.current);
        };
    }, [
        enabled,
        dragState,
        updateWaypoints,
        screenToFlowPosition
    ]);
    const waypointHandlers = (0, external_react_namespaceObject.useMemo)(()=>({
            onMouseDown: (index, e)=>{
                if (!enabled) return;
                e.stopPropagation();
                e.preventDefault();
                dragStartRef.current = {
                    x: e.clientX,
                    y: e.clientY
                };
                setDragState({
                    type: 'waypoint',
                    waypointIndex: index,
                    initialWaypoints: [
                        ...waypointsRef.current
                    ]
                });
            },
            onDoubleClick: (index)=>{
                if (!enabled) return;
                updateWaypoints((0, external_waypoints_cjs_namespaceObject.removeWaypoint)(waypointsRef.current, index));
            }
        }), [
        enabled,
        updateWaypoints
    ]);
    const segmentHandlers = (0, external_react_namespaceObject.useMemo)(()=>({
            onMouseDown: (segment, e)=>{
                if (!enabled) return;
                e.stopPropagation();
                e.preventDefault();
                dragStartRef.current = {
                    x: e.clientX,
                    y: e.clientY
                };
                const materialized = (0, external_waypoints_cjs_namespaceObject.materializePathWaypoints)(pathPointsRef.current, waypointsRef.current);
                setDragState({
                    type: 'segment',
                    segment,
                    initialWaypoints: materialized,
                    initialPathPoints: [
                        ...pathPointsRef.current
                    ]
                });
            },
            onDoubleClick: (segmentIndex, e)=>{
                if (!enabled) return;
                e.stopPropagation();
                e.preventDefault();
                const flowPoint = screenToFlowPosition({
                    x: e.clientX,
                    y: e.clientY
                });
                const snapped = (0, external_geometry_cjs_namespaceObject.snapPointToGrid)(flowPoint);
                updateWaypoints((0, external_waypoints_cjs_namespaceObject.insertWaypoint)(pathPointsRef.current, waypointsRef.current, segmentIndex, snapped));
            }
        }), [
        enabled,
        screenToFlowPosition,
        updateWaypoints
    ]);
    const isDragging = enabled && 'none' !== dragState.type;
    return (0, external_react_namespaceObject.useMemo)(()=>({
            isDragging,
            waypointHandlers,
            segmentHandlers
        }), [
        isDragging,
        waypointHandlers,
        segmentHandlers
    ]);
}
exports.useWaypointEditor = __webpack_exports__.useWaypointEditor;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "useWaypointEditor"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
