"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__, {
    HierarchicalCanvas: ()=>HierarchicalCanvas
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const react_cjs_namespaceObject = require("../../xyflow/react.cjs");
const apollo_wind_namespaceObject = require("@uipath/apollo-wind");
const external_react_namespaceObject = require("react");
const external_constants_cjs_namespaceObject = require("../../constants.cjs");
const index_cjs_namespaceObject = require("../../controls/index.cjs");
const external_core_index_cjs_namespaceObject = require("../../core/index.cjs");
const useAddNodeOnConnectEnd_cjs_namespaceObject = require("../../hooks/useAddNodeOnConnectEnd.cjs");
const useCanvasEvents_cjs_namespaceObject = require("../../hooks/useCanvasEvents.cjs");
const useCanvasNodeLayout_cjs_namespaceObject = require("../../hooks/useCanvasNodeLayout.cjs");
const animatedViewportManager_cjs_namespaceObject = require("../../stores/animatedViewportManager.cjs");
const canvasStore_cjs_namespaceObject = require("../../stores/canvasStore.cjs");
const viewportManager_cjs_namespaceObject = require("../../stores/viewportManager.cjs");
const external_types_cjs_namespaceObject = require("../../types.cjs");
const external_utils_index_cjs_namespaceObject = require("../../utils/index.cjs");
const createPreviewNode_cjs_namespaceObject = require("../../utils/createPreviewNode.cjs");
const icon_registry_cjs_namespaceObject = require("../../utils/icon-registry.cjs");
const transitions_cjs_namespaceObject = require("../../utils/transitions.cjs");
const AddNodeManager_cjs_namespaceObject = require("../AddNodePanel/AddNodeManager.cjs");
const AddNodePreview_cjs_namespaceObject = require("../AddNodePanel/AddNodePreview.cjs");
const createAddNodePreview_cjs_namespaceObject = require("../AddNodePanel/createAddNodePreview.cjs");
const external_BaseCanvas_index_cjs_namespaceObject = require("../BaseCanvas/index.cjs");
const external_BaseNode_index_cjs_namespaceObject = require("../BaseNode/index.cjs");
const external_BlankCanvasNode_index_cjs_namespaceObject = require("../BlankCanvasNode/index.cjs");
const external_CanvasPositionControls_cjs_namespaceObject = require("../CanvasPositionControls.cjs");
const external_LoopNode_index_cjs_namespaceObject = require("../LoopNode/index.cjs");
const external_MiniCanvasNavigator_index_cjs_namespaceObject = require("../MiniCanvasNavigator/index.cjs");
const DEFAULT_NODE_TYPES = {
    default: external_BaseNode_index_cjs_namespaceObject.BaseNode,
    'blank-canvas-node': external_BlankCanvasNode_index_cjs_namespaceObject.BlankCanvasNode,
    preview: AddNodePreview_cjs_namespaceObject.AddNodePreview
};
function shouldPersistNodeChange(change) {
    if ('position' === change.type) return !!change.dragging;
    if ('dimensions' === change.type) return !!change.setAttributes;
    return true;
}
function isDefaultViewport(viewport) {
    return 0 === viewport.x && 0 === viewport.y && 1 === viewport.zoom;
}
const HierarchicalCanvas = ({ mode = 'design', initialCanvases, initialPath, onCanvasesChange, onPathChange })=>{
    const canvasRef = (0, external_react_namespaceObject.useRef)(null);
    const containerRef = (0, external_react_namespaceObject.useRef)(null);
    const [reactFlowInstance, setReactFlowInstance] = (0, external_react_namespaceObject.useState)(null);
    const currentViewportRef = (0, external_react_namespaceObject.useRef)({
        x: 0,
        y: 0,
        zoom: 1
    });
    const lastCanvasIdRef = (0, external_react_namespaceObject.useRef)(null);
    const shouldAnimate = 'design' === mode && !(0, transitions_cjs_namespaceObject.prefersReducedMotion)();
    const nodeManifests = (0, external_core_index_cjs_namespaceObject.useNodeManifests)();
    const nodeTypes = (0, external_react_namespaceObject.useMemo)(()=>nodeManifests.reduce((acc, manifest)=>{
            acc[manifest.nodeType] = (0, external_utils_index_cjs_namespaceObject.isContainerNodeManifest)(manifest) ? external_LoopNode_index_cjs_namespaceObject.LoopCanvasNode : external_BaseNode_index_cjs_namespaceObject.BaseNode;
            return acc;
        }, {
            ...DEFAULT_NODE_TYPES
        }), [
        nodeManifests
    ]);
    const { getManifestForNode } = (0, useCanvasNodeLayout_cjs_namespaceObject.useCanvasNodeLayout)();
    const currentCanvas = (0, canvasStore_cjs_namespaceObject.useCanvasStore)(canvasStore_cjs_namespaceObject.selectCurrentCanvas);
    const previousCanvas = (0, canvasStore_cjs_namespaceObject.useCanvasStore)(canvasStore_cjs_namespaceObject.selectPreviousCanvas);
    const breadcrumbs = (0, canvasStore_cjs_namespaceObject.useCanvasStore)(canvasStore_cjs_namespaceObject.selectBreadcrumbs);
    const transitionState = (0, canvasStore_cjs_namespaceObject.useCanvasStore)(canvasStore_cjs_namespaceObject.selectTransitionState);
    const canvasStack = (0, canvasStore_cjs_namespaceObject.useCanvasStore)(canvasStore_cjs_namespaceObject.selectCanvasStack);
    const currentPath = (0, canvasStore_cjs_namespaceObject.useCanvasStore)(canvasStore_cjs_namespaceObject.selectCurrentPath);
    const initializeCanvas = (0, canvasStore_cjs_namespaceObject.useCanvasStore)(canvasStore_cjs_namespaceObject.selectInitializeCanvas);
    const initializeWithData = (0, canvasStore_cjs_namespaceObject.useCanvasStore)(canvasStore_cjs_namespaceObject.selectInitializeWithData);
    const navigateToDepth = (0, canvasStore_cjs_namespaceObject.useCanvasStore)(canvasStore_cjs_namespaceObject.selectNavigateToDepth);
    const navigateToSiblingCanvas = (0, canvasStore_cjs_namespaceObject.useCanvasStore)(canvasStore_cjs_namespaceObject.selectNavigateToSiblingCanvas);
    const updateNodes = (0, canvasStore_cjs_namespaceObject.useCanvasStore)(canvasStore_cjs_namespaceObject.selectUpdateNodes);
    const updateEdges = (0, canvasStore_cjs_namespaceObject.useCanvasStore)(canvasStore_cjs_namespaceObject.selectUpdateEdges);
    const updateSelection = (0, canvasStore_cjs_namespaceObject.useCanvasStore)(canvasStore_cjs_namespaceObject.selectUpdateSelection);
    const updateViewport = (0, canvasStore_cjs_namespaceObject.useCanvasStore)(canvasStore_cjs_namespaceObject.selectUpdateViewport);
    const drillIntoNode = (0, canvasStore_cjs_namespaceObject.useCanvasStore)(canvasStore_cjs_namespaceObject.selectDrillIntoNode);
    const currentPathRef = (0, external_react_namespaceObject.useRef)(currentPath);
    (0, external_react_namespaceObject.useEffect)(()=>{
        currentPathRef.current = currentPath;
    }, [
        currentPath
    ]);
    const addNodeOnConnectEnd = (0, useAddNodeOnConnectEnd_cjs_namespaceObject.useAddNodeOnConnectEnd)();
    const handleAddNodePreviewAction = (0, external_react_namespaceObject.useCallback)((event)=>{
        if (!reactFlowInstance) return;
        (0, createAddNodePreview_cjs_namespaceObject.createAddNodePreview)(event.nodeId, event.handleId, reactFlowInstance, event.position, 'input' === event.handleType ? 'target' : 'source', [], {
            getManifestForNode
        });
    }, [
        reactFlowInstance,
        getManifestForNode
    ]);
    (0, useCanvasEvents_cjs_namespaceObject.useCanvasEvent)('handle:action', handleAddNodePreviewAction);
    const hasInitialized = (0, external_react_namespaceObject.useRef)(false);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (hasInitialized.current) return;
        hasInitialized.current = true;
        if (initialCanvases && Object.keys(initialCanvases).length > 0) initializeWithData(initialCanvases, initialPath);
        else initializeCanvas();
    }, []);
    const prevCanvasStackRef = (0, external_react_namespaceObject.useRef)(canvasStack);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (onCanvasesChange && canvasStack !== prevCanvasStackRef.current) {
            prevCanvasStackRef.current = canvasStack;
            onCanvasesChange(canvasStack);
        }
    }, [
        canvasStack,
        onCanvasesChange
    ]);
    const prevPathRef = (0, external_react_namespaceObject.useRef)(currentPath);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (onPathChange && currentPath !== prevPathRef.current) {
            prevPathRef.current = currentPath;
            onPathChange(currentPath);
        }
    }, [
        currentPath,
        onPathChange
    ]);
    const currentCanvasViewportRef = (0, external_react_namespaceObject.useRef)(currentCanvas?.viewport);
    (0, external_react_namespaceObject.useEffect)(()=>{
        currentCanvasViewportRef.current = currentCanvas?.viewport;
    }, [
        currentCanvas?.viewport
    ]);
    const currentCanvasRef = (0, external_react_namespaceObject.useRef)(currentCanvas);
    (0, external_react_namespaceObject.useEffect)(()=>{
        currentCanvasRef.current = currentCanvas;
    }, [
        currentCanvas
    ]);
    const handleInit = (0, external_react_namespaceObject.useCallback)((instance)=>{
        setReactFlowInstance(instance);
        viewportManager_cjs_namespaceObject.viewportManager.setReactFlowInstance(instance);
        animatedViewportManager_cjs_namespaceObject.animatedViewportManager.setReactFlowInstance(instance);
        const viewport = currentCanvasViewportRef.current;
        if (viewport && (0 !== viewport.x || 0 !== viewport.y || 1 !== viewport.zoom)) instance.setViewport(viewport);
    }, []);
    (0, external_react_namespaceObject.useEffect)(()=>{
        const currentCanvasId = currentPath[currentPath.length - 1];
        if (currentCanvasId && lastCanvasIdRef.current && currentCanvasId !== lastCanvasIdRef.current) {
            if (reactFlowInstance && currentCanvas?.viewport) {
                const viewport = currentCanvas.viewport;
                if (isDefaultViewport(viewport)) {
                    currentViewportRef.current = viewport;
                    viewportManager_cjs_namespaceObject.viewportManager.setCurrentViewport(viewport);
                } else setTimeout(()=>{
                    reactFlowInstance.setViewport(viewport);
                    currentViewportRef.current = viewport;
                    viewportManager_cjs_namespaceObject.viewportManager.setCurrentViewport(viewport);
                }, 0);
            }
        }
        lastCanvasIdRef.current = currentCanvasId || null;
    }, [
        currentPath,
        reactFlowInstance,
        currentCanvas
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>()=>{
            viewportManager_cjs_namespaceObject.viewportManager.setReactFlowInstance(null);
        }, []);
    const handleNodesChange = (0, external_react_namespaceObject.useCallback)((changes)=>{
        const canvas = currentCanvasRef.current;
        if (!canvas) return;
        if (!changes.some(shouldPersistNodeChange)) return;
        const updatedNodes = (0, react_cjs_namespaceObject.applyNodeChanges)(changes, canvas.nodes ?? []);
        updateNodes(updatedNodes);
    }, [
        updateNodes
    ]);
    const handleEdgesChange = (0, external_react_namespaceObject.useCallback)((changes)=>{
        const canvas = currentCanvasRef.current;
        if (!canvas) return;
        const updatedEdges = (0, react_cjs_namespaceObject.applyEdgeChanges)(changes, canvas.edges ?? []);
        updateEdges(updatedEdges);
    }, [
        updateEdges
    ]);
    const handleSelectionChange = (0, external_react_namespaceObject.useCallback)((params)=>{
        const canvas = currentCanvasRef.current;
        if (!canvas) return;
        const nodeIds = params.nodes.map((n)=>n.id);
        const edgeIds = params.edges.map((e)=>e.id);
        const nodeIdsChanged = nodeIds.length !== canvas.selection.nodeIds.length || !nodeIds.every((id)=>canvas.selection.nodeIds.includes(id));
        const edgeIdsChanged = edgeIds.length !== canvas.selection.edgeIds.length || !edgeIds.every((id)=>canvas.selection.edgeIds.includes(id));
        if (nodeIdsChanged || edgeIdsChanged) updateSelection(nodeIds, edgeIds);
    }, [
        updateSelection
    ]);
    const handleMove = (0, external_react_namespaceObject.useCallback)((_event, viewport)=>{
        currentViewportRef.current = viewport;
        viewportManager_cjs_namespaceObject.viewportManager.setCurrentViewport(viewport);
    }, []);
    const handleConnect = (0, external_react_namespaceObject.useCallback)((connection)=>{
        const canvas = currentCanvasRef.current;
        if (!connection.source || !connection.target || !canvas) return;
        if ((0, createPreviewNode_cjs_namespaceObject.isPreviewEdge)(connection)) return;
        const newEdge = {
            id: `${connection.source}-${connection.target}-${Date.now()}`,
            source: connection.source,
            target: connection.target,
            sourceHandle: connection.sourceHandle || void 0,
            targetHandle: connection.targetHandle || void 0
        };
        const hasPreview = canvas.nodes.some((n)=>n.id === external_constants_cjs_namespaceObject.PREVIEW_NODE_ID);
        const baseEdges = hasPreview ? canvas.edges.filter((edge)=>!(0, createPreviewNode_cjs_namespaceObject.isPreviewEdge)(edge)) : canvas.edges;
        updateEdges([
            ...baseEdges,
            newEdge
        ]);
        if (hasPreview) updateNodes(canvas.nodes.filter((n)=>n.id !== external_constants_cjs_namespaceObject.PREVIEW_NODE_ID));
    }, [
        updateNodes,
        updateEdges
    ]);
    const handleNavigateToDepth = (0, external_react_namespaceObject.useCallback)(async (depth)=>{
        if (transitionState.isTransitioning) return;
        const path = currentPathRef.current;
        const currentCanvasId = path[path.length - 1];
        if (currentCanvasId && reactFlowInstance) {
            const viewport = reactFlowInstance.getViewport();
            updateViewport(viewport, currentCanvasId);
        }
        await navigateToDepth(depth, shouldAnimate);
    }, [
        navigateToDepth,
        updateViewport,
        reactFlowInstance,
        transitionState.isTransitioning,
        shouldAnimate
    ]);
    const handleToolbarAction = (0, external_react_namespaceObject.useCallback)(async (event)=>{
        if ('drill-in' === event.actionId) await drillIntoNode(event.nodeId, true);
    }, [
        drillIntoNode
    ]);
    const handlePreviousCanvasNodeClick = (0, external_react_namespaceObject.useCallback)((_event, node)=>{
        if (node.data?.childCanvasId) navigateToSiblingCanvas(node.data.childCanvasId, shouldAnimate);
    }, [
        navigateToSiblingCanvas,
        shouldAnimate
    ]);
    const shouldFitView = (0, external_react_namespaceObject.useMemo)(()=>{
        const viewport = currentCanvas?.viewport;
        return viewport ? isDefaultViewport(viewport) : false;
    }, [
        currentCanvas?.viewport
    ]);
    if (!currentCanvas) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        style: {
            height: '100vh',
            width: '100vw',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center'
        },
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(apollo_wind_namespaceObject.Spinner, {})
    });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
        ref: containerRef,
        style: {
            width: '100%',
            height: '100%',
            position: 'relative',
            background: 'var(--canvas-background-secondary)'
        },
        children: [
            breadcrumbs.length > 1 && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                style: {
                    position: 'absolute',
                    top: 16,
                    left: 16,
                    zIndex: 10,
                    background: 'var(--canvas-background)',
                    borderRadius: '8px',
                    padding: '8px 12px',
                    boxShadow: '0 2px 8px rgba(0, 0, 0, 0.1)'
                },
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.Breadcrumb, {
                    items: breadcrumbs.map((crumb, index)=>({
                            label: crumb.name,
                            onClick: index < breadcrumbs.length - 1 ? ()=>handleNavigateToDepth(index) : void 0,
                            startAdornment: 0 === index ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
                                icon: "house",
                                size: 16
                            }) : void 0
                        })),
                    delimiter: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
                        icon: "chevron-right",
                        size: 16
                    })
                })
            }),
            previousCanvas && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                style: {
                    position: 'absolute',
                    bottom: 16,
                    left: 16,
                    zIndex: 10
                },
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_MiniCanvasNavigator_index_cjs_namespaceObject.MiniCanvasNavigator, {
                    previousCanvas: previousCanvas,
                    currentCanvasId: currentCanvas?.id,
                    nodeTypes: nodeTypes,
                    onNodeClick: handlePreviousCanvasNodeClick
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_BaseCanvas_index_cjs_namespaceObject.BaseCanvas, {
                ref: canvasRef,
                nodes: currentCanvas.nodes,
                edges: currentCanvas.edges,
                nodeTypes: nodeTypes,
                onNodesChange: handleNodesChange,
                onEdgesChange: handleEdgesChange,
                onSelectionChange: handleSelectionChange,
                onMove: handleMove,
                onConnect: handleConnect,
                onConnectEnd: addNodeOnConnectEnd,
                onInit: handleInit,
                onToolbarAction: handleToolbarAction,
                mode: mode,
                defaultViewport: currentCanvas.viewport,
                fitView: shouldFitView,
                fitViewOptions: {
                    padding: 0.2,
                    minZoom: 1,
                    maxZoom: 1
                },
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(react_cjs_namespaceObject.Panel, {
                        position: "bottom-right",
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_CanvasPositionControls_cjs_namespaceObject.CanvasPositionControls, {
                            translations: external_types_cjs_namespaceObject.DefaultCanvasTranslations
                        })
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(AddNodeManager_cjs_namespaceObject.AddNodeManager, {})
                ]
            }, currentCanvas.id)
        ]
    });
};
exports.HierarchicalCanvas = __webpack_exports__.HierarchicalCanvas;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "HierarchicalCanvas"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
