"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const dagre = tslib_1.__importStar(require("dagre"));
const vector_1 = require("./vector");
const type = ["LR", "RL", "TB", "BT"];
let selection = 0;
function autolayout(models, connections, toggle = false) {
    const g = new dagre.graphlib.Graph();
    // Set an object for the graph label
    g.setGraph({
        rankdir: type[selection],
    });
    // Default to assigning a new object as a label for each new edge.
    g.setDefaultEdgeLabel(function () { return {}; });
    models.forEach((model, i) => {
        g.setNode(i.toString(), { label: model.model.name, width: model.width, height: model.height });
    });
    connections.forEach((conn, i) => {
        g.setEdge(conn.from_model_idx.toString(), conn.to_model_idx.toString());
    });
    dagre.layout(g);
    g.nodes().forEach(v => {
        const node = g.node(v);
        const pos = new vector_1.Vec(node.x, node.y);
        models[parseInt(v)].pos = pos;
    });
    const graph = g.graph();
    if (toggle) {
        if (selection === 4) {
            selection = 0;
        }
        else {
            selection += 1;
        }
    }
    return { width: graph.width, height: graph.height };
}
exports.autolayout = autolayout;
