import forEach_default from "../../../../lodash-es/forEach.js";
import minBy_default from "../../../../lodash-es/minBy.js";
import { Graph } from "../../graphlib/graph.js";
import "../../graphlib/index.js";
import { slack } from "./util.js";
function feasibleTree(e) {
	var t = new Graph({ directed: !1 }), i = e.nodes()[0], c = e.nodeCount();
	t.setNode(i, {});
	for (var l, u; tightTree(t, e) < c;) l = findMinSlackEdge(t, e), u = t.hasNode(l.v) ? slack(e, l) : -slack(e, l), shiftRanks(t, e, u);
	return t;
}
function tightTree(t, n) {
	function i(a) {
		forEach_default(n.nodeEdges(a), function(e) {
			var o = e.v, s = a === o ? e.w : o;
			!t.hasNode(s) && !slack(n, e) && (t.setNode(s, {}), t.setEdge(a, s, {}), i(s));
		});
	}
	return forEach_default(t.nodes(), i), t.nodeCount();
}
function findMinSlackEdge(e, n) {
	return minBy_default(n.edges(), function(t) {
		if (e.hasNode(t.v) !== e.hasNode(t.w)) return slack(n, t);
	});
}
function shiftRanks(t, n, r) {
	forEach_default(t.nodes(), function(e) {
		n.node(e).rank += r;
	});
}
export { feasibleTree };
