import { Position } from "../../xyflow/react.js";
import { showPreviewGraph } from "../../utils/createPreviewGraph.js";
import { resolveHandles } from "../../utils/manifest-resolver.js";
import { resolveContainerAddNodePreview } from "../LoopNode/LoopNode.helpers.js";
function buildSourceBoundaryResolver(sourceNodeId, reactFlowInstance, getManifestForNode) {
    const sourceNode = reactFlowInstance.getNode(sourceNodeId);
    if (!sourceNode) return;
    const manifest = getManifestForNode(sourceNode);
    if (!manifest?.handleConfiguration) return;
    const resolvedGroups = resolveHandles(manifest.handleConfiguration, {
        ...sourceNode.data,
        nodeId: sourceNode.id
    });
    const handleToBoundary = new Map();
    for (const group of resolvedGroups){
        const boundary = group.boundary ?? 'outer';
        for (const handle of group.handles)handleToBoundary.set(handle.id, boundary);
    }
    return (handleId)=>handleToBoundary.get(handleId);
}
function createAddNodePreview(sourceNodeId, sourceHandleId, reactFlowInstance, handlePosition = Position.Right, sourceHandleType = 'source', ignoredNodeTypes = [], options = {}) {
    const source = {
        nodeId: sourceNodeId,
        handleId: sourceHandleId
    };
    const overrides = options.getManifestForNode ? resolveContainerAddNodePreview({
        source,
        sourceHandleType,
        reactFlowInstance,
        getManifestForNode: options.getManifestForNode
    }) : null;
    const sourceBoundaryOf = options.getManifestForNode ? buildSourceBoundaryResolver(sourceNodeId, reactFlowInstance, options.getManifestForNode) : void 0;
    showPreviewGraph({
        source,
        reactFlowInstance,
        sourceHandleType,
        handlePosition,
        ignoredNodeTypes,
        sourceBoundaryOf,
        ...overrides ?? {}
    });
}
export { createAddNodePreview };
