"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const graph = require("./src-graphlib-graph.cjs");
const util = require("./src-dagre-util.cjs");
const addSubgraphConstraints = require("./src-dagre-order-add-subgraph-constraints.cjs");
const buildLayerGraph = require("./src-dagre-order-build-layer-graph.cjs");
const crossCount = require("./src-dagre-order-cross-count.cjs");
const initOrder = require("./src-dagre-order-init-order.cjs");
const sortSubgraph = require("./src-dagre-order-sort-subgraph.cjs");
const range = require("../lodash-es/range.cjs");
const cloneDeep = require("../lodash-es/cloneDeep.cjs");
const map = require("../lodash-es/map.cjs");
const forEach = require("../lodash-es/forEach.cjs");
function order(g) {
  var maxRank = util.maxRank(g), downLayerGraphs = buildLayerGraphs(g, range.default(1, maxRank + 1), "inEdges"), upLayerGraphs = buildLayerGraphs(g, range.default(maxRank - 1, -1, -1), "outEdges");
  var layering = initOrder.initOrder(g);
  assignOrder(g, layering);
  var bestCC = Number.POSITIVE_INFINITY, best;
  for (var i = 0, lastBest = 0; lastBest < 4; ++i, ++lastBest) {
    sweepLayerGraphs(i % 2 ? downLayerGraphs : upLayerGraphs, i % 4 >= 2);
    layering = util.buildLayerMatrix(g);
    var cc = crossCount.crossCount(g, layering);
    if (cc < bestCC) {
      lastBest = 0;
      best = cloneDeep.default(layering);
      bestCC = cc;
    }
  }
  assignOrder(g, best);
}
function buildLayerGraphs(g, ranks, relationship) {
  return map.default(ranks, function(rank) {
    return buildLayerGraph.buildLayerGraph(g, rank, relationship);
  });
}
function sweepLayerGraphs(layerGraphs, biasRight) {
  var cg = new graph.Graph();
  forEach.default(layerGraphs, function(lg) {
    var root = lg.graph().root;
    var sorted = sortSubgraph.sortSubgraph(lg, root, cg, biasRight);
    forEach.default(sorted.vs, function(v, i) {
      lg.node(v).order = i;
    });
    addSubgraphConstraints.addSubgraphConstraints(lg, cg, sorted.vs);
  });
}
function assignOrder(g, layering) {
  forEach.default(layering, function(layer) {
    forEach.default(layer, function(v, i) {
      g.node(v).order = i;
    });
  });
}
exports.order = order;
//# sourceMappingURL=src-dagre-order-index.cjs.map
