"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__, {
    useNodeDragRebalance: ()=>useNodeDragRebalance
});
const react_cjs_namespaceObject = require("../../../../xyflow/react.cjs");
const external_react_namespaceObject = require("react");
const external_waypoints_cjs_namespaceObject = require("../waypoints.cjs");
const external_useEdgeWaypointWriter_cjs_namespaceObject = require("./useEdgeWaypointWriter.cjs");
function useNodeDragRebalance(args) {
    const { edgeId, source, target, sourceX, sourceY, sourcePosition, targetX, targetY, targetPosition, waypoints, enabled, onChange } = args;
    const dragging = (0, react_cjs_namespaceObject.useStore)((state)=>enabled && !!(state.nodeLookup.get(source)?.dragging || state.nodeLookup.get(target)?.dragging));
    const write = (0, external_useEdgeWaypointWriter_cjs_namespaceObject.useEdgeWaypointWriter)(edgeId, onChange);
    const baselineRef = (0, external_react_namespaceObject.useRef)({
        s: {
            x: sourceX,
            y: sourceY
        },
        t: {
            x: targetX,
            y: targetY
        },
        waypoints
    });
    const lastRebalancedRef = (0, external_react_namespaceObject.useRef)(null);
    const prevDraggingRef = (0, external_react_namespaceObject.useRef)(false);
    const active = enabled && dragging && waypoints.length > 0;
    if (!dragging) baselineRef.current = {
        s: {
            x: sourceX,
            y: sourceY
        },
        t: {
            x: targetX,
            y: targetY
        },
        waypoints
    };
    let display = waypoints;
    if (active) {
        const { s, t, waypoints: w0 } = baselineRef.current;
        display = (0, external_waypoints_cjs_namespaceObject.rebalanceWaypoints)(w0, {
            position: sourcePosition,
            from: s,
            to: {
                x: sourceX,
                y: sourceY
            }
        }, {
            position: targetPosition,
            from: t,
            to: {
                x: targetX,
                y: targetY
            }
        });
        lastRebalancedRef.current = display;
    }
    (0, external_react_namespaceObject.useLayoutEffect)(()=>{
        const wasDragging = prevDraggingRef.current;
        prevDraggingRef.current = dragging;
        if (!wasDragging || dragging) return;
        const final = lastRebalancedRef.current;
        lastRebalancedRef.current = null;
        if (enabled && final && !(0, external_waypoints_cjs_namespaceObject.waypointsEqual)(waypoints, final)) write(final);
    });
    return display;
}
exports.useNodeDragRebalance = __webpack_exports__.useNodeDragRebalance;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "useNodeDragRebalance"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
