'use strict';

var map = require('../../../../../../lodash-es@4.17.23/node_modules/lodash-es/map.js');
var reduce = require('../../../../../../lodash-es@4.17.23/node_modules/lodash-es/reduce.js');

function barycenter(g, movable) {
  return map.default(movable, function (v) {
    var inV = g.inEdges(v);
    if (!inV.length) {
      return { v: v };
    } else {
      var result = reduce.default(
        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,
      };
    }
  });
}

exports.barycenter = barycenter;
