import { DEFAULT_SOURCE_HANDLE_ID, PREVIEW_NODE_ID } from "../constants.js";
import { PREVIEW_EDGE_STYLE, createPreviewNode, isPreviewEdge } from "./createPreviewNode.js";
import { getAbsolutePosition } from "./NodeUtils.js";
function inferPreviewContainerId(sourceNode, targetNode) {
    if (!targetNode) return sourceNode.parentId;
    if (sourceNode.parentId === targetNode.parentId) return sourceNode.parentId;
    if (targetNode.parentId === sourceNode.id) return sourceNode.id;
    if (sourceNode.parentId === targetNode.id) return targetNode.id;
}
function reparentPreviewNodeToContainer(previewNode, containerId, reactFlowInstance) {
    const containerNode = reactFlowInstance.getNode(containerId);
    if (!containerNode) return null;
    const containerAbsolutePosition = getAbsolutePosition(containerNode, reactFlowInstance.getNodes());
    return {
        ...previewNode,
        position: {
            x: previewNode.position.x - containerAbsolutePosition.x,
            y: previewNode.position.y - containerAbsolutePosition.y
        },
        parentId: containerId,
        extent: 'parent'
    };
}
function createPreviewNodeForGraph({ source, reactFlowInstance, position, data, sourceHandleType, previewNodeSize, handlePosition, ignoredNodeTypes, positionMode, sourceBoundaryOf }) {
    return createPreviewNode({
        sourceNodeId: source.nodeId,
        sourceHandleId: source.handleId ?? DEFAULT_SOURCE_HANDLE_ID,
        reactFlowInstance,
        position,
        data,
        sourceHandleType,
        previewNodeSize,
        handlePosition,
        ignoredNodeTypes,
        positionMode,
        sourceBoundaryOf
    });
}
function createTrailingPreviewEdge({ target, trailingEdgeId, trailingEdgeStyle = PREVIEW_EDGE_STYLE }) {
    if (!target) return null;
    return {
        id: trailingEdgeId ?? `${PREVIEW_NODE_ID}-${target.nodeId}`,
        source: PREVIEW_NODE_ID,
        sourceHandle: DEFAULT_SOURCE_HANDLE_ID,
        target: target.nodeId,
        targetHandle: target.handleId,
        type: 'default',
        style: trailingEdgeStyle
    };
}
function createPreviewGraph(options) {
    const { reactFlowInstance, target, containerId, source } = options;
    const preview = createPreviewNodeForGraph(options);
    if (!preview) return null;
    const sourceNode = reactFlowInstance.getNode(source.nodeId);
    const targetNode = target ? reactFlowInstance.getNode(target.nodeId) : void 0;
    const resolvedContainerId = containerId ?? inferPreviewContainerId(sourceNode, targetNode);
    const finalPreviewNode = resolvedContainerId ? reparentPreviewNodeToContainer(preview.node, resolvedContainerId, reactFlowInstance) : preview.node;
    if (!finalPreviewNode) return null;
    const trailingEdge = createTrailingPreviewEdge(options);
    const edges = trailingEdge ? [
        preview.edge,
        trailingEdge
    ] : [
        preview.edge
    ];
    return {
        node: finalPreviewNode,
        edges
    };
}
function showPreviewGraph(options) {
    const preview = createPreviewGraph(options);
    if (preview) applyPreviewGraphToReactFlow(preview, options.reactFlowInstance);
    return preview;
}
function applyPreviewGraphToReactFlow(preview, reactFlowInstance) {
    const originalEdge = preview.node.data?.originalEdge;
    setTimeout(()=>{
        reactFlowInstance.setNodes((nodes)=>[
                ...nodes.filter((node)=>node.id !== PREVIEW_NODE_ID).map((node)=>({
                        ...node,
                        selected: false
                    })),
                preview.node
            ]);
        reactFlowInstance.setEdges((edges)=>[
                ...edges.filter((edge)=>!isPreviewEdge(edge) && edge.id !== originalEdge?.id),
                ...preview.edges
            ]);
    }, 0);
}
export { applyPreviewGraphToReactFlow, createPreviewGraph, reparentPreviewNodeToContainer, showPreviewGraph };
