import e from "../../../../lodash-es/forEach.js";
import t from "../../../../lodash-es/filter.js";
import n from "../../../../lodash-es/find.js";
import r from "../../../../lodash-es/minBy.js";
import { simplify as i } from "../util.js";
import { longestPath as a, slack as o } from "./util.js";
import { feasibleTree as s } from "./feasible-tree.js";
import { postorder as c } from "../../graphlib/alg/postorder.js";
import { preorder as l } from "../../graphlib/alg/preorder.js";
import "../../graphlib/alg/index.js";
u.initLowLimValues = m, u.initCutValues = d, u.calcCutValue = p, u.leaveEdge = g, u.enterEdge = _, u.exchangeEdges = v;
function u(e) {
	e = i(e), a(e);
	var t = s(e);
	m(t), d(t, e);
	for (var n, r; n = g(t);) r = _(t, e, n), v(t, e, n, r);
}
function d(t, n) {
	var r = c(t, t.nodes());
	r = r.slice(0, r.length - 1), e(r, function(e) {
		f(t, n, e);
	});
}
function f(e, t, n) {
	var r = e.node(n).parent;
	e.edge(n, r).cutvalue = p(e, t, n);
}
function p(t, n, r) {
	var i = t.node(r).parent, a = !0, o = n.edge(r, i), s = 0;
	return o ||= (a = !1, n.edge(i, r)), s = o.weight, e(n.nodeEdges(r), function(e) {
		var o = e.v === r, c = o ? e.w : e.v;
		if (c !== i) {
			var l = o === a, u = n.edge(e).weight;
			if (s += l ? u : -u, b(t, r, c)) {
				var d = t.edge(r, c).cutvalue;
				s += l ? -d : d;
			}
		}
	}), s;
}
function m(e, t) {
	arguments.length < 2 && (t = e.nodes()[0]), h(e, {}, 1, t);
}
function h(t, n, r, i, a) {
	var o = r, s = t.node(i);
	return n[i] = !0, e(t.neighbors(i), function(e) {
		Object.prototype.hasOwnProperty.call(n, e) || (r = h(t, n, r, e, i));
	}), s.low = o, s.lim = r++, a ? s.parent = a : delete s.parent, r;
}
function g(e) {
	return n(e.edges(), function(t) {
		return e.edge(t).cutvalue < 0;
	});
}
function _(e, n, i) {
	var a = i.v, s = i.w;
	n.hasEdge(a, s) || (a = i.w, s = i.v);
	var c = e.node(a), l = e.node(s), u = c, d = !1;
	return c.lim > l.lim && (u = l, d = !0), r(t(n.edges(), function(t) {
		return d === x(e, e.node(t.v), u) && d !== x(e, e.node(t.w), u);
	}), function(e) {
		return o(n, e);
	});
}
function v(e, t, n, r) {
	var i = n.v, a = n.w;
	e.removeEdge(i, a), e.setEdge(r.v, r.w, {}), m(e), d(e, t), y(e, t);
}
function y(t, r) {
	var i = l(t, n(t.nodes(), function(e) {
		return !r.node(e).parent;
	}));
	i = i.slice(1), e(i, function(e) {
		var n = t.node(e).parent, i = r.edge(e, n), a = !1;
		i || (i = r.edge(n, e), a = !0), r.node(e).rank = r.node(n).rank + (a ? i.minlen : -i.minlen);
	});
}
function b(e, t, n) {
	return e.hasEdge(t, n);
}
function x(e, t, n) {
	return n.low <= t.lim && t.lim <= n.lim;
}
//#endregion
export { u as networkSimplex };

//# sourceMappingURL=network-simplex.js.map