"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const barycenter = require("./src-dagre-order-barycenter.cjs");
const resolveConflicts = require("./src-dagre-order-resolve-conflicts.cjs");
const sort = require("./src-dagre-order-sort.cjs");
const filter = require("../lodash-es/filter.cjs");
const forEach = require("../lodash-es/forEach.cjs");
const flatten = require("../lodash-es/flatten.cjs");
const isUndefined = require("../lodash-es/isUndefined.cjs");
function sortSubgraph(g, v, cg, biasRight) {
  var movable = g.children(v);
  var node = g.node(v);
  var bl = node ? node.borderLeft : void 0;
  var br = node ? node.borderRight : void 0;
  var subgraphs = {};
  if (bl) {
    movable = filter.default(movable, function(w) {
      return w !== bl && w !== br;
    });
  }
  var barycenters = barycenter.barycenter(g, movable);
  forEach.default(barycenters, function(entry) {
    if (g.children(entry.v).length) {
      var subgraphResult = sortSubgraph(g, entry.v, cg, biasRight);
      subgraphs[entry.v] = subgraphResult;
      if (Object.prototype.hasOwnProperty.call(subgraphResult, "barycenter")) {
        mergeBarycenters(entry, subgraphResult);
      }
    }
  });
  var entries = resolveConflicts.resolveConflicts(barycenters, cg);
  expandSubgraphs(entries, subgraphs);
  var result = sort.sort(entries, biasRight);
  if (bl) {
    result.vs = flatten.default([bl, result.vs, br]);
    if (g.predecessors(bl).length) {
      var blPred = g.node(g.predecessors(bl)[0]), brPred = g.node(g.predecessors(br)[0]);
      if (!Object.prototype.hasOwnProperty.call(result, "barycenter")) {
        result.barycenter = 0;
        result.weight = 0;
      }
      result.barycenter = (result.barycenter * result.weight + blPred.order + brPred.order) / (result.weight + 2);
      result.weight += 2;
    }
  }
  return result;
}
function expandSubgraphs(entries, subgraphs) {
  forEach.default(entries, function(entry) {
    entry.vs = flatten.default(
      entry.vs.map(function(v) {
        if (subgraphs[v]) {
          return subgraphs[v].vs;
        }
        return v;
      })
    );
  });
}
function mergeBarycenters(target, other) {
  if (!isUndefined.default(target.barycenter)) {
    target.barycenter = (target.barycenter * target.weight + other.barycenter * other.weight) / (target.weight + other.weight);
    target.weight += other.weight;
  } else {
    target.barycenter = other.barycenter;
    target.weight = other.weight;
  }
}
exports.sortSubgraph = sortSubgraph;
//# sourceMappingURL=src-dagre-order-sort-subgraph.cjs.map
