import e from "../../../lodash-es/forEach.js";
import t from "../../../lodash-es/reduce.js";
import n from "../../../lodash-es/values.js";
import r from "../../../lodash-es/max.js";
import { addBorderNode as i, addDummyNode as a } from "./util.js";
//#region ../../node_modules/dagre-d3-es/src/dagre/nesting-graph.js
function o(t) {
	var i = a(t, "root", {}, "_root"), o = c(t), u = r(n(o)) - 1, d = 2 * u + 1;
	t.graph().nestingRoot = i, e(t.edges(), function(e) {
		t.edge(e).minlen *= d;
	});
	var f = l(t) + 1;
	e(t.children(), function(e) {
		s(t, i, d, f, u, o, e);
	}), t.graph().nodeRankFactor = d;
}
function s(t, n, r, a, o, c, l) {
	var u = t.children(l);
	if (!u.length) {
		l !== n && t.setEdge(n, l, {
			weight: 0,
			minlen: r
		});
		return;
	}
	var d = i(t, "_bt"), f = i(t, "_bb"), p = t.node(l);
	t.setParent(d, l), p.borderTop = d, t.setParent(f, l), p.borderBottom = f, e(u, function(e) {
		s(t, n, r, a, o, c, e);
		var i = t.node(e), u = i.borderTop ? i.borderTop : e, p = i.borderBottom ? i.borderBottom : e, m = i.borderTop ? a : 2 * a, h = u === p ? o - c[l] + 1 : 1;
		t.setEdge(d, u, {
			weight: m,
			minlen: h,
			nestingEdge: !0
		}), t.setEdge(p, f, {
			weight: m,
			minlen: h,
			nestingEdge: !0
		});
	}), t.parent(l) || t.setEdge(n, d, {
		weight: 0,
		minlen: o + c[l]
	});
}
function c(t) {
	var n = {};
	function r(i, a) {
		var o = t.children(i);
		o && o.length && e(o, function(e) {
			r(e, a + 1);
		}), n[i] = a;
	}
	return e(t.children(), function(e) {
		r(e, 1);
	}), n;
}
function l(e) {
	return t(e.edges(), function(t, n) {
		return t + e.edge(n).weight;
	}, 0);
}
function u(t) {
	var n = t.graph();
	t.removeNode(n.nestingRoot), delete n.nestingRoot, e(t.edges(), function(e) {
		t.edge(e).nestingEdge && t.removeEdge(e);
	});
}
//#endregion
export { u as cleanup, o as run };
