import e from "../../../../lodash-es/last.js";
import t from "../../../../lodash-es/forEach.js";
import n from "../../../../lodash-es/find.js";
import r from "../../../../lodash-es/map.js";
import i from "../../../../lodash-es/forIn.js";
import a from "../../../../lodash-es/values.js";
import o from "../../../../lodash-es/mapValues.js";
import s from "../../../../lodash-es/max.js";
import c from "../../../../lodash-es/merge.js";
import l from "../../../../lodash-es/min.js";
import u from "../../../../lodash-es/minBy.js";
import d from "../../../../lodash-es/range.js";
import f from "../../../../lodash-es/reduce.js";
import p from "../../../../lodash-es/sortBy.js";
import { Graph as m } from "../../graphlib/graph.js";
import "../../graphlib/index.js";
import { buildLayerMatrix as h } from "../util.js";
//#region ../../node_modules/dagre-d3-es/src/dagre/position/bk.js
function g(n, r) {
	var i = {};
	function a(r, a) {
		var o = 0, s = 0, c = r.length, l = e(a);
		return t(a, function(e, r) {
			var u = v(n, e), d = u ? n.node(u).order : c;
			(u || e === l) && (t(a.slice(s, r + 1), function(e) {
				t(n.predecessors(e), function(t) {
					var r = n.node(t), a = r.order;
					(a < o || d < a) && !(r.dummy && n.node(e).dummy) && y(i, t, e);
				});
			}), s = r + 1, o = d);
		}), a;
	}
	return f(r, a), i;
}
function _(e, n) {
	var r = {};
	function i(n, i, a, o, s) {
		var c;
		t(d(i, a), function(i) {
			c = n[i], e.node(c).dummy && t(e.predecessors(c), function(t) {
				var n = e.node(t);
				n.dummy && (n.order < o || n.order > s) && y(r, t, c);
			});
		});
	}
	function a(n, r) {
		var a = -1, o, s = 0;
		return t(r, function(t, c) {
			if (e.node(t).dummy === "border") {
				var l = e.predecessors(t);
				l.length && (o = e.node(l[0]).order, i(r, s, c, a, o), s = c, a = o);
			}
			i(r, s, r.length, o, n.length);
		}), r;
	}
	return f(n, a), r;
}
function v(e, t) {
	if (e.node(t).dummy) return n(e.predecessors(t), function(t) {
		return e.node(t).dummy;
	});
}
function y(e, t, n) {
	if (t > n) {
		var r = t;
		t = n, n = r;
	}
	Object.prototype.hasOwnProperty.call(e, t) || Object.defineProperty(e, t, {
		enumerable: !0,
		configurable: !0,
		value: {},
		writable: !0
	});
	var i = e[t];
	Object.defineProperty(i, n, {
		enumerable: !0,
		configurable: !0,
		value: !0,
		writable: !0
	});
}
function b(e, t, n) {
	if (t > n) {
		var r = t;
		t = n, n = r;
	}
	return !!e[t] && Object.prototype.hasOwnProperty.call(e[t], n);
}
function x(e, n, r, i) {
	var a = {}, o = {}, s = {};
	return t(n, function(e) {
		t(e, function(e, t) {
			a[e] = e, o[e] = e, s[e] = t;
		});
	}), t(n, function(e) {
		var n = -1;
		t(e, function(e) {
			var t = i(e);
			if (t.length) {
				t = p(t, function(e) {
					return s[e];
				});
				for (var c = (t.length - 1) / 2, l = Math.floor(c), u = Math.ceil(c); l <= u; ++l) {
					var d = t[l];
					o[e] === e && n < s[d] && !b(r, e, d) && (o[d] = e, o[e] = a[e] = a[d], n = s[d]);
				}
			}
		});
	}), {
		root: a,
		align: o
	};
}
function S(e, n, r, i, a) {
	var o = {}, s = C(e, n, r, a), c = a ? "borderLeft" : "borderRight";
	function l(e, t) {
		for (var n = s.nodes(), r = n.pop(), i = {}; r;) i[r] ? e(r) : (i[r] = !0, n.push(r), n = n.concat(t(r))), r = n.pop();
	}
	function u(e) {
		o[e] = s.inEdges(e).reduce(function(e, t) {
			return Math.max(e, o[t.v] + s.edge(t));
		}, 0);
	}
	function d(t) {
		var n = s.outEdges(t).reduce(function(e, t) {
			return Math.min(e, o[t.w] - s.edge(t));
		}, Infinity), r = e.node(t);
		n !== Infinity && r.borderType !== c && (o[t] = Math.max(o[t], n));
	}
	return l(u, s.predecessors.bind(s)), l(d, s.successors.bind(s)), t(i, function(e) {
		o[e] = o[r[e]];
	}), o;
}
function C(e, n, r, i) {
	var a = new m(), o = e.graph(), s = O(o.nodesep, o.edgesep, i);
	return t(n, function(n) {
		var i;
		t(n, function(t) {
			var n = r[t];
			if (a.setNode(n), i) {
				var o = r[i], c = a.edge(o, n);
				a.setEdge(o, n, Math.max(s(e, t, i), c || 0));
			}
			i = t;
		});
	}), a;
}
function w(e, t) {
	return u(a(t), function(t) {
		var n = -Infinity, r = Infinity;
		return i(t, function(t, i) {
			var a = k(e, i) / 2;
			n = Math.max(t + a, n), r = Math.min(t - a, r);
		}), n - r;
	});
}
function T(e, n) {
	var r = a(n), i = l(r), c = s(r);
	t(["u", "d"], function(r) {
		t(["l", "r"], function(t) {
			var u = r + t, d = e[u], f;
			if (d !== n) {
				var p = a(d);
				f = t === "l" ? i - l(p) : c - s(p), f && (e[u] = o(d, function(e) {
					return e + f;
				}));
			}
		});
	});
}
function E(e, t) {
	return o(e.ul, function(n, i) {
		if (t) return e[t.toLowerCase()][i];
		var a = p(r(e, i));
		return (a[1] + a[2]) / 2;
	});
}
function D(e) {
	var n = h(e), i = c(g(e, n), _(e, n)), s = {}, l;
	return t(["u", "d"], function(c) {
		l = c === "u" ? n : a(n).reverse(), t(["l", "r"], function(t) {
			t === "r" && (l = r(l, function(e) {
				return a(e).reverse();
			}));
			var n = (c === "u" ? e.predecessors : e.successors).bind(e), u = x(e, l, i, n), d = S(e, l, u.root, u.align, t === "r");
			t === "r" && (d = o(d, function(e) {
				return -e;
			})), s[c + t] = d;
		});
	}), T(s, w(e, s)), E(s, e.graph().align);
}
function O(e, t, n) {
	return function(r, i, a) {
		var o = r.node(i), s = r.node(a), c = 0, l;
		if (c += o.width / 2, Object.prototype.hasOwnProperty.call(o, "labelpos")) switch (o.labelpos.toLowerCase()) {
			case "l":
				l = -o.width / 2;
				break;
			case "r":
				l = o.width / 2;
				break;
		}
		if (l && (c += n ? l : -l), l = 0, c += (o.dummy ? t : e) / 2, c += (s.dummy ? t : e) / 2, c += s.width / 2, Object.prototype.hasOwnProperty.call(s, "labelpos")) switch (s.labelpos.toLowerCase()) {
			case "l":
				l = s.width / 2;
				break;
			case "r":
				l = -s.width / 2;
				break;
		}
		return l && (c += n ? l : -l), l = 0, c;
	};
}
function k(e, t) {
	return e.node(t).width;
}
//#endregion
export { D as positionX };

//# sourceMappingURL=bk.js.map