import e from "../../../../lodash-es/merge.js";
import t from "../../../../lodash-es/last.js";
import n from "../../../../lodash-es/forEach.js";
import r from "../../../../lodash-es/find.js";
import i from "../../../../lodash-es/map.js";
import a from "../../../../lodash-es/forIn.js";
import o from "../../../../lodash-es/values.js";
import s from "../../../../lodash-es/mapValues.js";
import c from "../../../../lodash-es/max.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(e, r) {
	var i = {};
	function a(r, a) {
		var o = 0, s = 0, c = r.length, l = t(a);
		return n(a, function(t, r) {
			var u = v(e, t), d = u ? e.node(u).order : c;
			(u || t === l) && (n(a.slice(s, r + 1), function(t) {
				n(e.predecessors(t), function(n) {
					var r = e.node(n), a = r.order;
					(a < o || d < a) && !(r.dummy && e.node(t).dummy) && y(i, n, t);
				});
			}), s = r + 1, o = d);
		}), a;
	}
	return f(r, a), i;
}
function _(e, t) {
	var r = {};
	function i(t, i, a, o, s) {
		var c;
		n(d(i, a), function(i) {
			c = t[i], e.node(c).dummy && n(e.predecessors(c), function(t) {
				var n = e.node(t);
				n.dummy && (n.order < o || n.order > s) && y(r, t, c);
			});
		});
	}
	function a(t, r) {
		var a = -1, o, s = 0;
		return n(r, function(n, c) {
			if (e.node(n).dummy === "border") {
				var l = e.predecessors(n);
				l.length && (o = e.node(l[0]).order, i(r, s, c, a, o), s = c, a = o);
			}
			i(r, s, r.length, o, t.length);
		}), r;
	}
	return f(t, a), r;
}
function v(e, t) {
	if (e.node(t).dummy) return r(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, t, r, i) {
	var a = {}, o = {}, s = {};
	return n(t, function(e) {
		n(e, function(e, t) {
			a[e] = e, o[e] = e, s[e] = t;
		});
	}), n(t, function(e) {
		var t = -1;
		n(e, function(e) {
			var n = i(e);
			if (n.length) {
				n = p(n, function(e) {
					return s[e];
				});
				for (var c = (n.length - 1) / 2, l = Math.floor(c), u = Math.ceil(c); l <= u; ++l) {
					var d = n[l];
					o[e] === e && t < s[d] && !b(r, e, d) && (o[d] = e, o[e] = a[e] = a[d], t = s[d]);
				}
			}
		});
	}), {
		root: a,
		align: o
	};
}
function S(e, t, r, i, a) {
	var o = {}, s = C(e, t, 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)), n(i, function(e) {
		o[e] = o[r[e]];
	}), o;
}
function C(e, t, r, i) {
	var a = new m(), o = e.graph(), s = O(o.nodesep, o.edgesep, i);
	return n(t, function(t) {
		var i;
		n(t, 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(o(t), function(t) {
		var n = -Infinity, r = Infinity;
		return a(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, t) {
	var r = o(t), i = l(r), a = c(r);
	n(["u", "d"], function(r) {
		n(["l", "r"], function(n) {
			var u = r + n, d = e[u], f;
			if (d !== t) {
				var p = o(d);
				f = n === "l" ? i - l(p) : a - c(p), f && (e[u] = s(d, function(e) {
					return e + f;
				}));
			}
		});
	});
}
function E(e, t) {
	return s(e.ul, function(n, r) {
		if (t) return e[t.toLowerCase()][r];
		var a = p(i(e, r));
		return (a[1] + a[2]) / 2;
	});
}
function D(t) {
	var r = h(t), a = e(g(t, r), _(t, r)), c = {}, l;
	return n(["u", "d"], function(e) {
		l = e === "u" ? r : o(r).reverse(), n(["l", "r"], function(n) {
			n === "r" && (l = i(l, function(e) {
				return o(e).reverse();
			}));
			var r = (e === "u" ? t.predecessors : t.successors).bind(t), u = x(t, l, a, r), d = S(t, l, u.root, u.align, n === "r");
			n === "r" && (d = s(d, function(e) {
				return -e;
			})), c[e + n] = d;
		});
	}), T(c, w(t, c)), E(c, t.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