import { useMemo } from "react";
import { useEdgePath } from "../../../../hooks/index.js";
import { ARROW_ANGLE_MAP, ARROW_OFFSETS, EDGE_CONSTANTS } from "../constants.js";
import { buildPathVertices, createRoundedPath, extractSegments, getPathArcMidpoint } from "../geometry.js";
const EMPTY_VERTICES = [];
const EMPTY_SEGMENTS = [];
function useEdgeGeometry(args) {
    const { routing, sourceNodeId, targetNodeId, sourceHandleId, targetHandleId, sourceX, sourceY, sourcePosition, targetX, targetY, targetPosition, waypoints, enableSegments = true, hideArrowHead = false } = args;
    const arrowOffset = ARROW_OFFSETS[targetPosition];
    const isWaypoint = 'waypoint' === routing;
    const isHandle = 'handle' === routing;
    const pathPoints = useMemo(()=>isWaypoint ? buildPathVertices(sourceX, sourceY, sourcePosition, targetX, targetY, targetPosition, waypoints) : EMPTY_VERTICES, [
        isWaypoint,
        sourceX,
        sourceY,
        sourcePosition,
        targetX,
        targetY,
        targetPosition,
        waypoints
    ]);
    const segments = useMemo(()=>enableSegments ? extractSegments(pathPoints) : EMPTY_SEGMENTS, [
        enableSegments,
        pathPoints
    ]);
    const waypointPath = useMemo(()=>createRoundedPath(pathPoints, EDGE_CONSTANTS.BORDER_RADIUS), [
        pathPoints
    ]);
    const handle = useEdgePath({
        sourceNodeId: isHandle ? sourceNodeId : '',
        targetNodeId: isHandle ? targetNodeId : '',
        sourceHandleId: isHandle ? sourceHandleId : null,
        targetHandleId: isHandle ? targetHandleId : null,
        sourceX: isHandle ? sourceX : 0,
        sourceY: isHandle ? sourceY : 0,
        sourcePosition,
        targetX: isHandle ? hideArrowHead ? targetX + arrowOffset.x : targetX : 0,
        targetY: isHandle ? hideArrowHead ? targetY + arrowOffset.y : targetY : 0,
        targetPosition
    });
    const labelPoint = useMemo(()=>isHandle ? {
            x: handle.labelX,
            y: handle.labelY
        } : getPathArcMidpoint(pathPoints), [
        isHandle,
        handle.labelX,
        handle.labelY,
        pathPoints
    ]);
    const arrow = {
        angle: ARROW_ANGLE_MAP[targetPosition],
        offset: arrowOffset
    };
    if (isHandle) return {
        edgePath: handle.edgePath,
        pathPoints: EMPTY_VERTICES,
        segments: EMPTY_SEGMENTS,
        labelPoint,
        arrow
    };
    return {
        edgePath: waypointPath,
        pathPoints,
        segments,
        labelPoint,
        arrow
    };
}
export { useEdgeGeometry };
