"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const zipObject = require("../lodash-es/zipObject.cjs");
const map = require("../lodash-es/map.cjs");
const flatten = require("../lodash-es/flatten.cjs");
const sortBy = require("../lodash-es/sortBy.cjs");
const forEach = require("../lodash-es/forEach.cjs");
function crossCount(g, layering) {
  var cc = 0;
  for (var i = 1; i < layering.length; ++i) {
    cc += twoLayerCrossCount(g, layering[i - 1], layering[i]);
  }
  return cc;
}
function twoLayerCrossCount(g, northLayer, southLayer) {
  var southPos = zipObject.default(
    southLayer,
    map.default(southLayer, function(v, i) {
      return i;
    })
  );
  var southEntries = flatten.default(
    map.default(northLayer, function(v) {
      return sortBy.default(
        map.default(g.outEdges(v), function(e) {
          return { pos: southPos[e.w], weight: g.edge(e).weight };
        }),
        "pos"
      );
    })
  );
  var firstIndex = 1;
  while (firstIndex < southLayer.length) firstIndex <<= 1;
  var treeSize = 2 * firstIndex - 1;
  firstIndex -= 1;
  var tree = map.default(new Array(treeSize), function() {
    return 0;
  });
  var cc = 0;
  forEach.default(
    // @ts-expect-error
    southEntries.forEach(function(entry) {
      var index = entry.pos + firstIndex;
      tree[index] += entry.weight;
      var weightSum = 0;
      while (index > 0) {
        if (index % 2) {
          weightSum += tree[index + 1];
        }
        index = index - 1 >> 1;
        tree[index] += entry.weight;
      }
      cc += entry.weight * weightSum;
    })
  );
  return cc;
}
exports.crossCount = crossCount;
//# sourceMappingURL=src-dagre-order-cross-count.cjs.map
