import { Graph as x } from "../../graphlib/graph.js";
import { buildLayerMatrix as M } from "../util.js";
import k from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/merge.js";
import b from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/forEach.js";
import v from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/values.js";
import P from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/map.js";
import y from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/mapValues.js";
import T from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/reduce.js";
import g from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/sortBy.js";
import C from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/minBy.js";
import w from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/min.js";
import O from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/max.js";
import E from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/last.js";
import V from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/find.js";
import j from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/range.js";
import F from "../../../../../../lodash-es@4.17.22/node_modules/lodash-es/forIn.js";
function G(r, n) {
  var i = {};
  function d(a, u) {
    var e = 0, f = 0, o = a.length, t = E(u);
    return b(u, function(s, c) {
      var l = _(r, s), m = l ? r.node(l).order : o;
      (l || s === t) && (b(u.slice(f, c + 1), function(p) {
        b(r.predecessors(p), function(h) {
          var I = r.node(h), N = I.order;
          (N < e || m < N) && !(I.dummy && r.node(p).dummy) && L(i, h, p);
        });
      }), f = c + 1, e = m);
    }), u;
  }
  return T(n, d), i;
}
function Y(r, n) {
  var i = {};
  function d(u, e, f, o, t) {
    var s;
    b(j(e, f), function(c) {
      s = u[c], r.node(s).dummy && b(r.predecessors(s), function(l) {
        var m = r.node(l);
        m.dummy && (m.order < o || m.order > t) && L(i, l, s);
      });
    });
  }
  function a(u, e) {
    var f = -1, o, t = 0;
    return b(e, function(s, c) {
      if (r.node(s).dummy === "border") {
        var l = r.predecessors(s);
        l.length && (o = r.node(l[0]).order, d(e, t, c, f, o), t = c, f = o);
      }
      d(e, t, e.length, o, u.length);
    }), e;
  }
  return T(n, a), i;
}
function _(r, n) {
  if (r.node(n).dummy)
    return V(r.predecessors(n), function(i) {
      return r.node(i).dummy;
    });
}
function L(r, n, i) {
  if (n > i) {
    var d = n;
    n = i, i = d;
  }
  Object.prototype.hasOwnProperty.call(r, n) || Object.defineProperty(r, n, {
    enumerable: !0,
    configurable: !0,
    value: {},
    writable: !0
  });
  var a = r[n];
  Object.defineProperty(a, i, {
    enumerable: !0,
    configurable: !0,
    value: !0,
    writable: !0
  });
}
function A(r, n, i) {
  if (n > i) {
    var d = n;
    n = i, i = d;
  }
  return !!r[n] && Object.prototype.hasOwnProperty.call(r[n], i);
}
function B(r, n, i, d) {
  var a = {}, u = {}, e = {};
  return b(n, function(f) {
    b(f, function(o, t) {
      a[o] = o, u[o] = o, e[o] = t;
    });
  }), b(n, function(f) {
    var o = -1;
    b(f, function(t) {
      var s = d(t);
      if (s.length) {
        s = g(s, function(h) {
          return e[h];
        });
        for (var c = (s.length - 1) / 2, l = Math.floor(c), m = Math.ceil(c); l <= m; ++l) {
          var p = s[l];
          u[t] === t && o < e[p] && !A(i, t, p) && (u[p] = t, u[t] = a[t] = a[p], o = e[p]);
        }
      }
    });
  }), { root: a, align: u };
}
function W(r, n, i, d, a) {
  var u = {}, e = R(r, n, i, a), f = a ? "borderLeft" : "borderRight";
  function o(c, l) {
    for (var m = e.nodes(), p = m.pop(), h = {}; p; )
      h[p] ? c(p) : (h[p] = !0, m.push(p), m = m.concat(l(p))), p = m.pop();
  }
  function t(c) {
    u[c] = e.inEdges(c).reduce(function(l, m) {
      return Math.max(l, u[m.v] + e.edge(m));
    }, 0);
  }
  function s(c) {
    var l = e.outEdges(c).reduce(function(p, h) {
      return Math.min(p, u[h.w] - e.edge(h));
    }, Number.POSITIVE_INFINITY), m = r.node(c);
    l !== Number.POSITIVE_INFINITY && m.borderType !== f && (u[c] = Math.max(u[c], l));
  }
  return o(t, e.predecessors.bind(e)), o(s, e.successors.bind(e)), b(d, function(c) {
    u[c] = u[i[c]];
  }), u;
}
function R(r, n, i, d) {
  var a = new x(), u = r.graph(), e = D(u.nodesep, u.edgesep, d);
  return b(n, function(f) {
    var o;
    b(f, function(t) {
      var s = i[t];
      if (a.setNode(s), o) {
        var c = i[o], l = a.edge(c, s);
        a.setEdge(c, s, Math.max(e(r, t, o), l || 0));
      }
      o = t;
    });
  }), a;
}
function S(r, n) {
  return C(v(n), function(i) {
    var d = Number.NEGATIVE_INFINITY, a = Number.POSITIVE_INFINITY;
    return F(i, function(u, e) {
      var f = H(r, e) / 2;
      d = Math.max(u + f, d), a = Math.min(u - f, a);
    }), d - a;
  });
}
function X(r, n) {
  var i = v(n), d = w(i), a = O(i);
  b(["u", "d"], function(u) {
    b(["l", "r"], function(e) {
      var f = u + e, o = r[f], t;
      if (o !== n) {
        var s = v(o);
        t = e === "l" ? d - w(s) : a - O(s), t && (r[f] = y(o, function(c) {
          return c + t;
        }));
      }
    });
  });
}
function q(r, n) {
  return y(r.ul, function(i, d) {
    if (n)
      return r[n.toLowerCase()][d];
    var a = g(P(r, d));
    return (a[1] + a[2]) / 2;
  });
}
function cr(r) {
  var n = M(r), i = k(G(r, n), Y(r, n)), d = {}, a;
  b(["u", "d"], function(e) {
    a = e === "u" ? n : v(n).reverse(), b(["l", "r"], function(f) {
      f === "r" && (a = P(a, function(c) {
        return v(c).reverse();
      }));
      var o = (e === "u" ? r.predecessors : r.successors).bind(r), t = B(r, a, i, o), s = W(r, a, t.root, t.align, f === "r");
      f === "r" && (s = y(s, function(c) {
        return -c;
      })), d[e + f] = s;
    });
  });
  var u = S(r, d);
  return X(d, u), q(d, r.graph().align);
}
function D(r, n, i) {
  return function(d, a, u) {
    var e = d.node(a), f = d.node(u), o = 0, t;
    if (o += e.width / 2, Object.prototype.hasOwnProperty.call(e, "labelpos"))
      switch (e.labelpos.toLowerCase()) {
        case "l":
          t = -e.width / 2;
          break;
        case "r":
          t = e.width / 2;
          break;
      }
    if (t && (o += i ? t : -t), t = 0, o += (e.dummy ? n : r) / 2, o += (f.dummy ? n : r) / 2, o += f.width / 2, Object.prototype.hasOwnProperty.call(f, "labelpos"))
      switch (f.labelpos.toLowerCase()) {
        case "l":
          t = f.width / 2;
          break;
        case "r":
          t = -f.width / 2;
          break;
      }
    return t && (o += i ? t : -t), t = 0, o;
  };
}
function H(r, n) {
  return r.node(n).width;
}
export {
  L as addConflict,
  X as alignCoordinates,
  q as balance,
  S as findSmallestWidthAlignment,
  G as findType1Conflicts,
  Y as findType2Conflicts,
  A as hasConflict,
  W as horizontalCompaction,
  cr as positionX,
  B as verticalAlignment
};
