import map from '../../../../../../lodash-es@4.17.23/node_modules/lodash-es/map.mjs';
import reduce from '../../../../../../lodash-es@4.17.23/node_modules/lodash-es/reduce.mjs';

function barycenter(g, movable) {
  return map(movable, function (v) {
    var inV = g.inEdges(v);
    if (!inV.length) {
      return { v: v };
    } else {
      var result = reduce(
        inV,
        function (acc, e) {
          var edge = g.edge(e),
            nodeU = g.node(e.v);
          return {
            sum: acc.sum + edge.weight * nodeU.order,
            weight: acc.weight + edge.weight,
          };
        },
        { sum: 0, weight: 0 },
      );

      return {
        v: v,
        barycenter: result.sum / result.weight,
        weight: result.weight,
      };
    }
  });
}

export { barycenter };
