import { L as isFunction_default, N as constant_default, T as _baseRest_default, t as isArrayLikeObject_default } from "./isArrayLikeObject-LXbTYiBa.js";
import { r as _baseFlatten_default } from "./hasIn-DnfJcYpY.js";
import { t as isEmpty_default } from "./isEmpty-B7FX9wKt.js";
import { a as filter_default, b as keys_default, i as values_default, n as reduce_default, r as isUndefined_default, s as forEach_default, t as _baseUniq_default } from "./_baseUniq-C87CckHL.js";
var union_default = _baseRest_default(function(e) {
  return _baseUniq_default(_baseFlatten_default(e, 1, isArrayLikeObject_default, true));
}), DEFAULT_EDGE_NAME = "\0", GRAPH_NODE = "\0", EDGE_KEY_DELIM = "", Graph = class {
  constructor(e = {}) {
    this._isDirected = Object.prototype.hasOwnProperty.call(e, "directed") ? e.directed : true, this._isMultigraph = Object.prototype.hasOwnProperty.call(e, "multigraph") ? e.multigraph : false, this._isCompound = Object.prototype.hasOwnProperty.call(e, "compound") ? e.compound : false, this._label = void 0, this._defaultNodeLabelFn = constant_default(void 0), this._defaultEdgeLabelFn = constant_default(void 0), this._nodes = {}, this._isCompound && (this._parent = {}, this._children = {}, this._children[GRAPH_NODE] = {}), 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(_) {
    return isFunction_default(_) || (_ = constant_default(_)), this._defaultNodeLabelFn = _, this;
  }
  nodeCount() {
    return this._nodeCount;
  }
  nodes() {
    return keys_default(this._nodes);
  }
  sources() {
    var e = this;
    return filter_default(this.nodes(), function(g) {
      return isEmpty_default(e._in[g]);
    });
  }
  sinks() {
    var e = this;
    return filter_default(this.nodes(), function(g) {
      return isEmpty_default(e._out[g]);
    });
  }
  setNodes(e, g) {
    var _ = arguments, v = this;
    return forEach_default(e, function(e2) {
      _.length > 1 ? v.setNode(e2, g) : v.setNode(e2);
    }), this;
  }
  setNode(e, g) {
    return Object.prototype.hasOwnProperty.call(this._nodes, e) ? (arguments.length > 1 && (this._nodes[e] = g), this) : (this._nodes[e] = arguments.length > 1 ? g : this._defaultNodeLabelFn(e), this._isCompound && (this._parent[e] = GRAPH_NODE, this._children[e] = {}, this._children[GRAPH_NODE][e] = true), 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 g = (e2) => this.removeEdge(this._edgeObjs[e2]);
      delete this._nodes[e], this._isCompound && (this._removeFromParentsChildList(e), delete this._parent[e], forEach_default(this.children(e), (e2) => {
        this.setParent(e2);
      }), delete this._children[e]), forEach_default(keys_default(this._in[e]), g), delete this._in[e], delete this._preds[e], forEach_default(keys_default(this._out[e]), g), delete this._out[e], delete this._sucs[e], --this._nodeCount;
    }
    return this;
  }
  setParent(e, g) {
    if (!this._isCompound) throw Error("Cannot set parent in a non-compound graph");
    if (isUndefined_default(g)) g = GRAPH_NODE;
    else {
      g += "";
      for (var _ = g; !isUndefined_default(_); _ = this.parent(_)) if (_ === e) throw Error("Setting " + g + " as parent of " + e + " would create a cycle");
      this.setNode(g);
    }
    return this.setNode(e), this._removeFromParentsChildList(e), this._parent[e] = g, this._children[g][e] = true, this;
  }
  _removeFromParentsChildList(e) {
    delete this._children[this._parent[e]][e];
  }
  parent(e) {
    if (this._isCompound) {
      var g = this._parent[e];
      if (g !== GRAPH_NODE) return g;
    }
  }
  children(e) {
    if (isUndefined_default(e) && (e = GRAPH_NODE), this._isCompound) {
      var g = this._children[e];
      if (g) return keys_default(g);
    } else if (e === GRAPH_NODE) return this.nodes();
    else if (this.hasNode(e)) return [];
  }
  predecessors(e) {
    var g = this._preds[e];
    if (g) return keys_default(g);
  }
  successors(e) {
    var g = this._sucs[e];
    if (g) return keys_default(g);
  }
  neighbors(e) {
    var g = this.predecessors(e);
    if (g) return union_default(g, this.successors(e));
  }
  isLeaf(e) {
    return (this.isDirected() ? this.successors(e) : this.neighbors(e)).length === 0;
  }
  filterNodes(e) {
    var g = new this.constructor({
      directed: this._isDirected,
      multigraph: this._isMultigraph,
      compound: this._isCompound
    });
    g.setGraph(this.graph());
    var _ = this;
    forEach_default(this._nodes, function(_2, v2) {
      e(v2) && g.setNode(v2, _2);
    }), forEach_default(this._edgeObjs, function(e2) {
      g.hasNode(e2.v) && g.hasNode(e2.w) && g.setEdge(e2, _.edge(e2));
    });
    var v = {};
    function y(e2) {
      var b = _.parent(e2);
      return b === void 0 || g.hasNode(b) ? (v[e2] = b, b) : b in v ? v[b] : y(b);
    }
    return this._isCompound && forEach_default(g.nodes(), function(e2) {
      g.setParent(e2, y(e2));
    }), g;
  }
  setDefaultEdgeLabel(_) {
    return isFunction_default(_) || (_ = constant_default(_)), this._defaultEdgeLabelFn = _, this;
  }
  edgeCount() {
    return this._edgeCount;
  }
  edges() {
    return values_default(this._edgeObjs);
  }
  setPath(e, g) {
    var _ = this, v = arguments;
    return reduce_default(e, function(e2, y) {
      return v.length > 1 ? _.setEdge(e2, y, g) : _.setEdge(e2, y), y;
    }), this;
  }
  setEdge() {
    var e, g, _, v, y = false, b = arguments[0];
    typeof b == "object" && b && "v" in b ? (e = b.v, g = b.w, _ = b.name, arguments.length === 2 && (v = arguments[1], y = true)) : (e = b, g = arguments[1], _ = arguments[3], arguments.length > 2 && (v = arguments[2], y = true)), e = "" + e, g = "" + g, isUndefined_default(_) || (_ = "" + _);
    var x = edgeArgsToId(this._isDirected, e, g, _);
    if (Object.prototype.hasOwnProperty.call(this._edgeLabels, x)) return y && (this._edgeLabels[x] = v), this;
    if (!isUndefined_default(_) && !this._isMultigraph) throw Error("Cannot set a named edge when isMultigraph = false");
    this.setNode(e), this.setNode(g), this._edgeLabels[x] = y ? v : this._defaultEdgeLabelFn(e, g, _);
    var S = edgeArgsToObj(this._isDirected, e, g, _);
    return e = S.v, g = S.w, Object.freeze(S), this._edgeObjs[x] = S, incrementOrInitEntry(this._preds[g], e), incrementOrInitEntry(this._sucs[e], g), this._in[g][x] = S, this._out[e][x] = S, this._edgeCount++, this;
  }
  edge(e, g, _) {
    var v = arguments.length === 1 ? edgeObjToId(this._isDirected, arguments[0]) : edgeArgsToId(this._isDirected, e, g, _);
    return this._edgeLabels[v];
  }
  hasEdge(e, g, _) {
    var v = arguments.length === 1 ? edgeObjToId(this._isDirected, arguments[0]) : edgeArgsToId(this._isDirected, e, g, _);
    return Object.prototype.hasOwnProperty.call(this._edgeLabels, v);
  }
  removeEdge(e, g, _) {
    var v = arguments.length === 1 ? edgeObjToId(this._isDirected, arguments[0]) : edgeArgsToId(this._isDirected, e, g, _), y = this._edgeObjs[v];
    return y && (e = y.v, g = y.w, delete this._edgeLabels[v], delete this._edgeObjs[v], decrementOrRemoveEntry(this._preds[g], e), decrementOrRemoveEntry(this._sucs[e], g), delete this._in[g][v], delete this._out[e][v], this._edgeCount--), this;
  }
  inEdges(e, g) {
    var _ = this._in[e];
    if (_) {
      var v = values_default(_);
      return g ? filter_default(v, function(e2) {
        return e2.v === g;
      }) : v;
    }
  }
  outEdges(e, g) {
    var _ = this._out[e];
    if (_) {
      var v = values_default(_);
      return g ? filter_default(v, function(e2) {
        return e2.w === g;
      }) : v;
    }
  }
  nodeEdges(e, g) {
    var _ = this.inEdges(e, g);
    if (_) return _.concat(this.outEdges(e, g));
  }
};
Graph.prototype._nodeCount = 0, Graph.prototype._edgeCount = 0;
function incrementOrInitEntry(e, g) {
  e[g] ? e[g]++ : e[g] = 1;
}
function decrementOrRemoveEntry(e, g) {
  --e[g] || delete e[g];
}
function edgeArgsToId(e, g, _, v) {
  var y = "" + g, b = "" + _;
  if (!e && y > b) {
    var x = y;
    y = b, b = x;
  }
  return y + EDGE_KEY_DELIM + b + EDGE_KEY_DELIM + (isUndefined_default(v) ? DEFAULT_EDGE_NAME : v);
}
function edgeArgsToObj(e, g, _, v) {
  var y = "" + g, b = "" + _;
  if (!e && y > b) {
    var x = y;
    y = b, b = x;
  }
  var S = {
    v: y,
    w: b
  };
  return v && (S.name = v), S;
}
function edgeObjToId(e, g) {
  return edgeArgsToId(e, g.v, g.w, g.name);
}
export {
  Graph as t
};
