import { Graph as p } from "../graphlib/graph.js";
import { List as s } from "./data/list.js";
import l from "../../../../../lodash-es@4.17.22/node_modules/lodash-es/flatten.js";
import E from "../../../../../lodash-es@4.17.22/node_modules/lodash-es/map.js";
import d from "../../../../../lodash-es@4.17.22/node_modules/lodash-es/forEach.js";
import c from "../../../../../lodash-es@4.17.22/node_modules/lodash-es/range.js";
import q from "../../../../../lodash-es@4.17.22/node_modules/lodash-es/constant.js";
var g = q(1);
function L(r, t) {
  if (r.nodeCount() <= 1)
    return [];
  var n = F(r, t || g), a = x(n.graph, n.buckets, n.zeroIdx);
  return l(
    E(a, function(u) {
      return r.outEdges(u.v, u.w);
    })
  );
}
function x(r, t, n) {
  for (var a = [], u = t[t.length - 1], i = t[0], o; r.nodeCount(); ) {
    for (; o = i.dequeue(); )
      m(r, t, n, o);
    for (; o = u.dequeue(); )
      m(r, t, n, o);
    if (r.nodeCount()) {
      for (var e = t.length - 2; e > 0; --e)
        if (o = t[e].dequeue(), o) {
          a = a.concat(m(r, t, n, o, !0));
          break;
        }
    }
  }
  return a;
}
function m(r, t, n, a, u) {
  var i = u ? [] : void 0;
  return d(r.inEdges(a.v), function(o) {
    var e = r.edge(o), v = r.node(o.v);
    u && i.push({ v: o.v, w: o.w }), v.out -= e, h(t, n, v);
  }), d(r.outEdges(a.v), function(o) {
    var e = r.edge(o), v = o.w, f = r.node(v);
    f.in -= e, h(t, n, f);
  }), r.removeNode(a.v), i;
}
function F(r, t) {
  var n = new p(), a = 0, u = 0;
  d(r.nodes(), function(e) {
    n.setNode(e, { v: e, in: 0, out: 0 });
  }), d(r.edges(), function(e) {
    var v = n.edge(e.v, e.w) || 0, f = t(e), w = v + f;
    n.setEdge(e.v, e.w, w), u = Math.max(u, n.node(e.v).out += f), a = Math.max(a, n.node(e.w).in += f);
  });
  var i = c(u + a + 3).map(function() {
    return new s();
  }), o = a + 1;
  return d(n.nodes(), function(e) {
    h(i, o, n.node(e));
  }), { graph: n, buckets: i, zeroIdx: o };
}
function h(r, t, n) {
  n.out ? n.in ? r[n.out - n.in + t].enqueue(n) : r[r.length - 1].enqueue(n) : r[0].enqueue(n);
}
export {
  L as greedyFAS
};
