"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const graph = require("./src-graphlib-graph.cjs");
const util = require("./src-dagre-rank-util.cjs");
const forEach = require("../lodash-es/forEach.cjs");
const minBy = require("../lodash-es/minBy.cjs");
function feasibleTree(g) {
  var t = new graph.Graph({ directed: false });
  var start = g.nodes()[0];
  var size = g.nodeCount();
  t.setNode(start, {});
  var edge, delta;
  while (tightTree(t, g) < size) {
    edge = findMinSlackEdge(t, g);
    delta = t.hasNode(edge.v) ? util.slack(g, edge) : -util.slack(g, edge);
    shiftRanks(t, g, delta);
  }
  return t;
}
function tightTree(t, g) {
  function dfs(v) {
    forEach.default(g.nodeEdges(v), function(e) {
      var edgeV = e.v, w = v === edgeV ? e.w : edgeV;
      if (!t.hasNode(w) && !util.slack(g, e)) {
        t.setNode(w, {});
        t.setEdge(v, w, {});
        dfs(w);
      }
    });
  }
  forEach.default(t.nodes(), dfs);
  return t.nodeCount();
}
function findMinSlackEdge(t, g) {
  return minBy.default(g.edges(), function(e) {
    if (t.hasNode(e.v) !== t.hasNode(e.w)) {
      return util.slack(g, e);
    }
  });
}
function shiftRanks(t, g, delta) {
  forEach.default(t.nodes(), function(v) {
    g.node(v).rank += delta;
  });
}
exports.feasibleTree = feasibleTree;
//# sourceMappingURL=src-dagre-rank-feasible-tree.cjs.map
