"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__, {
    mermaidToReactFlow: ()=>mermaidToReactFlow
});
const external_mermaid_namespaceObject = require("mermaid");
var external_mermaid_default = /*#__PURE__*/ __webpack_require__.n(external_mermaid_namespaceObject);
const external_sanitize_html_namespaceObject = require("sanitize-html");
var external_sanitize_html_default = /*#__PURE__*/ __webpack_require__.n(external_sanitize_html_namespaceObject);
function ensureArray(data) {
    if (Array.isArray(data)) return data;
    if (data instanceof Map) return Array.from(data.values());
    if (data && 'object' == typeof data) return Object.values(data);
    return [];
}
function getNodeType(nodeLabel) {
    const lowerLabel = nodeLabel.toLowerCase();
    if (lowerLabel.includes('agent')) return 'agent';
    if (lowerLabel.includes('tool') || lowerLabel.includes('function')) return 'resource';
    if (lowerLabel.includes('context') || lowerLabel.includes('knowledge')) return 'resource';
    if (lowerLabel.includes('escalation') || lowerLabel.includes('human')) return 'resource';
    return 'flow';
}
async function mermaidToReactFlow(mermaidText) {
    try {
        external_mermaid_default().initialize({
            startOnLoad: false
        });
        const parseResult = await external_mermaid_default().parse(mermaidText, {
            suppressErrors: true
        });
        if (!parseResult) throw new Error('Invalid mermaid syntax');
        const diagram = await external_mermaid_default().mermaidAPI.getDiagramFromText(mermaidText);
        let rawNodes = [];
        let rawEdges = [];
        if (diagram && diagram.db) {
            rawNodes = ensureArray(diagram.db.getVertices());
            rawEdges = ensureArray(diagram.db.getEdges());
        }
        if (0 === rawNodes.length) throw new Error('Unable to extract nodes from diagram');
        const edges = rawEdges.map((mermaidEdge)=>({
                id: crypto.randomUUID(),
                source: mermaidEdge.start,
                target: mermaidEdge.end,
                type: 'default',
                label: mermaidEdge.text,
                animated: false,
                markerEnd: 'arrow',
                style: {
                    stroke: 'var(--canvas-foreground-de-emp)',
                    strokeWidth: 1
                },
                data: {
                    label: mermaidEdge.text,
                    raw: mermaidEdge
                }
            }));
        const nodes = rawNodes.map((mermaidNode)=>{
            let label = mermaidNode.text;
            if ('string' == typeof label && label.includes('<')) label = external_sanitize_html_default()(label, {
                allowedTags: [],
                allowedAttributes: {}
            });
            const nodeType = getNodeType(label);
            return {
                id: mermaidNode.id,
                position: {
                    x: 0,
                    y: 0
                },
                type: nodeType,
                data: {
                    label,
                    raw: mermaidNode
                }
            };
        });
        return {
            nodes,
            edges
        };
    } catch  {
        return {
            nodes: [],
            edges: []
        };
    }
}
exports.mermaidToReactFlow = __webpack_exports__.mermaidToReactFlow;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "mermaidToReactFlow"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
