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