"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__, {
    autoArrangeNodes: ()=>autoArrangeNodes,
    RESOURCE_NODE_SIZE: ()=>RESOURCE_NODE_SIZE,
    getAgentGroupBottomPosition: ()=>getAgentGroupBottomPosition
});
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 GROUP_DISTANCE_HORIZONTAL = 280;
const GROUP_DISTANCE_VERTICAL = 200;
const GROUP_SPACING = 160;
const AGENT_NODE_OFFSET_HORIZONTAL = 360;
const AGENT_NODE_OFFSET_VERTICAL_SMALL = 50;
const AGENT_NODE_OFFSET_VERTICAL_LARGE = 300;
const getResourceNodeTypeOrder = (nodeType)=>AgentFlow_constants_cjs_namespaceObject.ResourceNodeTypeOrder[nodeType] ?? 999;
const RESOURCE_NODE_SIZE = 80;
const getAgentGroupBottomPosition = (agent, nodes, edges)=>{
    const agentWidth = agent.measured?.width ?? agent.width ?? 0;
    const agentHeight = agent.measured?.height ?? agent.height ?? 0;
    const agentBottomY = agent.position.y + agentHeight;
    let bottomMostY = agentBottomY;
    let bottomMostX = agent.position.x + agentWidth / 2;
    const groups = {
        [AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.Context]: [],
        [AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.Escalation]: [],
        [AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.MCP]: [],
        [AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.Tool]: [],
        [AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.MemorySpace]: [],
        [AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.A2A]: []
    };
    for (const edge of edges)if (edge.source === agent.id && edge.sourceHandle && edge.sourceHandle in groups) {
        const node = nodes.find((n)=>n.id === edge.target);
        if (node && edge.sourceHandle in groups) {
            const group = groups[edge.sourceHandle];
            if (group) group.push(node);
        }
    } else if (edge.target === agent.id && edge.targetHandle && edge.targetHandle in groups) {
        const node = nodes.find((n)=>n.id === edge.source);
        if (node && edge.targetHandle in groups) {
            const group = groups[edge.targetHandle];
            if (group) group.push(node);
        }
    }
    for (const [_, handleNodes] of Object.entries(groups))for (const node of handleNodes){
        const nodeHeight = node.measured?.height ?? node.height ?? 0;
        const nodeBottomY = node.position.y + nodeHeight;
        if (nodeBottomY > bottomMostY) {
            bottomMostY = nodeBottomY;
            bottomMostX = node.position.x + (node.measured?.width ?? node.width ?? 0) / 2;
        }
    }
    return {
        x: bottomMostX,
        y: bottomMostY
    };
};
const arrangeAgent = (agent, clonedNodes, edges, heightOffsets, currentDepth)=>{
    const agentWidth = agent.measured?.width ?? agent.width ?? 0;
    const agentHeight = agent.measured?.height ?? agent.height ?? 0;
    const agentCenterX = agent.position.x + agentWidth / 2;
    const agentCenterY = agent.position.y + agentHeight / 2;
    const groups = {
        [AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.Context]: [],
        [AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.Escalation]: [],
        [AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.MCP]: [],
        [AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.Tool]: [],
        [AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.MemorySpace]: [],
        [AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.A2A]: []
    };
    for (const edge of edges)if (edge.source === agent.id && edge.sourceHandle && edge.sourceHandle in groups) {
        const node = clonedNodes.find((n)=>n.id === edge.target);
        if (node && !node.hasExplicitPosition) groups[edge.sourceHandle].push(node);
    } else if (edge.target === agent.id && edge.targetHandle && edge.targetHandle in groups) {
        const node = clonedNodes.find((n)=>n.id === edge.source);
        if (node && !node.hasExplicitPosition) groups[edge.targetHandle].push(node);
    }
    for (const key of Object.keys(groups))groups[key].sort((a, b)=>{
        if (!(0, external_types_cjs_namespaceObject.isAgentFlowResourceNode)(a) || !(0, external_types_cjs_namespaceObject.isAgentFlowResourceNode)(b)) return 0;
        const orderA = a.data.order || 0;
        const orderB = b.data.order || 0;
        return orderA - orderB;
    });
    const positionGroups = {
        [react_cjs_namespaceObject.Position.Top]: [],
        [react_cjs_namespaceObject.Position.Bottom]: [],
        [react_cjs_namespaceObject.Position.Left]: [],
        [react_cjs_namespaceObject.Position.Right]: []
    };
    for (const [handleId, nodes] of Object.entries(groups)){
        const position = AgentFlow_constants_cjs_namespaceObject.ResourceNodeTypeToPosition[handleId];
        if (nodes.length > 0) positionGroups[position].push({
            handleId: handleId,
            nodes
        });
    }
    for (const [position, handleGroups] of Object.entries(positionGroups))if (0 !== handleGroups.length) {
        if (position === react_cjs_namespaceObject.Position.Top || position === react_cjs_namespaceObject.Position.Bottom) {
            const yPosition = position === react_cjs_namespaceObject.Position.Top ? agent.position.y - GROUP_DISTANCE_VERTICAL : agent.position.y + agentHeight + GROUP_DISTANCE_VERTICAL;
            if (1 === handleGroups.length) {
                const handleGroup = handleGroups[0];
                if (!handleGroup) continue;
                const { handleId, nodes } = handleGroup;
                let handleCenterX = agentCenterX;
                if (handleId === AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.Context || handleId === AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.MemorySpace) handleCenterX = agentCenterX - 0.5 * GROUP_SPACING;
                if (handleId === AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.Tool || handleId === AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.MCP || handleId === AgentFlow_constants_cjs_namespaceObject.ResourceNodeType.Escalation) handleCenterX = agentCenterX + 1.5 * GROUP_SPACING;
                for (const [i, node] of nodes.entries()){
                    const nodeWidth = node.measured?.width ?? node.width ?? 0;
                    const nodeHeight = node.measured?.height ?? node.height ?? 0;
                    const singleNodeOffset = 1 === nodes.length ? -(GROUP_SPACING / 2) : 0;
                    node.position = {
                        x: handleCenterX - nodeWidth / 2 + (i - (nodes.length - 1) / 2) * GROUP_SPACING + singleNodeOffset,
                        y: position === react_cjs_namespaceObject.Position.Top ? yPosition - nodeHeight : yPosition
                    };
                }
            } else if (2 === handleGroups.length) {
                const sortedGroups = handleGroups.sort((a, b)=>getResourceNodeTypeOrder(a.handleId) - getResourceNodeTypeOrder(b.handleId));
                for (const [groupIndex, { nodes }] of sortedGroups.entries()){
                    const isFirstGroup = 0 === groupIndex;
                    for (const [i, node] of nodes.entries()){
                        const nodeWidth = node.measured?.width ?? node.width ?? 0;
                        const nodeHeight = node.measured?.height ?? node.height ?? 0;
                        if (isFirstGroup) {
                            const startX = agentCenterX - GROUP_SPACING;
                            node.position = {
                                x: startX - nodeWidth / 2 - i * GROUP_SPACING,
                                y: position === react_cjs_namespaceObject.Position.Top ? yPosition - nodeHeight : yPosition
                            };
                        } else {
                            const startX = agentCenterX + GROUP_SPACING;
                            node.position = {
                                x: startX - nodeWidth / 2 + i * GROUP_SPACING,
                                y: position === react_cjs_namespaceObject.Position.Top ? yPosition - nodeHeight : yPosition
                            };
                        }
                    }
                }
            } else if (3 === handleGroups.length) {
                const sortedGroups = handleGroups.sort((a, b)=>getResourceNodeTypeOrder(a.handleId) - getResourceNodeTypeOrder(b.handleId));
                const centerGroup = sortedGroups[1];
                const centerNodes = centerGroup?.nodes || [];
                const centerCount = centerNodes.length;
                const firstCenterOffset = centerCount > 1 ? -(centerCount - 1) / 2 * GROUP_SPACING : 0;
                const lastCenterOffset = centerCount > 1 ? (centerCount - 1) / 2 * GROUP_SPACING : 0;
                const firstCenterX = agentCenterX + firstCenterOffset;
                const lastCenterX = agentCenterX + lastCenterOffset;
                for (const [groupIndex, { nodes }] of sortedGroups.entries())for (const [i, node] of nodes.entries()){
                    const nodeWidth = node.measured?.width ?? node.width ?? 0;
                    const nodeHeight = node.measured?.height ?? node.height ?? 0;
                    let targetX;
                    if (0 === groupIndex) {
                        const nodeOffset = i * GROUP_SPACING;
                        targetX = firstCenterX - GROUP_SPACING - nodeOffset - nodeWidth / 2;
                    } else if (1 === groupIndex) {
                        const nodeOffset = (i - (nodes.length - 1) / 2) * GROUP_SPACING;
                        targetX = agentCenterX - nodeWidth / 2 + nodeOffset;
                    } else {
                        const nodeOffset = i * GROUP_SPACING;
                        targetX = lastCenterX + GROUP_SPACING + nodeOffset - nodeWidth / 2;
                    }
                    node.position = {
                        x: targetX,
                        y: position === react_cjs_namespaceObject.Position.Top ? yPosition - nodeHeight : yPosition
                    };
                }
            } else {
                const sortedGroups = handleGroups.sort((a, b)=>getResourceNodeTypeOrder(a.handleId) - getResourceNodeTypeOrder(b.handleId));
                const totalGroups = sortedGroups.length;
                for (const [groupIndex, { nodes }] of sortedGroups.entries())for (const [i, node] of nodes.entries()){
                    const nodeWidth = node.measured?.width ?? node.width ?? 0;
                    const nodeHeight = node.measured?.height ?? node.height ?? 0;
                    const groupOffset = (groupIndex - (totalGroups - 1) / 2) * GROUP_SPACING * 2;
                    const nodeOffset = (i - (nodes.length - 1) / 2) * GROUP_SPACING;
                    node.position = {
                        x: agentCenterX - nodeWidth / 2 + groupOffset + nodeOffset,
                        y: position === react_cjs_namespaceObject.Position.Top ? yPosition - nodeHeight : yPosition
                    };
                }
            }
        } else if (position === react_cjs_namespaceObject.Position.Left) {
            const handleGroup = handleGroups[0];
            if (!handleGroup) continue;
            const { nodes } = handleGroup;
            for (const [i, node] of nodes.entries()){
                if ((0, external_types_cjs_namespaceObject.isAgentFlowResourceNode)(node) && 'Agent' === node.data.projectType) continue;
                const nodeWidth = node.measured?.width ?? node.width ?? 0;
                const nodeHeight = node.measured?.height ?? node.height ?? 0;
                node.position = {
                    x: agent.position.x - nodeWidth - GROUP_DISTANCE_HORIZONTAL,
                    y: agentCenterY - nodeHeight / 2 + (i - (nodes.length - 1) / 2) * GROUP_SPACING
                };
            }
        } else if (position === react_cjs_namespaceObject.Position.Right) {
            const handleGroup = handleGroups[0];
            if (!handleGroup) continue;
            const { nodes } = handleGroup;
            for (const [i, node] of nodes.entries()){
                const nodeHeight = node.measured?.height ?? node.height ?? 0;
                const singleNodeOffset = 1 === nodes.length ? -(GROUP_SPACING / 2) : 0;
                node.position = {
                    x: agent.position.x + agentWidth + GROUP_DISTANCE_HORIZONTAL,
                    y: agentCenterY - nodeHeight / 2 + (i - (nodes.length - 1) / 2) * GROUP_SPACING + singleNodeOffset
                };
                if ((0, external_types_cjs_namespaceObject.isAgentFlowResourceNode)(node) && 'Agent' === node.data.projectType) {
                    const agentNode = clonedNodes.find((n)=>n.data.parentNodeId === node.id);
                    if (agentNode) {
                        const agentNodeY = Math.max(node.position.y, heightOffsets[currentDepth] ?? 0);
                        const hasUpperEdge = edges.some((edge)=>{
                            const edgePosition = AgentFlow_constants_cjs_namespaceObject.ResourceNodeTypeToPosition[edge.targetHandle];
                            return edge.source === agentNode.id && edgePosition === react_cjs_namespaceObject.Position.Top;
                        });
                        const leftNodesConnectedToAgentNode = edges.filter((edge)=>{
                            const edgePosition = AgentFlow_constants_cjs_namespaceObject.ResourceNodeTypeToPosition[edge.targetHandle];
                            return edge.source === agentNode.id && edgePosition === react_cjs_namespaceObject.Position.Left;
                        });
                        agentNode.position = {
                            x: node.position.x + (node.measured?.width ?? node.width ?? 0) + GROUP_DISTANCE_HORIZONTAL + AGENT_NODE_OFFSET_HORIZONTAL,
                            y: agentNodeY + (hasUpperEdge ? AGENT_NODE_OFFSET_VERTICAL_LARGE : AGENT_NODE_OFFSET_VERTICAL_SMALL) + (heightOffsets[currentDepth] ? leftNodesConnectedToAgentNode.length / 2 * GROUP_SPACING : 0)
                        };
                        const dimensions = arrangeAgent(agentNode, clonedNodes, edges, heightOffsets, currentDepth + 1);
                        heightOffsets[currentDepth] = Math.max(heightOffsets[currentDepth] ?? 0, dimensions);
                    }
                }
            }
        }
    }
    const xY = getAgentGroupBottomPosition(agent, clonedNodes, edges);
    return xY.y;
};
const autoArrangeNodes = (nodes, edges, agentNodePosition, resourceNodePositions)=>{
    const stickyNotes = nodes.filter(external_types_cjs_namespaceObject.isStickyNoteNode);
    const layoutNodes = nodes.filter((node)=>!(0, external_types_cjs_namespaceObject.isStickyNoteNode)(node));
    const clonedNodes = structuredClone(layoutNodes);
    const agentNodes = clonedNodes.filter(external_types_cjs_namespaceObject.isAgentFlowAgentNode).find((node)=>void 0 === node.data.parentNodeId);
    if (agentNodes) {
        agentNodes.position = agentNodePosition || {
            x: 0,
            y: 0
        };
        const heightOffsets = [];
        arrangeAgent(agentNodes, clonedNodes, edges, heightOffsets, 0);
    }
    if (resourceNodePositions) {
        for (const node of clonedNodes)if ((0, external_types_cjs_namespaceObject.isAgentFlowResourceNode)(node)) {
            const externalPosition = resourceNodePositions[node.id];
            if (externalPosition) {
                node.position = externalPosition;
                node.hasExplicitPosition = true;
            }
        }
    }
    return [
        ...clonedNodes,
        ...stickyNotes
    ];
};
exports.RESOURCE_NODE_SIZE = __webpack_exports__.RESOURCE_NODE_SIZE;
exports.autoArrangeNodes = __webpack_exports__.autoArrangeNodes;
exports.getAgentGroupBottomPosition = __webpack_exports__.getAgentGroupBottomPosition;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "RESOURCE_NODE_SIZE",
    "autoArrangeNodes",
    "getAgentGroupBottomPosition"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
