"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__, {
    useEdgeToolbarPositioning: ()=>useEdgeToolbarPositioning
});
const react_cjs_namespaceObject = require("../../../xyflow/react.cjs");
const external_react_namespaceObject = require("react");
const index_cjs_namespaceObject = require("../../../utils/index.cjs");
const MIN_MOUSE_FOLLOW_DISTANCE = 100;
const DEFAULT_OFFSET_DISTANCE = 24;
const LINE_TANGENT_SAMPLE_DISTANCE = 10;
function getNearestLengthOnPath(pathElement, mousePosition) {
    const totalLength = pathElement.getTotalLength();
    if (totalLength < MIN_MOUSE_FOLLOW_DISTANCE) return totalLength / 2;
    let left = 0;
    let right = totalLength;
    const threshold = 1;
    while(right - left > threshold){
        const mid1 = left + (right - left) / 3;
        const mid2 = right - (right - left) / 3;
        const point1 = pathElement.getPointAtLength(mid1);
        const point2 = pathElement.getPointAtLength(mid2);
        const dist1 = Math.hypot(point1.x - mousePosition.x, point1.y - mousePosition.y);
        const dist2 = Math.hypot(point2.x - mousePosition.x, point2.y - mousePosition.y);
        if (dist1 > dist2) left = mid1;
        else right = mid2;
    }
    return (left + right) / 2;
}
function getOffsetPointFromPathBoundingBox(pathElement, targetPosition, offsetDistance = DEFAULT_OFFSET_DISTANCE) {
    const box = pathElement.getBBox();
    switch(targetPosition){
        case react_cjs_namespaceObject.Position.Top:
            {
                const centerY = box.y + box.height / 2;
                const offsetX = box.x + box.width + offsetDistance;
                return {
                    x: offsetX,
                    y: centerY
                };
            }
        case react_cjs_namespaceObject.Position.Bottom:
            {
                const centerY = box.y + box.height / 2;
                const offsetX = box.x - offsetDistance;
                return {
                    x: offsetX,
                    y: centerY
                };
            }
        case react_cjs_namespaceObject.Position.Left:
        case react_cjs_namespaceObject.Position.Right:
            {
                const centerX = box.x + box.width / 2;
                const offsetY = box.y - offsetDistance;
                return {
                    x: centerX,
                    y: offsetY
                };
            }
    }
}
function getPerpendicularOffsetPointAtPathLength(pathElement, length, offsetDistance = 24) {
    const totalLength = pathElement.getTotalLength();
    const beforeLength = Math.max(0, length - LINE_TANGENT_SAMPLE_DISTANCE);
    const afterLength = Math.min(totalLength, length + LINE_TANGENT_SAMPLE_DISTANCE);
    const pathPoint = pathElement.getPointAtLength(length);
    const beforePoint = pathElement.getPointAtLength(beforeLength);
    const afterPoint = pathElement.getPointAtLength(afterLength);
    const tangentX = afterPoint.x - beforePoint.x;
    const tangentY = afterPoint.y - beforePoint.y;
    const tangentLength = Math.hypot(tangentX, tangentY);
    const normTangentX = tangentX / tangentLength;
    const normTangentY = tangentY / tangentLength;
    const normalX = normTangentY;
    const normalY = -normTangentX;
    const offsetX = pathPoint.x + normalX * offsetDistance;
    const offsetY = pathPoint.y + normalY * offsetDistance;
    return {
        x: offsetX,
        y: offsetY
    };
}
function useEdgeToolbarPositioning({ pathElementRef, isEnabled, targetPosition }) {
    const reactFlow = (0, react_cjs_namespaceObject.useReactFlow)();
    const [positionData, setPositionData] = (0, external_react_namespaceObject.useState)(null);
    const rafIdRef = (0, external_react_namespaceObject.useRef)(null);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (!isEnabled) {
            if (null !== rafIdRef.current) {
                cancelAnimationFrame(rafIdRef.current);
                rafIdRef.current = null;
            }
            setPositionData(null);
        }
        return ()=>{
            if (null !== rafIdRef.current) {
                cancelAnimationFrame(rafIdRef.current);
                rafIdRef.current = null;
            }
        };
    }, [
        isEnabled
    ]);
    const handleMouseMoveOnPath = (0, external_react_namespaceObject.useCallback)((event)=>{
        if (!isEnabled || !pathElementRef.current) return;
        if (null !== rafIdRef.current) cancelAnimationFrame(rafIdRef.current);
        rafIdRef.current = requestAnimationFrame(()=>{
            if (!pathElementRef.current) return;
            const screenPosition = {
                x: event.clientX,
                y: event.clientY
            };
            const mouseFlowPosition = reactFlow.screenToFlowPosition(screenPosition, {
                snapToGrid: false
            });
            const nearestLength = getNearestLengthOnPath(pathElementRef.current, mouseFlowPosition);
            const pathPosition = pathElementRef.current.getPointAtLength(nearestLength);
            const snappedPathPosition = {
                x: (0, index_cjs_namespaceObject.snapToGrid)(pathPosition.x),
                y: (0, index_cjs_namespaceObject.snapToGrid)(pathPosition.y)
            };
            let offsetPosition;
            offsetPosition = pathElementRef.current.getTotalLength() < MIN_MOUSE_FOLLOW_DISTANCE ? getOffsetPointFromPathBoundingBox(pathElementRef.current, targetPosition) : getPerpendicularOffsetPointAtPathLength(pathElementRef.current, nearestLength);
            setPositionData({
                offsetPosition,
                pathPosition: snappedPathPosition
            });
            rafIdRef.current = null;
        });
    }, [
        isEnabled,
        reactFlow,
        pathElementRef,
        targetPosition
    ]);
    return {
        positionData,
        handleMouseMoveOnPath: isEnabled ? handleMouseMoveOnPath : void 0
    };
}
exports.useEdgeToolbarPositioning = __webpack_exports__.useEdgeToolbarPositioning;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "useEdgeToolbarPositioning"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
