import { useReactFlow } from "../../../xyflow/react.js";
import { useCallback, useMemo } from "react";
import { DEFAULT_SOURCE_HANDLE_ID } from "../../../constants.js";
import { useCanvasNodeLayout } from "../../../hooks/useCanvasNodeLayout.js";
import { showPreviewGraph } from "../../../utils/createPreviewGraph.js";
import { isPreviewEdge } from "../../../utils/createPreviewNode.js";
import { useBaseCanvasMode } from "../../BaseCanvas/BaseCanvasModeProvider.js";
import { resolveContainerAddNodePreview } from "../../LoopNode/LoopNode.helpers.js";
import { useEdgeToolbarPositioning } from "./useEdgeToolbarPositioning.js";
function useEdgeToolbarState({ edgeId, pathElementRef, isHovered, source, target, sourceHandleId, targetHandleId, sourcePosition, targetPosition, ignoredNodeTypes }) {
    const reactFlow = useReactFlow();
    const { getManifestForNode } = useCanvasNodeLayout();
    const { mode } = useBaseCanvasMode();
    const isDesignMode = 'design' === mode;
    const previewEdge = isPreviewEdge({
        id: edgeId,
        source,
        target
    });
    const { positionData, handleMouseMoveOnPath } = useEdgeToolbarPositioning({
        pathElementRef,
        isEnabled: isHovered && isDesignMode && !previewEdge,
        targetPosition
    });
    const handleAddNodeOnEdge = useCallback((position)=>{
        const originalEdge = reactFlow.getEdges().find((edge)=>edge.id === edgeId);
        if (!originalEdge) return;
        const sourceEndpoint = {
            nodeId: source,
            handleId: sourceHandleId ?? DEFAULT_SOURCE_HANDLE_ID
        };
        const containerOverrides = resolveContainerAddNodePreview({
            source: sourceEndpoint,
            sourceHandleType: 'source',
            reactFlowInstance: reactFlow,
            getManifestForNode,
            replacedEdge: originalEdge
        });
        showPreviewGraph({
            source: sourceEndpoint,
            reactFlowInstance: reactFlow,
            position,
            positionMode: 'drop',
            data: {
                originalEdge
            },
            sourceHandleType: 'source',
            handlePosition: sourcePosition,
            ignoredNodeTypes: ignoredNodeTypes ?? [],
            target: {
                nodeId: target,
                handleId: targetHandleId
            },
            ...containerOverrides ?? {}
        });
    }, [
        sourcePosition,
        source,
        sourceHandleId,
        reactFlow,
        getManifestForNode,
        target,
        targetHandleId,
        edgeId,
        ignoredNodeTypes
    ]);
    const config = useMemo(()=>({
            actions: [
                {
                    id: 'add-node',
                    icon: 'plus',
                    label: 'Add node',
                    disabled: false,
                    onAction: (_edgeId, position)=>{
                        handleAddNodeOnEdge(position);
                    }
                }
            ]
        }), [
        handleAddNodeOnEdge
    ]);
    const showToolbar = isHovered && isDesignMode && null !== positionData && !previewEdge;
    return {
        showToolbar,
        toolbarPositioning: positionData,
        config,
        handleMouseMoveOnPath
    };
}
export { useEdgeToolbarState };
