"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const graph = require("./src-graphlib-graph.cjs");
const forEach = require("../lodash-es/forEach.cjs");
const min = require("../lodash-es/min.cjs");
const map = require("../lodash-es/map.cjs");
const isUndefined = require("../lodash-es/isUndefined.cjs");
const has = require("../lodash-es/has.cjs");
const uniqueId = require("../lodash-es/uniqueId.cjs");
const range = require("../lodash-es/range.cjs");
const max = require("../lodash-es/max.cjs");
function addDummyNode(g, type, attrs, name) {
  var v;
  do {
    v = uniqueId.default(name);
  } while (g.hasNode(v));
  attrs.dummy = type;
  g.setNode(v, attrs);
  return v;
}
function simplify(g) {
  var simplified = new graph.Graph().setGraph(g.graph());
  forEach.default(g.nodes(), function(v) {
    simplified.setNode(v, g.node(v));
  });
  forEach.default(g.edges(), function(e) {
    var simpleLabel = simplified.edge(e.v, e.w) || { weight: 0, minlen: 1 };
    var label = g.edge(e);
    simplified.setEdge(e.v, e.w, {
      weight: simpleLabel.weight + label.weight,
      minlen: Math.max(simpleLabel.minlen, label.minlen)
    });
  });
  return simplified;
}
function asNonCompoundGraph(g) {
  var simplified = new graph.Graph({ multigraph: g.isMultigraph() }).setGraph(g.graph());
  forEach.default(g.nodes(), function(v) {
    if (!g.children(v).length) {
      simplified.setNode(v, g.node(v));
    }
  });
  forEach.default(g.edges(), function(e) {
    simplified.setEdge(e, g.edge(e));
  });
  return simplified;
}
function intersectRect(rect, point) {
  var x = rect.x;
  var y = rect.y;
  var dx = point.x - x;
  var dy = point.y - y;
  var w = rect.width / 2;
  var h = rect.height / 2;
  if (!dx && !dy) {
    throw new Error("Not possible to find intersection inside of the rectangle");
  }
  var sx, sy;
  if (Math.abs(dy) * w > Math.abs(dx) * h) {
    if (dy < 0) {
      h = -h;
    }
    sx = h * dx / dy;
    sy = h;
  } else {
    if (dx < 0) {
      w = -w;
    }
    sx = w;
    sy = w * dy / dx;
  }
  return { x: x + sx, y: y + sy };
}
function buildLayerMatrix(g) {
  var layering = map.default(range.default(maxRank(g) + 1), function() {
    return [];
  });
  forEach.default(g.nodes(), function(v) {
    var node = g.node(v);
    var rank = node.rank;
    if (!isUndefined.default(rank)) {
      layering[rank][node.order] = v;
    }
  });
  return layering;
}
function normalizeRanks(g) {
  var min$1 = min.default(
    map.default(g.nodes(), function(v) {
      return g.node(v).rank;
    })
  );
  forEach.default(g.nodes(), function(v) {
    var node = g.node(v);
    if (has.default(node, "rank")) {
      node.rank -= min$1;
    }
  });
}
function removeEmptyRanks(g) {
  var offset = min.default(
    map.default(g.nodes(), function(v) {
      return g.node(v).rank;
    })
  );
  var layers = [];
  forEach.default(g.nodes(), function(v) {
    var rank = g.node(v).rank - offset;
    if (!layers[rank]) {
      layers[rank] = [];
    }
    layers[rank].push(v);
  });
  var delta = 0;
  var nodeRankFactor = g.graph().nodeRankFactor;
  forEach.default(layers, function(vs, i) {
    if (isUndefined.default(vs) && i % nodeRankFactor !== 0) {
      --delta;
    } else if (delta) {
      forEach.default(vs, function(v) {
        g.node(v).rank += delta;
      });
    }
  });
}
function addBorderNode(g, prefix, rank, order) {
  var node = {
    width: 0,
    height: 0
  };
  if (arguments.length >= 4) {
    node.rank = rank;
    node.order = order;
  }
  return addDummyNode(g, "border", node, prefix);
}
function maxRank(g) {
  return max.default(
    map.default(g.nodes(), function(v) {
      var rank = g.node(v).rank;
      if (!isUndefined.default(rank)) {
        return rank;
      }
    })
  );
}
function partition(collection, fn) {
  var result = { lhs: [], rhs: [] };
  forEach.default(collection, function(value) {
    if (fn(value)) {
      result.lhs.push(value);
    } else {
      result.rhs.push(value);
    }
  });
  return result;
}
function notime(name, fn) {
  return fn();
}
exports.addBorderNode = addBorderNode;
exports.addDummyNode = addDummyNode;
exports.asNonCompoundGraph = asNonCompoundGraph;
exports.buildLayerMatrix = buildLayerMatrix;
exports.intersectRect = intersectRect;
exports.maxRank = maxRank;
exports.normalizeRanks = normalizeRanks;
exports.notime = notime;
exports.partition = partition;
exports.removeEmptyRanks = removeEmptyRanks;
exports.simplify = simplify;
//# sourceMappingURL=src-dagre-util.cjs.map
