import { addDummyNode as B, addBorderNode as E } from "./util.js";
import T from "../../../../../lodash-es@4.17.22/node_modules/lodash-es/max.js";
import w from "../../../../../lodash-es@4.17.22/node_modules/lodash-es/values.js";
import a from "../../../../../lodash-es@4.17.22/node_modules/lodash-es/forEach.js";
import N from "../../../../../lodash-es@4.17.22/node_modules/lodash-es/reduce.js";
function y(e) {
  var r = B(e, "root", {}, "_root"), t = R(e), o = T(w(t)) - 1, i = 2 * o + 1;
  e.graph().nestingRoot = r, a(e.edges(), function(n) {
    e.edge(n).minlen *= i;
  });
  var d = _(e) + 1;
  a(e.children(), function(n) {
    g(e, r, i, d, o, t, n);
  }), e.graph().nodeRankFactor = i;
}
function g(e, r, t, o, i, d, n) {
  var s = e.children(n);
  if (!s.length) {
    n !== r && e.setEdge(r, n, { weight: 0, minlen: t });
    return;
  }
  var f = E(e, "_bt"), c = E(e, "_bb"), l = e.node(n);
  e.setParent(f, n), l.borderTop = f, e.setParent(c, n), l.borderBottom = c, a(s, function(u) {
    g(e, r, t, o, i, d, u);
    var m = e.node(u), h = m.borderTop ? m.borderTop : u, p = m.borderBottom ? m.borderBottom : u, b = m.borderTop ? o : 2 * o, v = h !== p ? 1 : i - d[n] + 1;
    e.setEdge(f, h, {
      weight: b,
      minlen: v,
      nestingEdge: !0
    }), e.setEdge(p, c, {
      weight: b,
      minlen: v,
      nestingEdge: !0
    });
  }), e.parent(n) || e.setEdge(r, f, { weight: 0, minlen: i + d[n] });
}
function R(e) {
  var r = {};
  function t(o, i) {
    var d = e.children(o);
    d && d.length && a(d, function(n) {
      t(n, i + 1);
    }), r[o] = i;
  }
  return a(e.children(), function(o) {
    t(o, 1);
  }), r;
}
function _(e) {
  return N(
    e.edges(),
    function(r, t) {
      return r + e.edge(t).weight;
    },
    0
  );
}
function F(e) {
  var r = e.graph();
  e.removeNode(r.nestingRoot), delete r.nestingRoot, a(e.edges(), function(t) {
    var o = e.edge(t);
    o.nestingEdge && e.removeEdge(t);
  });
}
export {
  F as cleanup,
  y as run
};
