import { Position, useReactFlow } from "../../../xyflow/react.js";
import { useCallback, useEffect, useRef, useState } from "react";
import { snapToGrid } from "../../../utils/index.js";
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 Position.Top:
            {
                const centerY = box.y + box.height / 2;
                const offsetX = box.x + box.width + offsetDistance;
                return {
                    x: offsetX,
                    y: centerY
                };
            }
        case Position.Bottom:
            {
                const centerY = box.y + box.height / 2;
                const offsetX = box.x - offsetDistance;
                return {
                    x: offsetX,
                    y: centerY
                };
            }
        case Position.Left:
        case 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 = useReactFlow();
    const [positionData, setPositionData] = useState(null);
    const rafIdRef = useRef(null);
    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 = 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: snapToGrid(pathPosition.x),
                y: 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
    };
}
export { useEdgeToolbarPositioning };
