"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const forEach = require("../lodash-es/forEach.cjs");
const isUndefined = require("../lodash-es/isUndefined.cjs");
const filter = require("../lodash-es/filter.cjs");
const map = require("../lodash-es/map.cjs");
const pick = require("../lodash-es/pick.cjs");
function resolveConflicts(entries, cg) {
  var mappedEntries = {};
  forEach.default(entries, function(entry, i) {
    var tmp = mappedEntries[entry.v] = {
      indegree: 0,
      in: [],
      out: [],
      vs: [entry.v],
      i
    };
    if (!isUndefined.default(entry.barycenter)) {
      tmp.barycenter = entry.barycenter;
      tmp.weight = entry.weight;
    }
  });
  forEach.default(cg.edges(), function(e) {
    var entryV = mappedEntries[e.v];
    var entryW = mappedEntries[e.w];
    if (!isUndefined.default(entryV) && !isUndefined.default(entryW)) {
      entryW.indegree++;
      entryV.out.push(mappedEntries[e.w]);
    }
  });
  var sourceSet = filter.default(mappedEntries, function(entry) {
    return !entry.indegree;
  });
  return doResolveConflicts(sourceSet);
}
function doResolveConflicts(sourceSet) {
  var entries = [];
  function handleIn(vEntry) {
    return function(uEntry) {
      if (uEntry.merged) {
        return;
      }
      if (isUndefined.default(uEntry.barycenter) || isUndefined.default(vEntry.barycenter) || uEntry.barycenter >= vEntry.barycenter) {
        mergeEntries(vEntry, uEntry);
      }
    };
  }
  function handleOut(vEntry) {
    return function(wEntry) {
      wEntry["in"].push(vEntry);
      if (--wEntry.indegree === 0) {
        sourceSet.push(wEntry);
      }
    };
  }
  while (sourceSet.length) {
    var entry = sourceSet.pop();
    entries.push(entry);
    forEach.default(entry["in"].reverse(), handleIn(entry));
    forEach.default(entry.out, handleOut(entry));
  }
  return map.default(
    filter.default(entries, function(entry2) {
      return !entry2.merged;
    }),
    function(entry2) {
      return pick.default(entry2, ["vs", "i", "barycenter", "weight"]);
    }
  );
}
function mergeEntries(target, source) {
  var sum = 0;
  var weight = 0;
  if (target.weight) {
    sum += target.barycenter * target.weight;
    weight += target.weight;
  }
  if (source.weight) {
    sum += source.barycenter * source.weight;
    weight += source.weight;
  }
  target.vs = source.vs.concat(target.vs);
  target.barycenter = sum / weight;
  target.weight = weight;
  target.i = Math.min(source.i, target.i);
  source.merged = true;
}
exports.resolveConflicts = resolveConflicts;
//# sourceMappingURL=src-dagre-order-resolve-conflicts.cjs.map
