import e from "../../../lodash-es/defaults.js";
import t from "../../../lodash-es/last.js";
import n from "../../../lodash-es/forEach.js";
import r from "../../../lodash-es/mapValues.js";
import i from "../../../lodash-es/max.js";
import a from "../../../lodash-es/merge.js";
import o from "../../../lodash-es/pick.js";
import { Graph as s } from "../graphlib/graph.js";
import "../graphlib/index.js";
import { run as c, undo as l } from "./acyclic.js";
import { addDummyNode as u, asNonCompoundGraph as d, buildLayerMatrix as f, intersectRect as p, normalizeRanks as m, notime as h, removeEmptyRanks as g, time as _ } from "./util.js";
import { addBorderSegments as v } from "./add-border-segments.js";
import { adjust as y, undo as b } from "./coordinate-system.js";
import { run as x, undo as S } from "./normalize.js";
import { rank as C } from "./rank/index.js";
import { cleanup as w, run as T } from "./nesting-graph.js";
import { order as E } from "./order/index.js";
import { parentDummyChains as D } from "./parent-dummy-chains.js";
import { position as O } from "./position/index.js";
//#region ../../node_modules/dagre-d3-es/src/dagre/layout.js
function k(e, t) {
	var n = t && t.debugTiming ? _ : h;
	n("layout", () => {
		var t = n("  buildLayoutGraph", () => z(e));
		n("  runLayout", () => A(t, n)), n("  updateInputGraph", () => j(e, t));
	});
}
function A(e, t) {
	t("    makeSpaceForEdgeLabels", () => B(e)), t("    removeSelfEdges", () => Y(e)), t("    acyclic", () => c(e)), t("    nestingGraph.run", () => T(e)), t("    rank", () => C(d(e))), t("    injectEdgeLabelProxies", () => V(e)), t("    removeEmptyRanks", () => g(e)), t("    nestingGraph.cleanup", () => w(e)), t("    normalizeRanks", () => m(e)), t("    assignRankMinMax", () => H(e)), t("    removeEdgeLabelProxies", () => U(e)), t("    normalize.run", () => x(e)), t("    parentDummyChains", () => D(e)), t("    addBorderSegments", () => v(e)), t("    order", () => E(e)), t("    insertSelfEdges", () => X(e)), t("    adjustCoordinateSystem", () => y(e)), t("    position", () => O(e)), t("    positionSelfEdges", () => Z(e)), t("    removeBorderNodes", () => J(e)), t("    normalize.undo", () => S(e)), t("    fixupEdgeLabelCoords", () => K(e)), t("    undoCoordinateSystem", () => b(e)), t("    translateGraph", () => W(e)), t("    assignNodeIntersects", () => G(e)), t("    reversePoints", () => q(e)), t("    acyclic.undo", () => l(e));
}
function j(e, t) {
	n(e.nodes(), function(n) {
		var r = e.node(n), i = t.node(n);
		r && (r.x = i.x, r.y = i.y, t.children(n).length && (r.width = i.width, r.height = i.height));
	}), n(e.edges(), function(n) {
		var r = e.edge(n), i = t.edge(n);
		r.points = i.points, Object.prototype.hasOwnProperty.call(i, "x") && (r.x = i.x, r.y = i.y);
	}), e.graph().width = t.graph().width, e.graph().height = t.graph().height;
}
var M = [
	"nodesep",
	"edgesep",
	"ranksep",
	"marginx",
	"marginy"
], N = {
	ranksep: 50,
	edgesep: 20,
	nodesep: 50,
	rankdir: "tb"
}, P = [
	"acyclicer",
	"ranker",
	"rankdir",
	"align"
], ee = ["width", "height"], F = {
	width: 0,
	height: 0
}, I = [
	"minlen",
	"weight",
	"width",
	"height",
	"labeloffset"
], L = {
	minlen: 1,
	weight: 1,
	width: 0,
	height: 0,
	labeloffset: 10,
	labelpos: "r"
}, R = ["labelpos"];
function z(t) {
	var r = new s({
		multigraph: !0,
		compound: !0
	}), i = $(t.graph());
	return r.setGraph(a({}, N, Q(i, M), o(i, P))), n(t.nodes(), function(n) {
		var i = $(t.node(n));
		r.setNode(n, e(Q(i, ee), F)), r.setParent(n, t.parent(n));
	}), n(t.edges(), function(e) {
		var n = $(t.edge(e));
		r.setEdge(e, a({}, L, Q(n, I), o(n, R)));
	}), r;
}
function B(e) {
	var t = e.graph();
	t.ranksep /= 2, n(e.edges(), function(n) {
		var r = e.edge(n);
		r.minlen *= 2, r.labelpos.toLowerCase() !== "c" && (t.rankdir === "TB" || t.rankdir === "BT" ? r.width += r.labeloffset : r.height += r.labeloffset);
	});
}
function V(e) {
	n(e.edges(), function(t) {
		var n = e.edge(t);
		if (n.width && n.height) {
			var r = e.node(t.v);
			u(e, "edge-proxy", {
				rank: (e.node(t.w).rank - r.rank) / 2 + r.rank,
				e: t
			}, "_ep");
		}
	});
}
function H(e) {
	var t = 0;
	n(e.nodes(), function(n) {
		var r = e.node(n);
		r.borderTop && (r.minRank = e.node(r.borderTop).rank, r.maxRank = e.node(r.borderBottom).rank, t = i(t, r.maxRank));
	}), e.graph().maxRank = t;
}
function U(e) {
	n(e.nodes(), function(t) {
		var n = e.node(t);
		n.dummy === "edge-proxy" && (e.edge(n.e).labelRank = n.rank, e.removeNode(t));
	});
}
function W(e) {
	var t = Infinity, r = 0, i = Infinity, a = 0, o = e.graph(), s = o.marginx || 0, c = o.marginy || 0;
	function l(e) {
		var n = e.x, o = e.y, s = e.width, c = e.height;
		t = Math.min(t, n - s / 2), r = Math.max(r, n + s / 2), i = Math.min(i, o - c / 2), a = Math.max(a, o + c / 2);
	}
	n(e.nodes(), function(t) {
		l(e.node(t));
	}), n(e.edges(), function(t) {
		var n = e.edge(t);
		Object.prototype.hasOwnProperty.call(n, "x") && l(n);
	}), t -= s, i -= c, n(e.nodes(), function(n) {
		var r = e.node(n);
		r.x -= t, r.y -= i;
	}), n(e.edges(), function(r) {
		var a = e.edge(r);
		n(a.points, function(e) {
			e.x -= t, e.y -= i;
		}), Object.prototype.hasOwnProperty.call(a, "x") && (a.x -= t), Object.prototype.hasOwnProperty.call(a, "y") && (a.y -= i);
	}), o.width = r - t + s, o.height = a - i + c;
}
function G(e) {
	n(e.edges(), function(t) {
		var n = e.edge(t), r = e.node(t.v), i = e.node(t.w), a, o;
		n.points ? (a = n.points[0], o = n.points[n.points.length - 1]) : (n.points = [], a = i, o = r), n.points.unshift(p(r, a)), n.points.push(p(i, o));
	});
}
function K(e) {
	n(e.edges(), function(t) {
		var n = e.edge(t);
		if (Object.prototype.hasOwnProperty.call(n, "x")) switch ((n.labelpos === "l" || n.labelpos === "r") && (n.width -= n.labeloffset), n.labelpos) {
			case "l":
				n.x -= n.width / 2 + n.labeloffset;
				break;
			case "r":
				n.x += n.width / 2 + n.labeloffset;
				break;
		}
	});
}
function q(e) {
	n(e.edges(), function(t) {
		var n = e.edge(t);
		n.reversed && n.points.reverse();
	});
}
function J(e) {
	n(e.nodes(), function(n) {
		if (e.children(n).length) {
			var r = e.node(n), i = e.node(r.borderTop), a = e.node(r.borderBottom), o = e.node(t(r.borderLeft)), s = e.node(t(r.borderRight));
			r.width = Math.abs(s.x - o.x), r.height = Math.abs(a.y - i.y), r.x = o.x + r.width / 2, r.y = i.y + r.height / 2;
		}
	}), n(e.nodes(), function(t) {
		e.node(t).dummy === "border" && e.removeNode(t);
	});
}
function Y(e) {
	n(e.edges(), function(t) {
		if (t.v === t.w) {
			var n = e.node(t.v);
			n.selfEdges ||= [], n.selfEdges.push({
				e: t,
				label: e.edge(t)
			}), e.removeEdge(t);
		}
	});
}
function X(e) {
	n(f(e), function(t) {
		var r = 0;
		n(t, function(t, i) {
			var a = e.node(t);
			a.order = i + r, n(a.selfEdges, function(t) {
				u(e, "selfedge", {
					width: t.label.width,
					height: t.label.height,
					rank: a.rank,
					order: i + ++r,
					e: t.e,
					label: t.label
				}, "_se");
			}), delete a.selfEdges;
		});
	});
}
function Z(e) {
	n(e.nodes(), function(t) {
		var n = e.node(t);
		if (n.dummy === "selfedge") {
			var r = e.node(n.e.v), i = r.x + r.width / 2, a = r.y, o = n.x - i, s = r.height / 2;
			e.setEdge(n.e, n.label), e.removeNode(t), n.label.points = [
				{
					x: i + 2 * o / 3,
					y: a - s
				},
				{
					x: i + 5 * o / 6,
					y: a - s
				},
				{
					x: i + o,
					y: a
				},
				{
					x: i + 5 * o / 6,
					y: a + s
				},
				{
					x: i + 2 * o / 3,
					y: a + s
				}
			], n.label.x = n.x, n.label.y = n.y;
		}
	});
}
function Q(e, t) {
	return r(o(e, t), Number);
}
function $(e) {
	var t = {};
	return n(e, function(e, n) {
		t[n.toLowerCase()] = e;
	}), t;
}
//#endregion
export { k as layout };

//# sourceMappingURL=layout.js.map