"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
        for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
            r[k] = a[j];
    return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
function treeLayout(nodes, current, root) {
    var depthMap = {};
    var currentPath = getPathTo(nodes, root, current);
    DFS(nodes, root, depthMap, currentPath);
    return currentPath;
}
exports.treeLayout = treeLayout;
function DFS(nodes, node, depthMap, currentPath) {
    var explored = new Set();
    var toExplore = [];
    var currDepth = 0;
    toExplore.push(nodes[node]);
    while (toExplore.length > 0) {
        var temp = toExplore.pop();
        if (!explored.has(temp.id)) {
            temp.width = currDepth;
            depthMap[temp.id] = temp.width;
            explored.add(temp.id);
        }
        else {
            temp.width = depthMap[temp.id];
        }
        if (temp.children) {
            toExplore.push.apply(toExplore, temp.children.sort(function (a, b) {
                var aIncludes = currentPath.includes(a.id) ? 1 : 0;
                var bIncludes = currentPath.includes(b.id) ? 1 : 0;
                return aIncludes - bIncludes;
            }));
        }
        else {
            currDepth++;
        }
    }
}
function getPathTo(nodes, from, to) {
    var path = [];
    search(nodes, from, to, path);
    return __spreadArrays([from], path.reverse());
}
exports.getPathTo = getPathTo;
function search(nodes, node, final, path) {
    if (!nodes[node])
        return false;
    if (node === final) {
        path.push(node);
        return true;
    }
    var children = nodes[node].children || [];
    for (var _i = 0, children_1 = children; _i < children_1.length; _i++) {
        var child = children_1[_i];
        if (search(nodes, child.id, final, path)) {
            path.push(child.id);
            return true;
        }
    }
    return false;
}
//# sourceMappingURL=TreeLayout.js.map