"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__, {
    EDGE_ID_DELIMITER: ()=>EDGE_ID_DELIMITER,
    NODE_ID_DELIMITER: ()=>NODE_ID_DELIMITER,
    computeNodesAndEdges: ()=>computeNodesAndEdges,
    hasModelError: ()=>hasModelError,
    computeSuggestionNodesAndEdges: ()=>computeSuggestionNodesAndEdges,
    hasAgentRunning: ()=>hasAgentRunning,
    hasModelRunning: ()=>hasModelRunning,
    hasResourceSuccess: ()=>hasResourceSuccess,
    createResourceEdge: ()=>createResourceEdge,
    hasModelSuccess: ()=>hasModelSuccess,
    hasResourceError: ()=>hasResourceError,
    hasResourceRunning: ()=>hasResourceRunning
});
const react_cjs_namespaceObject = require("../xyflow/react.cjs");
const AgentFlow_constants_cjs_namespaceObject = require("../components/AgentCanvas/AgentFlow.constants.cjs");
const external_types_cjs_namespaceObject = require("../types.cjs");
const external_auto_layout_cjs_namespaceObject = require("./auto-layout.cjs");
const SPAN_STATUS = {
    RUNNING: 0,
    SUCCESS: 1,
    ERROR: 2
};
const NODE_ID_DELIMITER = '=>';
const EDGE_ID_DELIMITER = '::';
const parseSpanAttributes = (span)=>{
    if (!span.Attributes) return null;
    try {
        return JSON.parse(span.Attributes);
    } catch  {
        return null;
    }
};
const spanStatus = (span)=>Number.isFinite(span.Status) ? span.Status : SPAN_STATUS.RUNNING;
const getResourceNodeId = (resource)=>resource.id;
const normalizeToolName = (resource)=>{
    if ('escalation' === resource.type) return `escalate_${resource.name.toLowerCase().replace(/\s/g, '_')}`;
    if ('mcp' === resource.type) return `mcp_${resource.name.toLowerCase().replace(/\s/g, '_')}`;
    if ('a2a' === resource.type) return `a2a_${resource.name.toLowerCase().replace(/\s/g, '_')}`;
    return resource.name.replace(/\s+/g, '_');
};
const hasResourceStatus = (resource, spans, targetStatus)=>{
    for (const span of spans){
        const attributes = parseSpanAttributes(span);
        if (attributes) {
            if ('context' === resource.type || 'escalation' === resource.type || 'tool' === resource.type || 'mcp' === resource.type || 'a2a' === resource.type) {
                const normalizedToolName = normalizeToolName(resource);
                if ('string' == typeof attributes.toolName && attributes.toolName.toLowerCase() === normalizedToolName.toLowerCase() && spanStatus(span) === targetStatus) return true;
            }
        }
    }
    return false;
};
const hasResourceRunning = (resource, spans)=>hasResourceStatus(resource, spans, SPAN_STATUS.RUNNING);
const hasResourceError = (resource, spans)=>hasResourceStatus(resource, spans, SPAN_STATUS.ERROR) || Boolean(resource.errors?.length);
const hasResourceSuccess = (resource, spans)=>{
    if (hasResourceRunning(resource, spans)) return false;
    return hasResourceStatus(resource, spans, SPAN_STATUS.SUCCESS);
};
const hasModelStatus = (_model, spans, targetStatus)=>{
    for (const span of spans){
        const attributes = parseSpanAttributes(span);
        if (attributes) {
            if ('completion' === attributes.type && spanStatus(span) === targetStatus) return true;
        }
    }
    return false;
};
const hasModelError = (model, spans)=>hasModelStatus(model, spans, SPAN_STATUS.ERROR);
const hasModelRunning = (model, spans)=>hasModelStatus(model, spans, SPAN_STATUS.RUNNING);
const hasModelSuccess = (model, spans)=>{
    if (hasModelRunning(model, spans)) return false;
    return hasModelStatus(model, spans, SPAN_STATUS.SUCCESS);
};
const hasAgentRunning = (spans)=>{
    for (const span of spans){
        const attributes = parseSpanAttributes(span);
        if (attributes) {
            if ('agentRun' === attributes.type) {
                const isRunning = spanStatus(span) === SPAN_STATUS.RUNNING;
                return isRunning;
            }
        }
    }
    return false;
};
const createAgentNode = (props, parentNodeId)=>{
    const agentId = parentNodeId ? `${parentNodeId}${NODE_ID_DELIMITER}${props.definition.processKey}` : props.definition.processKey || 'agent';
    return {
        id: agentId,
        type: 'agent',
        position: {
            x: 0,
            y: 0
        },
        data: {
            name: props.name,
            description: props.description,
            definition: props.definition,
            parentNodeId,
            instructions: props.instructions
        },
        draggable: Boolean(props.allowDragging && 'design' === props.mode)
    };
};
const createResourceNode = (resource, index, props, parentNodeId)=>{
    const id = getResourceNodeId(resource);
    const baseData = {
        name: resource.name,
        originalName: resource.originalName,
        description: resource.description,
        order: index,
        isActive: props.activeResourceIds?.includes(id) ?? false,
        hasError: hasResourceError(resource, props.spans),
        hasSuccess: hasResourceSuccess(resource, props.spans),
        hasRunning: hasResourceRunning(resource, props.spans),
        hasBreakpoint: resource.hasBreakpoint ?? false,
        isCurrentBreakpoint: resource.isCurrentBreakpoint ?? false,
        hasGuardrails: resource.hasGuardrails ?? false,
        isDisabled: resource.isDisabled ?? false,
        errors: resource.errors,
        errorAction: resource.errorAction
    };
    const hasExplicitPosition = 'position' in resource && 'object' == typeof resource.position && null !== resource.position;
    const position = hasExplicitPosition ? resource.position : {
        x: 0,
        y: 0
    };
    const resourceId = `${resource.name}:${id}`;
    const finalId = parentNodeId ? `${parentNodeId}${NODE_ID_DELIMITER}${resourceId}` : resourceId;
    const createBaseNode = (data)=>{
        const node = {
            id: finalId,
            type: 'resource',
            position,
            draggable: Boolean(props.allowDragging && 'design' === props.mode),
            data,
            hasExplicitPosition
        };
        return node;
    };
    switch(resource.type){
        case 'tool':
            return createBaseNode({
                ...baseData,
                type: 'tool',
                iconUrl: resource.iconUrl,
                projectType: resource.projectType,
                toolType: resource.toolType,
                projectId: resource.projectId,
                parentNodeId,
                isExpandable: resource.isExpandable,
                processName: resource.processName
            });
        case 'context':
            return createBaseNode({
                ...baseData,
                type: 'context',
                projectId: resource.projectId,
                parentNodeId
            });
        case 'mcp':
            return createBaseNode({
                ...baseData,
                type: 'mcp',
                slug: resource.slug,
                folderPath: resource.folderPath,
                availableTools: resource.availableTools,
                parentNodeId
            });
        case 'memorySpace':
            return createBaseNode({
                ...baseData,
                type: 'memorySpace',
                parentNodeId
            });
        case 'a2a':
            return createBaseNode({
                ...baseData,
                type: 'a2a',
                projectId: resource.projectId,
                parentNodeId
            });
        case 'escalation':
        default:
            return createBaseNode({
                ...baseData,
                type: 'escalation',
                projectId: resource.projectId,
                parentNodeId
            });
    }
};
const calculateOptimalHandles = (agentNode, resourceNode)=>{
    const resourceType = resourceNode.data.type;
    const agentHeight = agentNode.measured?.height ?? agentNode.height ?? 0;
    const agentCenterY = agentNode.position.y + agentHeight / 2;
    const resourceHeight = resourceNode.measured?.height ?? resourceNode.height ?? 0;
    const resourceCenterY = resourceNode.position.y + resourceHeight / 2;
    let sourceHandle;
    switch(resourceType){
        case 'context':
            sourceHandle = AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.Context;
            break;
        case 'escalation':
            sourceHandle = AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.Escalation;
            break;
        case 'tool':
            sourceHandle = AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.Tool;
            break;
        case 'mcp':
            sourceHandle = AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.Tool;
            break;
        case 'a2a':
            sourceHandle = AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.Tool;
            break;
        case 'memorySpace':
            sourceHandle = AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.MemorySpace;
            break;
        default:
            sourceHandle = resourceType;
            break;
    }
    const targetHandle = resourceCenterY < agentCenterY ? react_cjs_namespaceObject.Position.Bottom : react_cjs_namespaceObject.Position.Top;
    return {
        sourceHandle,
        targetHandle
    };
};
const createResourceEdge = (agentNode, resourceNode, props)=>{
    const isViewMode = 'view' === props.mode;
    const isResourceActive = resourceNode.data.isActive ?? false;
    const { sourceHandle, targetHandle } = calculateOptimalHandles(agentNode, resourceNode);
    const id = `${agentNode.id}${EDGE_ID_DELIMITER}${resourceNode.id}`;
    return {
        id,
        source: agentNode.id,
        target: resourceNode.id,
        sourceHandle,
        targetHandle,
        type: 'default',
        data: {
            label: null
        },
        animated: isViewMode && isResourceActive,
        selectable: false
    };
};
const computeNodesAndEdges = (props, parentNodeId, existingNodes)=>{
    const agentNode = createAgentNode(props, parentNodeId);
    const filteredResources = props.resources.filter((resource)=>{
        if ('mcp' === resource.type && false === props.enableMcpTools) return false;
        if ('a2a' === resource.type && true !== props.enableA2a) return false;
        return true;
    });
    const existingOrderMap = new Map();
    let maxExistingOrder = -1;
    if (existingNodes) for (const node of existingNodes){
        if (!(0, external_types_cjs_namespaceObject.isAgentFlowResourceNode)(node)) continue;
        if (void 0 === node.data.order) continue;
        existingOrderMap.set(node.id, node.data.order);
        const parts = node.id.split(NODE_ID_DELIMITER);
        const baseId = parts[parts.length - 1] ?? node.id;
        const resourceIdPart = baseId.split(':')[1];
        if (resourceIdPart) existingOrderMap.set(resourceIdPart, node.data.order);
        maxExistingOrder = Math.max(maxExistingOrder, node.data.order);
    }
    let nextOrderIndex = maxExistingOrder + 1;
    const resourceNodes = filteredResources.map((resource)=>{
        const resourceId = getResourceNodeId(resource);
        let order = existingOrderMap.get(resourceId);
        if (void 0 === order) order = nextOrderIndex++;
        return createResourceNode(resource, order, props, agentNode.id);
    });
    const edges = resourceNodes.map((resourceNode)=>createResourceEdge(agentNode, resourceNode, props));
    const allNodes = [
        agentNode,
        ...resourceNodes
    ];
    const arrangedNodes = (0, external_auto_layout_cjs_namespaceObject.autoArrangeNodes)(allNodes, edges, props.agentNodePosition, props.resourceNodePositions);
    return {
        nodes: arrangedNodes,
        edges
    };
};
const computeSuggestionNodesAndEdges = (suggestionGroup, props, agentNode, existingResources, existingNodes)=>{
    const suggestionNodes = [];
    const suggestionEdges = [];
    const existingOrderBySuggestionId = new Map();
    let maxExistingOrder = existingResources.length - 1;
    if (existingNodes) {
        for (const node of existingNodes)if ((0, external_types_cjs_namespaceObject.isAgentFlowResourceNode)(node)) {
            if (void 0 !== node.data.order) {
                maxExistingOrder = Math.max(maxExistingOrder, node.data.order);
                if (node.data.suggestionId) existingOrderBySuggestionId.set(node.data.suggestionId, node.data.order);
            }
        }
    }
    let nextOrderIndex = maxExistingOrder + 1;
    for (const suggestion of suggestionGroup.suggestions)if ('add' === suggestion.type && suggestion.resource) {
        const resource = suggestion.resource;
        let order = existingOrderBySuggestionId.get(suggestion.id);
        if (void 0 === order) order = nextOrderIndex++;
        const node = createResourceNode(resource, order, props, agentNode.id);
        node.data = {
            ...node.data,
            isSuggestion: !suggestion.isStandalone,
            suggestionId: suggestion.id,
            suggestionType: 'add',
            isPlaceholder: true,
            hasRunning: suggestion.isProcessing
        };
        node.style = {
            ...node.style
        };
        suggestionNodes.push(node);
        suggestionEdges.push(createResourceEdge(agentNode, node, props));
    }
    return {
        nodes: suggestionNodes,
        edges: suggestionEdges
    };
};
exports.EDGE_ID_DELIMITER = __webpack_exports__.EDGE_ID_DELIMITER;
exports.NODE_ID_DELIMITER = __webpack_exports__.NODE_ID_DELIMITER;
exports.computeNodesAndEdges = __webpack_exports__.computeNodesAndEdges;
exports.computeSuggestionNodesAndEdges = __webpack_exports__.computeSuggestionNodesAndEdges;
exports.createResourceEdge = __webpack_exports__.createResourceEdge;
exports.hasAgentRunning = __webpack_exports__.hasAgentRunning;
exports.hasModelError = __webpack_exports__.hasModelError;
exports.hasModelRunning = __webpack_exports__.hasModelRunning;
exports.hasModelSuccess = __webpack_exports__.hasModelSuccess;
exports.hasResourceError = __webpack_exports__.hasResourceError;
exports.hasResourceRunning = __webpack_exports__.hasResourceRunning;
exports.hasResourceSuccess = __webpack_exports__.hasResourceSuccess;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "EDGE_ID_DELIMITER",
    "NODE_ID_DELIMITER",
    "computeNodesAndEdges",
    "computeSuggestionNodesAndEdges",
    "createResourceEdge",
    "hasAgentRunning",
    "hasModelError",
    "hasModelRunning",
    "hasModelSuccess",
    "hasResourceError",
    "hasResourceRunning",
    "hasResourceSuccess"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
