"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    createPreviewGraph: ()=>createPreviewGraph,
    reparentPreviewNodeToContainer: ()=>reparentPreviewNodeToContainer,
    showPreviewGraph: ()=>showPreviewGraph,
    applyPreviewGraphToReactFlow: ()=>applyPreviewGraphToReactFlow
});
const external_constants_cjs_namespaceObject = require("../constants.cjs");
const external_createPreviewNode_cjs_namespaceObject = require("./createPreviewNode.cjs");
const external_NodeUtils_cjs_namespaceObject = require("./NodeUtils.cjs");
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 = (0, external_NodeUtils_cjs_namespaceObject.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 (0, external_createPreviewNode_cjs_namespaceObject.createPreviewNode)({
        sourceNodeId: source.nodeId,
        sourceHandleId: source.handleId ?? external_constants_cjs_namespaceObject.DEFAULT_SOURCE_HANDLE_ID,
        reactFlowInstance,
        position,
        data,
        sourceHandleType,
        previewNodeSize,
        handlePosition,
        ignoredNodeTypes,
        positionMode,
        sourceBoundaryOf
    });
}
function createTrailingPreviewEdge({ target, trailingEdgeId, trailingEdgeStyle = external_createPreviewNode_cjs_namespaceObject.PREVIEW_EDGE_STYLE }) {
    if (!target) return null;
    return {
        id: trailingEdgeId ?? `${external_constants_cjs_namespaceObject.PREVIEW_NODE_ID}-${target.nodeId}`,
        source: external_constants_cjs_namespaceObject.PREVIEW_NODE_ID,
        sourceHandle: external_constants_cjs_namespaceObject.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 !== external_constants_cjs_namespaceObject.PREVIEW_NODE_ID).map((node)=>({
                        ...node,
                        selected: false
                    })),
                preview.node
            ]);
        reactFlowInstance.setEdges((edges)=>[
                ...edges.filter((edge)=>!(0, external_createPreviewNode_cjs_namespaceObject.isPreviewEdge)(edge) && edge.id !== originalEdge?.id),
                ...preview.edges
            ]);
    }, 0);
}
exports.applyPreviewGraphToReactFlow = __webpack_exports__.applyPreviewGraphToReactFlow;
exports.createPreviewGraph = __webpack_exports__.createPreviewGraph;
exports.reparentPreviewNodeToContainer = __webpack_exports__.reparentPreviewNodeToContainer;
exports.showPreviewGraph = __webpack_exports__.showPreviewGraph;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "applyPreviewGraphToReactFlow",
    "createPreviewGraph",
    "reparentPreviewNodeToContainer",
    "showPreviewGraph"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
