import e from "../../../../lodash-es/filter.js";
import t from "../../../../lodash-es/forEach.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(e, n) {
	var r = c(e, e.nodes());
	r = r.slice(0, r.length - 1), t(r, function(t) {
		f(e, n, t);
	});
}
function f(e, t, n) {
	var r = e.node(n).parent;
	e.edge(n, r).cutvalue = p(e, t, n);
}
function p(e, n, r) {
	var i = e.node(r).parent, a = !0, o = n.edge(r, i), s = 0;
	return o ||= (a = !1, n.edge(i, r)), s = o.weight, t(n.nodeEdges(r), function(t) {
		var o = t.v === r, c = o ? t.w : t.v;
		if (c !== i) {
			var l = o === a, u = n.edge(t).weight;
			if (s += l ? u : -u, b(e, r, c)) {
				var d = e.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(e, n, r, i, a) {
	var o = r, s = e.node(i);
	return n[i] = !0, t(e.neighbors(i), function(t) {
		Object.prototype.hasOwnProperty.call(n, t) || (r = h(e, n, r, t, 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 _(t, n, i) {
	var a = i.v, s = i.w;
	n.hasEdge(a, s) || (a = i.w, s = i.v);
	var c = t.node(a), l = t.node(s), u = c, d = !1;
	return c.lim > l.lim && (u = l, d = !0), r(e(n.edges(), function(e) {
		return d === x(t, t.node(e.v), u) && d !== x(t, t.node(e.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(e, r) {
	var i = l(e, n(e.nodes(), function(e) {
		return !r.node(e).parent;
	}));
	i = i.slice(1), t(i, function(t) {
		var n = e.node(t).parent, i = r.edge(t, n), a = !1;
		i || (i = r.edge(n, t), a = !0), r.node(t).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 };
