'use strict';

var util = require('../util.js');
var bk = require('./bk.js');
var forOwn = require('../../../../../../lodash-es@4.17.21/node_modules/lodash-es/forOwn.js');
var forEach = require('../../../../../../lodash-es@4.17.21/node_modules/lodash-es/forEach.js');
var max = require('../../../../../../lodash-es@4.17.21/node_modules/lodash-es/max.js');
var map = require('../../../../../../lodash-es@4.17.21/node_modules/lodash-es/map.js');

function position(g) {
  g = util.asNonCompoundGraph(g);

  positionY(g);
  forOwn(bk.positionX(g), function (x, v) {
    g.node(v).x = x;
  });
}

function positionY(g) {
  var layering = util.buildLayerMatrix(g);
  var rankSep = g.graph().ranksep;
  var prevY = 0;
  forEach(layering, function (layer) {
    var maxHeight = max(
      map(layer, function (v) {
        return g.node(v).height;
      }),
    );
    forEach(layer, function (v) {
      g.node(v).y = prevY + maxHeight / 2;
    });
    prevY += maxHeight + rankSep;
  });
}

exports.position = position;
