import { asNonCompoundGraph, buildLayerMatrix } from '../util.mjs';
import { positionX } from './bk.mjs';
import forOwn from '../../../../../../lodash-es@4.17.23/node_modules/lodash-es/forOwn.mjs';
import forEach from '../../../../../../lodash-es@4.17.23/node_modules/lodash-es/forEach.mjs';
import max from '../../../../../../lodash-es@4.17.23/node_modules/lodash-es/max.mjs';
import map from '../../../../../../lodash-es@4.17.23/node_modules/lodash-es/map.mjs';

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

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

function positionY(g) {
  var layering = 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;
  });
}

export { position };
