import e from "../../../lodash-es/merge.js";
import t from "../../../lodash-es/forEach.js";
import n from "../../../lodash-es/defaults.js";
import r from "../../../lodash-es/last.js";
import i from "../../../lodash-es/mapValues.js";
import a from "../../../lodash-es/max.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, n) {
	t(e.nodes(), function(t) {
		var r = e.node(t), i = n.node(t);
		r && (r.x = i.x, r.y = i.y, n.children(t).length && (r.width = i.width, r.height = i.height));
	}), t(e.edges(), function(t) {
		var r = e.edge(t), i = n.edge(t);
		r.points = i.points, Object.prototype.hasOwnProperty.call(i, "x") && (r.x = i.x, r.y = i.y);
	}), e.graph().width = n.graph().width, e.graph().height = n.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(r) {
	var i = new s({
		multigraph: !0,
		compound: !0
	}), a = $(r.graph());
	return i.setGraph(e({}, N, Q(a, M), o(a, P))), t(r.nodes(), function(e) {
		var t = $(r.node(e));
		i.setNode(e, n(Q(t, ee), F)), i.setParent(e, r.parent(e));
	}), t(r.edges(), function(t) {
		var n = $(r.edge(t));
		i.setEdge(t, e({}, L, Q(n, I), o(n, R)));
	}), i;
}
function B(e) {
	var n = e.graph();
	n.ranksep /= 2, t(e.edges(), function(t) {
		var r = e.edge(t);
		r.minlen *= 2, r.labelpos.toLowerCase() !== "c" && (n.rankdir === "TB" || n.rankdir === "BT" ? r.width += r.labeloffset : r.height += r.labeloffset);
	});
}
function V(e) {
	t(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 n = 0;
	t(e.nodes(), function(t) {
		var r = e.node(t);
		r.borderTop && (r.minRank = e.node(r.borderTop).rank, r.maxRank = e.node(r.borderBottom).rank, n = a(n, r.maxRank));
	}), e.graph().maxRank = n;
}
function U(e) {
	t(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 n = Infinity, r = 0, i = Infinity, a = 0, o = e.graph(), s = o.marginx || 0, c = o.marginy || 0;
	function l(e) {
		var t = e.x, o = e.y, s = e.width, c = e.height;
		n = Math.min(n, t - s / 2), r = Math.max(r, t + s / 2), i = Math.min(i, o - c / 2), a = Math.max(a, o + c / 2);
	}
	t(e.nodes(), function(t) {
		l(e.node(t));
	}), t(e.edges(), function(t) {
		var n = e.edge(t);
		Object.prototype.hasOwnProperty.call(n, "x") && l(n);
	}), n -= s, i -= c, t(e.nodes(), function(t) {
		var r = e.node(t);
		r.x -= n, r.y -= i;
	}), t(e.edges(), function(r) {
		var a = e.edge(r);
		t(a.points, function(e) {
			e.x -= n, e.y -= i;
		}), Object.prototype.hasOwnProperty.call(a, "x") && (a.x -= n), Object.prototype.hasOwnProperty.call(a, "y") && (a.y -= i);
	}), o.width = r - n + s, o.height = a - i + c;
}
function G(e) {
	t(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) {
	t(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) {
	t(e.edges(), function(t) {
		var n = e.edge(t);
		n.reversed && n.points.reverse();
	});
}
function J(e) {
	t(e.nodes(), function(t) {
		if (e.children(t).length) {
			var n = e.node(t), i = e.node(n.borderTop), a = e.node(n.borderBottom), o = e.node(r(n.borderLeft)), s = e.node(r(n.borderRight));
			n.width = Math.abs(s.x - o.x), n.height = Math.abs(a.y - i.y), n.x = o.x + n.width / 2, n.y = i.y + n.height / 2;
		}
	}), t(e.nodes(), function(t) {
		e.node(t).dummy === "border" && e.removeNode(t);
	});
}
function Y(e) {
	t(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) {
	t(f(e), function(n) {
		var r = 0;
		t(n, function(n, i) {
			var a = e.node(n);
			a.order = i + r, t(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) {
	t(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 i(o(e, t), Number);
}
function $(e) {
	var n = {};
	return t(e, function(e, t) {
		n[t.toLowerCase()] = e;
	}), n;
}
//#endregion
export { k as layout };
