import now_default from "../../../lodash-es/now.js";
import forEach_default from "../../../lodash-es/forEach.js";
import map_default from "../../../lodash-es/map.js";
import has_default from "../../../lodash-es/has.js";
import isUndefined_default from "../../../lodash-es/isUndefined.js";
import max_default from "../../../lodash-es/max.js";
import min_default from "../../../lodash-es/min.js";
import range_default from "../../../lodash-es/range.js";
import uniqueId_default from "../../../lodash-es/uniqueId.js";
import { Graph } from "../graphlib/graph.js";
import "../graphlib/index.js";
function addDummyNode(e, p, m, h) {
	var g;
	do
		g = uniqueId_default(h);
	while (e.hasNode(g));
	return m.dummy = p, e.setNode(g, m), g;
}
function simplify(e) {
	var m = new Graph().setGraph(e.graph());
	return forEach_default(e.nodes(), function(p) {
		m.setNode(p, e.node(p));
	}), forEach_default(e.edges(), function(p) {
		var h = m.edge(p.v, p.w) || {
			weight: 0,
			minlen: 1
		}, g = e.edge(p);
		m.setEdge(p.v, p.w, {
			weight: h.weight + g.weight,
			minlen: Math.max(h.minlen, g.minlen)
		});
	}), m;
}
function asNonCompoundGraph(e) {
	var m = new Graph({ multigraph: e.isMultigraph() }).setGraph(e.graph());
	return forEach_default(e.nodes(), function(p) {
		e.children(p).length || m.setNode(p, e.node(p));
	}), forEach_default(e.edges(), function(p) {
		m.setEdge(p, e.edge(p));
	}), m;
}
function intersectRect(e, p) {
	var m = e.x, h = e.y, g = p.x - m, _ = p.y - h, v = e.width / 2, y = e.height / 2;
	if (!g && !_) throw Error("Not possible to find intersection inside of the rectangle");
	var b, x;
	return Math.abs(_) * v > Math.abs(g) * y ? (_ < 0 && (y = -y), b = y * g / _, x = y) : (g < 0 && (v = -v), b = v, x = v * _ / g), {
		x: m + b,
		y: h + x
	};
}
function buildLayerMatrix(e) {
	var h = map_default(range_default(maxRank(e) + 1), function() {
		return [];
	});
	return forEach_default(e.nodes(), function(p) {
		var m = e.node(p), _ = m.rank;
		isUndefined_default(_) || (h[_][m.order] = p);
	}), h;
}
function normalizeRanks(e) {
	var g = min_default(map_default(e.nodes(), function(p) {
		return e.node(p).rank;
	}));
	forEach_default(e.nodes(), function(p) {
		var m = e.node(p);
		has_default(m, "rank") && (m.rank -= g);
	});
}
function removeEmptyRanks(e) {
	var h = min_default(map_default(e.nodes(), function(p) {
		return e.node(p).rank;
	})), _ = [];
	forEach_default(e.nodes(), function(p) {
		var m = e.node(p).rank - h;
		_[m] || (_[m] = []), _[m].push(p);
	});
	var y = 0, b = e.graph().nodeRankFactor;
	forEach_default(_, function(m, h) {
		isUndefined_default(m) && h % b !== 0 ? --y : y && forEach_default(m, function(p) {
			e.node(p).rank += y;
		});
	});
}
function addBorderNode(e, p, m, h) {
	var g = {
		width: 0,
		height: 0
	};
	return arguments.length >= 4 && (g.rank = m, g.order = h), addDummyNode(e, "border", g, p);
}
function maxRank(e) {
	return max_default(map_default(e.nodes(), function(p) {
		var m = e.node(p).rank;
		if (!isUndefined_default(m)) return m;
	}));
}
function partition(e, m) {
	var h = {
		lhs: [],
		rhs: []
	};
	return forEach_default(e, function(e) {
		m(e) ? h.lhs.push(e) : h.rhs.push(e);
	}), h;
}
function time(p, m) {
	var h = now_default();
	try {
		return m();
	} finally {
		console.log(p + " time: " + (now_default() - h) + "ms");
	}
}
function notime(e, p) {
	return p();
}
export { addBorderNode, addDummyNode, asNonCompoundGraph, buildLayerMatrix, intersectRect, maxRank, normalizeRanks, notime, partition, removeEmptyRanks, simplify, time };
