import e from "../../../lodash-es/constant.js";
import t from "../../../lodash-es/map.js";
import n from "../../../lodash-es/flatten.js";
import r from "../../../lodash-es/forEach.js";
import i from "../../../lodash-es/range.js";
import { Graph as a } from "../graphlib/graph.js";
import "../graphlib/index.js";
import { List as o } from "./data/list.js";
//#region ../../node_modules/dagre-d3-es/src/dagre/greedy-fas.js
var s = e(1);
function c(e, r) {
	if (e.nodeCount() <= 1) return [];
	var i = d(e, r || s);
	return n(t(l(i.graph, i.buckets, i.zeroIdx), function(t) {
		return e.outEdges(t.v, t.w);
	}));
}
function l(e, t, n) {
	for (var r = [], i = t[t.length - 1], a = t[0], o; e.nodeCount();) {
		for (; o = a.dequeue();) u(e, t, n, o);
		for (; o = i.dequeue();) u(e, t, n, o);
		if (e.nodeCount()) {
			for (var s = t.length - 2; s > 0; --s) if (o = t[s].dequeue(), o) {
				r = r.concat(u(e, t, n, o, !0));
				break;
			}
		}
	}
	return r;
}
function u(e, t, n, i, a) {
	var o = a ? [] : void 0;
	return r(e.inEdges(i.v), function(r) {
		var i = e.edge(r), s = e.node(r.v);
		a && o.push({
			v: r.v,
			w: r.w
		}), s.out -= i, f(t, n, s);
	}), r(e.outEdges(i.v), function(r) {
		var i = e.edge(r), a = r.w, o = e.node(a);
		o.in -= i, f(t, n, o);
	}), e.removeNode(i.v), o;
}
function d(e, t) {
	var n = new a(), s = 0, c = 0;
	r(e.nodes(), function(e) {
		n.setNode(e, {
			v: e,
			in: 0,
			out: 0
		});
	}), r(e.edges(), function(e) {
		var r = n.edge(e.v, e.w) || 0, i = t(e), a = r + i;
		n.setEdge(e.v, e.w, a), c = Math.max(c, n.node(e.v).out += i), s = Math.max(s, n.node(e.w).in += i);
	});
	var l = i(c + s + 3).map(function() {
		return new o();
	}), u = s + 1;
	return r(n.nodes(), function(e) {
		f(l, u, n.node(e));
	}), {
		graph: n,
		buckets: l,
		zeroIdx: u
	};
}
function f(e, t, n) {
	n.out ? n.in ? e[n.out - n.in + t].enqueue(n) : e[e.length - 1].enqueue(n) : e[0].enqueue(n);
}
//#endregion
export { c as greedyFAS };
