/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * Compute dynamic width for a node based on its edge degree (connection count).
 * Nodes with many connections get wider to accommodate distributed connection points.
 *
 * @param nodeId - The node identifier
 * @param edges - All edges in the graph
 * @param baseWidth - Minimum width for nodes with few connections (default 300)
 * @param growthPerEdge - Additional width per connection above threshold (default 80)
 * @param threshold - Number of connections before growth kicks in (default 3)
 */
export function computeDynamicWidth(nodeId, edges, baseWidth = 300, growthPerEdge = 80, threshold = 3) {
    const degree = edges.filter((e) => e.from === nodeId || e.to === nodeId).length;
    return baseWidth + Math.max(0, degree - threshold) * growthPerEdge;
}
export async function computeGraphLayout(nodes, edges, options = {}) {
    const dagre = await import('@dagrejs/dagre');
    const dagreLib = dagre.default ?? dagre;
    const g = new dagreLib.graphlib.Graph();
    const graphConfig = {
        rankdir: options.rankdir ?? 'TB',
        nodesep: options.nodesep ?? 80,
        ranksep: options.ranksep ?? 150,
        marginx: options.marginx ?? 40,
        marginy: options.marginy ?? 40,
    };
    if (options.align) {
        graphConfig.align = options.align;
    }
    g.setGraph(graphConfig);
    g.setDefaultEdgeLabel(() => ({}));
    for (const node of nodes) {
        g.setNode(node.id, { width: node.width, height: node.height });
    }
    for (const edge of edges) {
        if (edge.from !== edge.to) {
            g.setEdge(edge.from, edge.to);
        }
    }
    dagreLib.layout(g);
    const results = new Map();
    for (const node of nodes) {
        const positioned = g.node(node.id);
        if (positioned) {
            results.set(node.id, {
                x: Math.round(positioned.x - node.width / 2),
                y: Math.round(positioned.y - positioned.height / 2),
                width: node.width,
                height: positioned.height,
            });
        }
    }
    return results;
}
//# sourceMappingURL=dagre-layout.js.map