"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __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__, {
    CodedAgentFlow: ()=>CodedAgentFlow
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const styled_namespaceObject = require("@emotion/styled");
var styled_default = /*#__PURE__*/ __webpack_require__.n(styled_namespaceObject);
const apollo_core_namespaceObject = require("@uipath/apollo-core");
const index_cjs_namespaceObject = require("../../icons/index.cjs");
const external_layouts_index_cjs_namespaceObject = require("../../layouts/index.cjs");
const react_cjs_namespaceObject = require("../../xyflow/react.cjs");
const apollo_wind_namespaceObject = require("@uipath/apollo-wind");
const external_react_namespaceObject = require("react");
const NodeRegistryProvider_cjs_namespaceObject = require("../../core/NodeRegistryProvider.cjs");
const external_types_cjs_namespaceObject = require("../../types.cjs");
const d3_layout_cjs_namespaceObject = require("../../utils/coded-agents/d3-layout.cjs");
const mermaid_parser_cjs_namespaceObject = require("../../utils/coded-agents/mermaid-parser.cjs");
const icon_registry_cjs_namespaceObject = require("../../utils/icon-registry.cjs");
const external_BaseCanvas_index_cjs_namespaceObject = require("../BaseCanvas/index.cjs");
const BaseNode_cjs_namespaceObject = require("../BaseNode/BaseNode.cjs");
const BaseNodeConfigContext_cjs_namespaceObject = require("../BaseNode/BaseNodeConfigContext.cjs");
const external_CanvasPositionControls_cjs_namespaceObject = require("../CanvasPositionControls.cjs");
const external_coded_agent_manifest_cjs_namespaceObject = require("./coded-agent.manifest.cjs");
const LAYOUT_SPACING = [
    110,
    80
];
const CodedAgentEdge = /*#__PURE__*/ (0, external_react_namespaceObject.memo)(({ id, sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, style, markerEnd, animated })=>{
    const [edgePath] = (0, react_cjs_namespaceObject.getSimpleBezierPath)({
        sourceX,
        sourceY,
        sourcePosition,
        targetX,
        targetY,
        targetPosition
    });
    const strokeColor = animated ? 'var(--canvas-primary)' : 'var(--canvas-foreground-de-emp)';
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(react_cjs_namespaceObject.BaseEdge, {
        id: id,
        path: edgePath,
        markerEnd: markerEnd,
        style: {
            ...style,
            stroke: strokeColor,
            strokeWidth: 1,
            transition: 'stroke 0.2s ease-in-out'
        }
    });
});
const CenteredDiv = styled_default().div`
  background-color: var(--canvas-background-secondary);
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
`;
const TextContainer = styled_default().div`
  position: absolute;
  bottom: 0;
  transform: translateY(100%);
  text-align: center;
  white-space: nowrap;
`;
const leftTargetHandle = [
    {
        id: 'left',
        type: 'target',
        handleType: 'artifact',
        showButton: false
    }
];
const rightSourceHandle = [
    {
        id: 'right',
        type: 'source',
        handleType: 'artifact',
        showButton: false
    }
];
const rightOutputHandle = [
    {
        id: 'right',
        type: 'source',
        handleType: 'output',
        showButton: false
    }
];
const leftInputHandle = [
    {
        id: 'left',
        type: 'target',
        handleType: 'input',
        showButton: false
    }
];
const createCodedAgentNodeWrapper = (translations = external_types_cjs_namespaceObject.DefaultCodedAgentNodeTranslations)=>/*#__PURE__*/ (0, external_react_namespaceObject.memo)(({ data, selected, id, ...nodeProps })=>{
        const nodeData = data;
        const executionStatus = (0, external_react_namespaceObject.useMemo)(()=>{
            if (nodeData.hasError) return 'Failed';
            if (nodeData.hasSuccess) return 'Completed';
            if (nodeData.hasRunning) return 'InProgress';
        }, [
            nodeData.hasError,
            nodeData.hasSuccess,
            nodeData.hasRunning
        ]);
        const statusAdornment = (0, external_react_namespaceObject.useMemo)(()=>{
            if (nodeData.hasError) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
                icon: "circle-alert",
                size: 16,
                color: "var(--canvas-error-icon)"
            });
            if (nodeData.hasSuccess && !nodeData.hasError) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
                icon: "circle-check",
                size: 16,
                color: "var(--canvas-success-icon)"
            });
            if (nodeData.hasRunning && !nodeData.hasError && !nodeData.hasSuccess) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(apollo_wind_namespaceObject.Spinner, {
                size: "sm"
            });
        }, [
            nodeData.hasError,
            nodeData.hasSuccess,
            nodeData.hasRunning
        ]);
        const handleConfigurations = (0, external_react_namespaceObject.useMemo)(()=>[
                {
                    position: react_cjs_namespaceObject.Position.Left,
                    handles: leftTargetHandle,
                    visible: true
                },
                {
                    position: react_cjs_namespaceObject.Position.Right,
                    handles: rightSourceHandle,
                    visible: true
                }
            ], []);
        const baseNodeConfig = (0, external_react_namespaceObject.useMemo)(()=>({
                executionStatusOverride: executionStatus,
                handleConfigurations,
                adornments: {
                    topRight: statusAdornment
                },
                iconComponent: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.CodedAgentIcon, {
                    w: 40,
                    h: 40
                })
            }), [
            executionStatus,
            handleConfigurations,
            statusAdornment
        ]);
        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BaseNodeConfigContext_cjs_namespaceObject.BaseNodeOverrideConfigProvider, {
            value: baseNodeConfig,
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BaseNode_cjs_namespaceObject.BaseNode, {
                ...nodeProps,
                type: "uipath.coded.agent",
                id: id,
                selected: selected,
                data: {
                    ...nodeData,
                    display: {
                        label: nodeData.label,
                        subLabel: translations.codedAgentStep,
                        shape: 'rectangle'
                    }
                }
            })
        });
    });
const CodedResourceNodeElement = /*#__PURE__*/ (0, external_react_namespaceObject.memo)(({ data, selected, id, ...nodeProps })=>{
    const nodeData = data;
    const label = nodeData.label.toLowerCase();
    const executionStatus = (0, external_react_namespaceObject.useMemo)(()=>{
        if (nodeData.hasError) return 'Failed';
        if (nodeData.hasSuccess) return 'Completed';
        if (nodeData.hasRunning) return 'InProgress';
    }, [
        nodeData.hasError,
        nodeData.hasSuccess,
        nodeData.hasRunning
    ]);
    const resourceIcon = (0, external_react_namespaceObject.useMemo)(()=>{
        const resourceType = nodeData.type || '';
        if ('tool' === resourceType || label.includes('tool') || label.includes('function')) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
            icon: "wrench",
            size: 40
        });
        if ('context' === resourceType || label.includes('context') || label.includes('knowledge')) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
            icon: "network",
            size: 40
        });
        if ('escalation' === resourceType || label.includes('escalation') || label.includes('human')) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
            icon: "user",
            size: 40
        });
        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
            icon: "message-circle",
            size: 40
        });
    }, [
        label,
        nodeData.type
    ]);
    const statusAdornment = (0, external_react_namespaceObject.useMemo)(()=>{
        if (nodeData.hasError) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
            icon: "circle-alert",
            size: 16,
            color: "var(--canvas-error-icon)"
        });
        if (nodeData.hasSuccess && !nodeData.hasError) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
            icon: "circle-check",
            size: 16,
            color: "var(--canvas-success-icon)"
        });
        if (nodeData.hasRunning && !nodeData.hasError && !nodeData.hasSuccess) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(apollo_wind_namespaceObject.Spinner, {
            size: "sm"
        });
    }, [
        nodeData.hasError,
        nodeData.hasSuccess,
        nodeData.hasRunning
    ]);
    const handleConfigurations = (0, external_react_namespaceObject.useMemo)(()=>[
            {
                position: react_cjs_namespaceObject.Position.Left,
                handles: leftTargetHandle,
                visible: true
            },
            {
                position: react_cjs_namespaceObject.Position.Right,
                handles: rightSourceHandle,
                visible: true
            }
        ], []);
    const baseNodeConfig = (0, external_react_namespaceObject.useMemo)(()=>({
            executionStatusOverride: executionStatus,
            handleConfigurations,
            adornments: {
                topRight: statusAdornment
            },
            iconComponent: resourceIcon
        }), [
        executionStatus,
        handleConfigurations,
        statusAdornment,
        resourceIcon
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
        style: {
            position: 'relative'
        },
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BaseNodeConfigContext_cjs_namespaceObject.BaseNodeOverrideConfigProvider, {
                value: baseNodeConfig,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BaseNode_cjs_namespaceObject.BaseNode, {
                    ...nodeProps,
                    type: "uipath.coded.resource",
                    id: id,
                    selected: selected,
                    data: {
                        ...nodeData,
                        display: {
                            label: void 0,
                            shape: 'circle'
                        }
                    }
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(TextContainer, {
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                    className: "text-foreground-muted",
                    children: nodeData.label
                })
            })
        ]
    });
});
const CodedFlowNodeElement = /*#__PURE__*/ (0, external_react_namespaceObject.memo)(({ data, selected, id, ...nodeProps })=>{
    const nodeData = data;
    const isStart = nodeData.label.toLowerCase().includes('start');
    const isEnd = nodeData.label.toLowerCase().includes('end');
    if (isStart || isEnd) {
        const handleConfigs = isStart ? [
            {
                position: react_cjs_namespaceObject.Position.Right,
                handles: rightOutputHandle,
                visible: true
            }
        ] : [
            {
                position: react_cjs_namespaceObject.Position.Left,
                handles: leftInputHandle,
                visible: true
            }
        ];
        const nodeType = isStart ? 'uipath.coded.flow.start' : 'uipath.coded.flow.end';
        const config = {
            handleConfigurations: handleConfigs
        };
        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
            style: {
                position: 'relative'
            },
            children: [
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BaseNodeConfigContext_cjs_namespaceObject.BaseNodeOverrideConfigProvider, {
                    value: config,
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BaseNode_cjs_namespaceObject.BaseNode, {
                        ...nodeProps,
                        type: nodeType,
                        id: id,
                        selected: selected,
                        data: {
                            ...nodeData,
                            display: {
                                label: void 0,
                                shape: 'square'
                            }
                        }
                    })
                }),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(TextContainer, {
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                        className: "text-xs text-foreground-muted",
                        children: nodeData.label
                    })
                })
            ]
        });
    }
    const handleConfigs = [
        {
            position: react_cjs_namespaceObject.Position.Left,
            handles: leftTargetHandle,
            visible: true
        },
        {
            position: react_cjs_namespaceObject.Position.Right,
            handles: rightSourceHandle,
            visible: true
        }
    ];
    const config = {
        handleConfigurations: handleConfigs,
        iconComponent: null
    };
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BaseNodeConfigContext_cjs_namespaceObject.BaseNodeOverrideConfigProvider, {
        value: config,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BaseNode_cjs_namespaceObject.BaseNode, {
            ...nodeProps,
            type: "uipath.coded.flow.node",
            id: id,
            selected: selected,
            data: {
                ...nodeData,
                display: {
                    label: nodeData.label,
                    shape: 'rectangle'
                }
            }
        })
    });
});
const edgeTypes = {
    default: CodedAgentEdge
};
const CodedAgentFlowInner = (props)=>{
    const reactFlowInstance = (0, react_cjs_namespaceObject.useReactFlow)();
    const [nodes, setNodes, onNodesChange] = (0, react_cjs_namespaceObject.useNodesState)([]);
    const [edges, setEdges] = (0, react_cjs_namespaceObject.useEdgesState)([]);
    const [isLoading, setIsLoading] = (0, external_react_namespaceObject.useState)(false);
    const [parseError, setParseError] = (0, external_react_namespaceObject.useState)(null);
    const hasInitialized = (0, external_react_namespaceObject.useRef)(false);
    const edgesRef = (0, external_react_namespaceObject.useRef)([]);
    const agentNodeTranslations = (0, external_react_namespaceObject.useMemo)(()=>props.agentNodeTranslations ?? external_types_cjs_namespaceObject.DefaultCodedAgentNodeTranslations, [
        props.agentNodeTranslations
    ]);
    const nodeTypes = (0, external_react_namespaceObject.useMemo)(()=>({
            agent: createCodedAgentNodeWrapper(agentNodeTranslations),
            resource: CodedResourceNodeElement,
            flow: CodedFlowNodeElement
        }), [
        agentNodeTranslations
    ]);
    const layoutDirection = (0, external_react_namespaceObject.useMemo)(()=>props.layoutDirection || 'LR', [
        props.layoutDirection
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        const processData = async ()=>{
            hasInitialized.current = false;
            if (props.mermaidText && props.mermaidText.length > 0) {
                setIsLoading(true);
                setParseError(null);
                try {
                    const parsedResult = await (0, mermaid_parser_cjs_namespaceObject.mermaidToReactFlow)(props.mermaidText);
                    if (0 === parsedResult.nodes.length) throw new Error('Invalid mermaid syntax');
                    edgesRef.current = parsedResult.edges;
                    setNodes(parsedResult.nodes);
                    setEdges(parsedResult.edges);
                } catch (error) {
                    setParseError(error.message);
                    setNodes([]);
                    setEdges([]);
                } finally{
                    setIsLoading(false);
                }
            } else {
                setNodes([]);
                setEdges([]);
            }
        };
        processData();
    }, [
        props.mermaidText,
        setEdges,
        setNodes
    ]);
    const autoLayout = (0, external_react_namespaceObject.useCallback)(async ()=>{
        if (hasInitialized.current) return;
        const currentNodes = reactFlowInstance.getNodes();
        if (0 === currentNodes.length) return;
        try {
            const { nodes: layoutNodes } = await (0, d3_layout_cjs_namespaceObject.d3HierarchyLayout)(currentNodes, edgesRef.current, {
                direction: layoutDirection,
                spacing: LAYOUT_SPACING
            });
            setNodes(layoutNodes);
            hasInitialized.current = true;
        } catch  {}
    }, [
        layoutDirection,
        setNodes,
        reactFlowInstance
    ]);
    if (parseError) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(CenteredDiv, {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("span", {
            className: "text-error",
            children: [
                "Error: ",
                parseError
            ]
        })
    });
    if (isLoading) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(CenteredDiv, {
        children: "Loading..."
    });
    if (0 === nodes.length) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(CenteredDiv, {
        children: agentNodeTranslations.noDataToDisplay
    });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        style: {
            width: '100%',
            height: '100%',
            touchAction: 'none'
        },
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_BaseCanvas_index_cjs_namespaceObject.BaseCanvas, {
            ref: props.canvasRef,
            nodes: nodes,
            edges: edges,
            nodeTypes: nodeTypes,
            edgeTypes: edgeTypes,
            mode: props.mode || 'view',
            initialAutoLayout: autoLayout,
            onNodesChange: onNodesChange,
            deleteKeyCode: [],
            children: [
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(react_cjs_namespaceObject.Panel, {
                    position: "top-left",
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_layouts_index_cjs_namespaceObject.Row, {
                        align: "center",
                        gap: apollo_core_namespaceObject.Spacing.SpacingXs,
                        children: [
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.AgentIcon, {
                                w: 20,
                                h: 20
                            }),
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                                className: "text-xs font-bold",
                                children: "Coded Agent"
                            })
                        ]
                    })
                }),
                /*#__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: props.canvasTranslations ?? external_types_cjs_namespaceObject.DefaultCanvasTranslations
                    })
                })
            ]
        })
    });
};
const CodedAgentFlow = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(NodeRegistryProvider_cjs_namespaceObject.NodeRegistryProvider, {
        manifest: external_coded_agent_manifest_cjs_namespaceObject.codedAgentManifest,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(react_cjs_namespaceObject.ReactFlowProvider, {
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(CodedAgentFlowInner, {
                ...props
            })
        })
    });
exports.CodedAgentFlow = __webpack_exports__.CodedAgentFlow;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "CodedAgentFlow"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
