import { E as v, P as c, R as m, t as L } from "./isArrayLikeObject.js";
import { E as u, a as g, i as f, n as N, r as _, s as d, t as j, x as P } from "./_baseUniq.js";
import { t as b } from "./isEmpty.js";
var D = v(function(e) {
  return j(P(e, 1, L, !0));
}), w = "\0", a = "\0", E = "", C = class {
  constructor(e = {}) {
    this._isDirected = Object.prototype.hasOwnProperty.call(e, "directed") ? e.directed : !0, this._isMultigraph = Object.prototype.hasOwnProperty.call(e, "multigraph") ? e.multigraph : !1, this._isCompound = Object.prototype.hasOwnProperty.call(e, "compound") ? e.compound : !1, this._label = void 0, this._defaultNodeLabelFn = c(void 0), this._defaultEdgeLabelFn = c(void 0), this._nodes = {}, this._isCompound && (this._parent = {}, this._children = {}, this._children[a] = {}), this._in = {}, this._preds = {}, this._out = {}, this._sucs = {}, this._edgeObjs = {}, this._edgeLabels = {};
  }
  isDirected() {
    return this._isDirected;
  }
  isMultigraph() {
    return this._isMultigraph;
  }
  isCompound() {
    return this._isCompound;
  }
  setGraph(e) {
    return this._label = e, this;
  }
  graph() {
    return this._label;
  }
  setDefaultNodeLabel(e) {
    return m(e) || (e = c(e)), this._defaultNodeLabelFn = e, this;
  }
  nodeCount() {
    return this._nodeCount;
  }
  nodes() {
    return u(this._nodes);
  }
  sources() {
    var e = this;
    return g(this.nodes(), function(t) {
      return b(e._in[t]);
    });
  }
  sinks() {
    var e = this;
    return g(this.nodes(), function(t) {
      return b(e._out[t]);
    });
  }
  setNodes(e, t) {
    var s = arguments, i = this;
    return d(e, function(n) {
      s.length > 1 ? i.setNode(n, t) : i.setNode(n);
    }), this;
  }
  setNode(e, t) {
    return Object.prototype.hasOwnProperty.call(this._nodes, e) ? (arguments.length > 1 && (this._nodes[e] = t), this) : (this._nodes[e] = arguments.length > 1 ? t : this._defaultNodeLabelFn(e), this._isCompound && (this._parent[e] = a, this._children[e] = {}, this._children[a][e] = !0), this._in[e] = {}, this._preds[e] = {}, this._out[e] = {}, this._sucs[e] = {}, ++this._nodeCount, this);
  }
  node(e) {
    return this._nodes[e];
  }
  hasNode(e) {
    return Object.prototype.hasOwnProperty.call(this._nodes, e);
  }
  removeNode(e) {
    if (Object.prototype.hasOwnProperty.call(this._nodes, e)) {
      var t = (s) => this.removeEdge(this._edgeObjs[s]);
      delete this._nodes[e], this._isCompound && (this._removeFromParentsChildList(e), delete this._parent[e], d(this.children(e), (s) => {
        this.setParent(s);
      }), delete this._children[e]), d(u(this._in[e]), t), delete this._in[e], delete this._preds[e], d(u(this._out[e]), t), delete this._out[e], delete this._sucs[e], --this._nodeCount;
    }
    return this;
  }
  setParent(e, t) {
    if (!this._isCompound) throw new Error("Cannot set parent in a non-compound graph");
    if (_(t)) t = a;
    else {
      t += "";
      for (var s = t; !_(s); s = this.parent(s)) if (s === e) throw new Error("Setting " + t + " as parent of " + e + " would create a cycle");
      this.setNode(t);
    }
    return this.setNode(e), this._removeFromParentsChildList(e), this._parent[e] = t, this._children[t][e] = !0, this;
  }
  _removeFromParentsChildList(e) {
    delete this._children[this._parent[e]][e];
  }
  parent(e) {
    if (this._isCompound) {
      var t = this._parent[e];
      if (t !== a) return t;
    }
  }
  children(e) {
    if (_(e) && (e = a), this._isCompound) {
      var t = this._children[e];
      if (t) return u(t);
    } else {
      if (e === a) return this.nodes();
      if (this.hasNode(e)) return [];
    }
  }
  predecessors(e) {
    var t = this._preds[e];
    if (t) return u(t);
  }
  successors(e) {
    var t = this._sucs[e];
    if (t) return u(t);
  }
  neighbors(e) {
    var t = this.predecessors(e);
    if (t) return D(t, this.successors(e));
  }
  isLeaf(e) {
    var t;
    return this.isDirected() ? t = this.successors(e) : t = this.neighbors(e), t.length === 0;
  }
  filterNodes(e) {
    var t = new this.constructor({
      directed: this._isDirected,
      multigraph: this._isMultigraph,
      compound: this._isCompound
    });
    t.setGraph(this.graph());
    var s = this;
    d(this._nodes, function(r, h) {
      e(h) && t.setNode(h, r);
    }), d(this._edgeObjs, function(r) {
      t.hasNode(r.v) && t.hasNode(r.w) && t.setEdge(r, s.edge(r));
    });
    var i = {};
    function n(r) {
      var h = s.parent(r);
      return h === void 0 || t.hasNode(h) ? (i[r] = h, h) : h in i ? i[h] : n(h);
    }
    return this._isCompound && d(t.nodes(), function(r) {
      t.setParent(r, n(r));
    }), t;
  }
  setDefaultEdgeLabel(e) {
    return m(e) || (e = c(e)), this._defaultEdgeLabelFn = e, this;
  }
  edgeCount() {
    return this._edgeCount;
  }
  edges() {
    return f(this._edgeObjs);
  }
  setPath(e, t) {
    var s = this, i = arguments;
    return N(e, function(n, r) {
      return i.length > 1 ? s.setEdge(n, r, t) : s.setEdge(n, r), r;
    }), this;
  }
  setEdge() {
    var e, t, s, i, n = !1, r = arguments[0];
    typeof r == "object" && r !== null && "v" in r ? (e = r.v, t = r.w, s = r.name, arguments.length === 2 && (i = arguments[1], n = !0)) : (e = r, t = arguments[1], s = arguments[3], arguments.length > 2 && (i = arguments[2], n = !0)), e = "" + e, t = "" + t, _(s) || (s = "" + s);
    var h = l(this._isDirected, e, t, s);
    if (Object.prototype.hasOwnProperty.call(this._edgeLabels, h))
      return n && (this._edgeLabels[h] = i), this;
    if (!_(s) && !this._isMultigraph) throw new Error("Cannot set a named edge when isMultigraph = false");
    this.setNode(e), this.setNode(t), this._edgeLabels[h] = n ? i : this._defaultEdgeLabelFn(e, t, s);
    var o = F(this._isDirected, e, t, s);
    return e = o.v, t = o.w, Object.freeze(o), this._edgeObjs[h] = o, O(this._preds[t], e), O(this._sucs[e], t), this._in[t][h] = o, this._out[e][h] = o, this._edgeCount++, this;
  }
  edge(e, t, s) {
    var i = arguments.length === 1 ? p(this._isDirected, arguments[0]) : l(this._isDirected, e, t, s);
    return this._edgeLabels[i];
  }
  hasEdge(e, t, s) {
    var i = arguments.length === 1 ? p(this._isDirected, arguments[0]) : l(this._isDirected, e, t, s);
    return Object.prototype.hasOwnProperty.call(this._edgeLabels, i);
  }
  removeEdge(e, t, s) {
    var i = arguments.length === 1 ? p(this._isDirected, arguments[0]) : l(this._isDirected, e, t, s), n = this._edgeObjs[i];
    return n && (e = n.v, t = n.w, delete this._edgeLabels[i], delete this._edgeObjs[i], y(this._preds[t], e), y(this._sucs[e], t), delete this._in[t][i], delete this._out[e][i], this._edgeCount--), this;
  }
  inEdges(e, t) {
    var s = this._in[e];
    if (s) {
      var i = f(s);
      return t ? g(i, function(n) {
        return n.v === t;
      }) : i;
    }
  }
  outEdges(e, t) {
    var s = this._out[e];
    if (s) {
      var i = f(s);
      return t ? g(i, function(n) {
        return n.w === t;
      }) : i;
    }
  }
  nodeEdges(e, t) {
    var s = this.inEdges(e, t);
    if (s) return s.concat(this.outEdges(e, t));
  }
};
C.prototype._nodeCount = 0;
C.prototype._edgeCount = 0;
function O(e, t) {
  e[t] ? e[t]++ : e[t] = 1;
}
function y(e, t) {
  --e[t] || delete e[t];
}
function l(e, t, s, i) {
  var n = "" + t, r = "" + s;
  if (!e && n > r) {
    var h = n;
    n = r, r = h;
  }
  return n + E + r + E + (_(i) ? w : i);
}
function F(e, t, s, i) {
  var n = "" + t, r = "" + s;
  if (!e && n > r) {
    var h = n;
    n = r, r = h;
  }
  var o = {
    v: n,
    w: r
  };
  return i && (o.name = i), o;
}
function p(e, t) {
  return l(e, t.v, t.w, t.name);
}
export {
  C as t
};

//# sourceMappingURL=graph.js.map