import { Graph as w } from "../graphlib/graph.js";
import { addBorderSegments as E } from "./add-border-segments.js";
import { adjust as L, undo as N } from "./coordinate-system.js";
import { run as R, undo as S } from "./acyclic.js";
import { run as I, undo as P } from "./normalize.js";
import { rank as O } from "./rank/index.js";
import { run as M, cleanup as j } from "./nesting-graph.js";
import { order as C } from "./order/index.js";
import { parentDummyChains as T } from "./parent-dummy-chains.js";
import { position as B } from "./position/index.js";
import { notime as A, asNonCompoundGraph as D, removeEmptyRanks as z, normalizeRanks as F, addDummyNode as k, intersectRect as y, buildLayerMatrix as Y } from "./util.js";
import t from "../../../../../lodash-es@4.17.22/node_modules/lodash-es/forEach.js";
import g from "../../../../../lodash-es@4.17.22/node_modules/lodash-es/merge.js";
import m from "../../../../../lodash-es@4.17.22/node_modules/lodash-es/pick.js";
import V from "../../../../../lodash-es@4.17.22/node_modules/lodash-es/defaults.js";
import _ from "../../../../../lodash-es@4.17.22/node_modules/lodash-es/max.js";
import x from "../../../../../lodash-es@4.17.22/node_modules/lodash-es/last.js";
import $ from "../../../../../lodash-es@4.17.22/node_modules/lodash-es/mapValues.js";
function Me(e, r) {
  var n = A;
  n("layout", () => {
    var o = n("  buildLayoutGraph", () => ee(e));
    n("  runLayout", () => X(o, n)), n("  updateInputGraph", () => W(e, o));
  });
}
function X(e, r) {
  r("    makeSpaceForEdgeLabels", () => re(e)), r("    removeSelfEdges", () => fe(e)), r("    acyclic", () => R(e)), r("    nestingGraph.run", () => M(e)), r("    rank", () => O(D(e))), r("    injectEdgeLabelProxies", () => ne(e)), r("    removeEmptyRanks", () => z(e)), r("    nestingGraph.cleanup", () => j(e)), r("    normalizeRanks", () => F(e)), r("    assignRankMinMax", () => oe(e)), r("    removeEdgeLabelProxies", () => ae(e)), r("    normalize.run", () => I(e)), r("    parentDummyChains", () => T(e)), r("    addBorderSegments", () => E(e)), r("    order", () => C(e)), r("    insertSelfEdges", () => ue(e)), r("    adjustCoordinateSystem", () => L(e)), r("    position", () => B(e)), r("    positionSelfEdges", () => pe(e)), r("    removeBorderNodes", () => le(e)), r("    normalize.undo", () => P(e)), r("    fixupEdgeLabelCoords", () => de(e)), r("    undoCoordinateSystem", () => N(e)), r("    translateGraph", () => te(e)), r("    assignNodeIntersects", () => ie(e)), r("    reversePoints", () => se(e)), r("    acyclic.undo", () => S(e));
}
function W(e, r) {
  t(e.nodes(), function(n) {
    var o = e.node(n), a = r.node(n);
    o && (o.x = a.x, o.y = a.y, r.children(n).length && (o.width = a.width, o.height = a.height));
  }), t(e.edges(), function(n) {
    var o = e.edge(n), a = r.edge(n);
    o.points = a.points, Object.prototype.hasOwnProperty.call(a, "x") && (o.x = a.x, o.y = a.y);
  }), e.graph().width = r.graph().width, e.graph().height = r.graph().height;
}
var q = ["nodesep", "edgesep", "ranksep", "marginx", "marginy"], H = { ranksep: 50, edgesep: 20, nodesep: 50, rankdir: "tb" }, J = ["acyclicer", "ranker", "rankdir", "align"], K = ["width", "height"], Q = { width: 0, height: 0 }, U = ["minlen", "weight", "width", "height", "labeloffset"], Z = {
  minlen: 1,
  weight: 1,
  width: 0,
  height: 0,
  labeloffset: 10,
  labelpos: "r"
}, G = ["labelpos"];
function ee(e) {
  var r = new w({ multigraph: !0, compound: !0 }), n = h(e.graph());
  return r.setGraph(
    g({}, H, p(n, q), m(n, J))
  ), t(e.nodes(), function(o) {
    var a = h(e.node(o));
    r.setNode(o, V(p(a, K), Q)), r.setParent(o, e.parent(o));
  }), t(e.edges(), function(o) {
    var a = h(e.edge(o));
    r.setEdge(
      o,
      g({}, Z, p(a, U), m(a, G))
    );
  }), r;
}
function re(e) {
  var r = e.graph();
  r.ranksep /= 2, t(e.edges(), function(n) {
    var o = e.edge(n);
    o.minlen *= 2, o.labelpos.toLowerCase() !== "c" && (r.rankdir === "TB" || r.rankdir === "BT" ? o.width += o.labeloffset : o.height += o.labeloffset);
  });
}
function ne(e) {
  t(e.edges(), function(r) {
    var n = e.edge(r);
    if (n.width && n.height) {
      var o = e.node(r.v), a = e.node(r.w), i = { rank: (a.rank - o.rank) / 2 + o.rank, e: r };
      k(e, "edge-proxy", i, "_ep");
    }
  });
}
function oe(e) {
  var r = 0;
  t(e.nodes(), function(n) {
    var o = e.node(n);
    o.borderTop && (o.minRank = e.node(o.borderTop).rank, o.maxRank = e.node(o.borderBottom).rank, r = _(r, o.maxRank));
  }), e.graph().maxRank = r;
}
function ae(e) {
  t(e.nodes(), function(r) {
    var n = e.node(r);
    n.dummy === "edge-proxy" && (e.edge(n.e).labelRank = n.rank, e.removeNode(r));
  });
}
function te(e) {
  var r = Number.POSITIVE_INFINITY, n = 0, o = Number.POSITIVE_INFINITY, a = 0, i = e.graph(), d = i.marginx || 0, l = i.marginy || 0;
  function c(f) {
    var s = f.x, u = f.y, v = f.width, b = f.height;
    r = Math.min(r, s - v / 2), n = Math.max(n, s + v / 2), o = Math.min(o, u - b / 2), a = Math.max(a, u + b / 2);
  }
  t(e.nodes(), function(f) {
    c(e.node(f));
  }), t(e.edges(), function(f) {
    var s = e.edge(f);
    Object.prototype.hasOwnProperty.call(s, "x") && c(s);
  }), r -= d, o -= l, t(e.nodes(), function(f) {
    var s = e.node(f);
    s.x -= r, s.y -= o;
  }), t(e.edges(), function(f) {
    var s = e.edge(f);
    t(s.points, function(u) {
      u.x -= r, u.y -= o;
    }), Object.prototype.hasOwnProperty.call(s, "x") && (s.x -= r), Object.prototype.hasOwnProperty.call(s, "y") && (s.y -= o);
  }), i.width = n - r + d, i.height = a - o + l;
}
function ie(e) {
  t(e.edges(), function(r) {
    var n = e.edge(r), o = e.node(r.v), a = e.node(r.w), i, d;
    n.points ? (i = n.points[0], d = n.points[n.points.length - 1]) : (n.points = [], i = a, d = o), n.points.unshift(y(o, i)), n.points.push(y(a, d));
  });
}
function de(e) {
  t(e.edges(), function(r) {
    var n = e.edge(r);
    if (Object.prototype.hasOwnProperty.call(n, "x"))
      switch ((n.labelpos === "l" || n.labelpos === "r") && (n.width -= n.labeloffset), n.labelpos) {
        case "l":
          n.x -= n.width / 2 + n.labeloffset;
          break;
        case "r":
          n.x += n.width / 2 + n.labeloffset;
          break;
      }
  });
}
function se(e) {
  t(e.edges(), function(r) {
    var n = e.edge(r);
    n.reversed && n.points.reverse();
  });
}
function le(e) {
  t(e.nodes(), function(r) {
    if (e.children(r).length) {
      var n = e.node(r), o = e.node(n.borderTop), a = e.node(n.borderBottom), i = e.node(x(n.borderLeft)), d = e.node(x(n.borderRight));
      n.width = Math.abs(d.x - i.x), n.height = Math.abs(a.y - o.y), n.x = i.x + n.width / 2, n.y = o.y + n.height / 2;
    }
  }), t(e.nodes(), function(r) {
    e.node(r).dummy === "border" && e.removeNode(r);
  });
}
function fe(e) {
  t(e.edges(), function(r) {
    if (r.v === r.w) {
      var n = e.node(r.v);
      n.selfEdges || (n.selfEdges = []), n.selfEdges.push({ e: r, label: e.edge(r) }), e.removeEdge(r);
    }
  });
}
function ue(e) {
  var r = Y(e);
  t(r, function(n) {
    var o = 0;
    t(n, function(a, i) {
      var d = e.node(a);
      d.order = i + o, t(d.selfEdges, function(l) {
        k(
          e,
          "selfedge",
          {
            width: l.label.width,
            height: l.label.height,
            rank: d.rank,
            order: i + ++o,
            e: l.e,
            label: l.label
          },
          "_se"
        );
      }), delete d.selfEdges;
    });
  });
}
function pe(e) {
  t(e.nodes(), function(r) {
    var n = e.node(r);
    if (n.dummy === "selfedge") {
      var o = e.node(n.e.v), a = o.x + o.width / 2, i = o.y, d = n.x - a, l = o.height / 2;
      e.setEdge(n.e, n.label), e.removeNode(r), n.label.points = [
        { x: a + 2 * d / 3, y: i - l },
        { x: a + 5 * d / 6, y: i - l },
        { x: a + d, y: i },
        { x: a + 5 * d / 6, y: i + l },
        { x: a + 2 * d / 3, y: i + l }
      ], n.label.x = n.x, n.label.y = n.y;
    }
  });
}
function p(e, r) {
  return $(m(e, r), Number);
}
function h(e) {
  var r = {};
  return t(e, function(n, o) {
    r[o.toLowerCase()] = n;
  }), r;
}
export {
  Me as layout
};
