import forEach_default from "../../../../lodash-es/forEach.js";
import filter_default from "../../../../lodash-es/filter.js";
import map_default from "../../../../lodash-es/map.js";
import isUndefined_default from "../../../../lodash-es/isUndefined.js";
import pick_default from "../../../../lodash-es/pick.js";
function resolveConflicts(r, a) {
	var o = {};
	return forEach_default(r, function(e, n) {
		var r = o[e.v] = {
			indegree: 0,
			in: [],
			out: [],
			vs: [e.v],
			i: n
		};
		isUndefined_default(e.barycenter) || (r.barycenter = e.barycenter, r.weight = e.weight);
	}), forEach_default(a.edges(), function(e) {
		var n = o[e.v], r = o[e.w];
		!isUndefined_default(n) && !isUndefined_default(r) && (r.indegree++, n.out.push(o[e.w]));
	}), doResolveConflicts(filter_default(o, function(e) {
		return !e.indegree;
	}));
}
function doResolveConflicts(o) {
	var s = [];
	function c(e) {
		return function(n) {
			n.merged || (isUndefined_default(n.barycenter) || isUndefined_default(e.barycenter) || n.barycenter >= e.barycenter) && mergeEntries(e, n);
		};
	}
	function l(e) {
		return function(n) {
			n.in.push(e), --n.indegree === 0 && o.push(n);
		};
	}
	for (; o.length;) {
		var u = o.pop();
		s.push(u), forEach_default(u.in.reverse(), c(u)), forEach_default(u.out, l(u));
	}
	return map_default(filter_default(s, function(e) {
		return !e.merged;
	}), function(e) {
		return pick_default(e, [
			"vs",
			"i",
			"barycenter",
			"weight"
		]);
	});
}
function mergeEntries(e, n) {
	var r = 0, i = 0;
	e.weight && (r += e.barycenter * e.weight, i += e.weight), n.weight && (r += n.barycenter * n.weight, i += n.weight), e.vs = n.vs.concat(e.vs), e.barycenter = r / i, e.weight = i, e.i = Math.min(n.i, e.i), n.merged = !0;
}
export { resolveConflicts };
