import flatten_default from "../../../../lodash-es/flatten.js";
import last_default from "../../../../lodash-es/last.js";
import forEach_default from "../../../../lodash-es/forEach.js";
import sortBy_default from "../../../../lodash-es/sortBy.js";
import { partition } from "../util.js";
function sort(r, s) {
	var c = partition(r, function(e) {
		return Object.prototype.hasOwnProperty.call(e, "barycenter");
	}), l = c.lhs, u = sortBy_default(c.rhs, function(e) {
		return -e.i;
	}), d = [], f = 0, p = 0, m = 0;
	l.sort(compareWithBias(!!s)), m = consumeUnsortable(d, u, m), forEach_default(l, function(e) {
		m += e.vs.length, d.push(e.vs), f += e.barycenter * e.weight, p += e.weight, m = consumeUnsortable(d, u, m);
	});
	var h = { vs: flatten_default(d) };
	return p && (h.barycenter = f / p, h.weight = p), h;
}
function consumeUnsortable(e, i, a) {
	for (var o; i.length && (o = last_default(i)).i <= a;) i.pop(), e.push(o.vs), a++;
	return a;
}
function compareWithBias(e) {
	return function(r, i) {
		return r.barycenter < i.barycenter ? -1 : r.barycenter > i.barycenter ? 1 : e ? i.i - r.i : r.i - i.i;
	};
}
export { sort };
