import { barycenter as w } from "./barycenter.js";
import { resolveConflicts as u } from "./resolve-conflicts.js";
import { sort as O } from "./sort.js";
import j from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/filter.js";
import v from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/forEach.js";
import h from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/flatten.js";
import x from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/isUndefined.js";
function P(r, e, c, o) {
  var a = r.children(e), f = r.node(e), t = f ? f.borderLeft : void 0, s = f ? f.borderRight : void 0, b = {};
  t && (a = j(a, function(i) {
    return i !== t && i !== s;
  }));
  var p = w(r, a);
  v(p, function(i) {
    if (r.children(i.v).length) {
      var d = P(r, i.v, c, o);
      b[i.v] = d, Object.prototype.hasOwnProperty.call(d, "barycenter") && B(i, d);
    }
  });
  var m = u(p, c);
  S(m, b);
  var n = O(m, o);
  if (t && (n.vs = h([t, n.vs, s]), r.predecessors(t).length)) {
    var l = r.node(r.predecessors(t)[0]), y = r.node(r.predecessors(s)[0]);
    Object.prototype.hasOwnProperty.call(n, "barycenter") || (n.barycenter = 0, n.weight = 0), n.barycenter = (n.barycenter * n.weight + l.order + y.order) / (n.weight + 2), n.weight += 2;
  }
  return n;
}
function S(r, e) {
  v(r, function(c) {
    c.vs = h(
      c.vs.map(function(o) {
        return e[o] ? e[o].vs : o;
      })
    );
  });
}
function B(r, e) {
  x(r.barycenter) ? (r.barycenter = e.barycenter, r.weight = e.weight) : (r.barycenter = (r.barycenter * r.weight + e.barycenter * e.weight) / (r.weight + e.weight), r.weight += e.weight);
}
export {
  P as sortSubgraph
};
