import { __values as d, __spreadArray as m, __read as b } from "./index-tslib.es6.js";
import { isFunction as a } from "./index-isFunction.js";
import { UnsubscriptionError as l } from "./index-UnsubscriptionError.js";
import { arrRemove as P } from "./index-arrRemove.js";
var x = (function() {
  function t(r) {
    this.initialTeardown = r, this.closed = !1, this._parentage = null, this._finalizers = null;
  }
  return t.prototype.unsubscribe = function() {
    var r, i, h, y, s;
    if (!this.closed) {
      this.closed = !0;
      var c = this._parentage;
      if (c)
        if (this._parentage = null, Array.isArray(c))
          try {
            for (var f = d(c), o = f.next(); !o.done; o = f.next()) {
              var T = o.value;
              T.remove(this);
            }
          } catch (e) {
            r = { error: e };
          } finally {
            try {
              o && !o.done && (i = f.return) && i.call(f);
            } finally {
              if (r) throw r.error;
            }
          }
        else
          c.remove(this);
      var v = this.initialTeardown;
      if (a(v))
        try {
          v();
        } catch (e) {
          s = e instanceof l ? e.errors : [e];
        }
      var p = this._finalizers;
      if (p) {
        this._finalizers = null;
        try {
          for (var u = d(p), n = u.next(); !n.done; n = u.next()) {
            var S = n.value;
            try {
              A(S);
            } catch (e) {
              s = s ?? [], e instanceof l ? s = m(m([], b(s)), b(e.errors)) : s.push(e);
            }
          }
        } catch (e) {
          h = { error: e };
        } finally {
          try {
            n && !n.done && (y = u.return) && y.call(u);
          } finally {
            if (h) throw h.error;
          }
        }
      }
      if (s)
        throw new l(s);
    }
  }, t.prototype.add = function(r) {
    var i;
    if (r && r !== this)
      if (this.closed)
        A(r);
      else {
        if (r instanceof t) {
          if (r.closed || r._hasParent(this))
            return;
          r._addParent(this);
        }
        (this._finalizers = (i = this._finalizers) !== null && i !== void 0 ? i : []).push(r);
      }
  }, t.prototype._hasParent = function(r) {
    var i = this._parentage;
    return i === r || Array.isArray(i) && i.includes(r);
  }, t.prototype._addParent = function(r) {
    var i = this._parentage;
    this._parentage = Array.isArray(i) ? (i.push(r), i) : i ? [i, r] : r;
  }, t.prototype._removeParent = function(r) {
    var i = this._parentage;
    i === r ? this._parentage = null : Array.isArray(i) && P(i, r);
  }, t.prototype.remove = function(r) {
    var i = this._finalizers;
    i && P(i, r), r instanceof t && r._removeParent(this);
  }, t.EMPTY = (function() {
    var r = new t();
    return r.closed = !0, r;
  })(), t;
})(), R = x.EMPTY;
function U(t) {
  return t instanceof x || t && "closed" in t && a(t.remove) && a(t.add) && a(t.unsubscribe);
}
function A(t) {
  a(t) ? t() : t.unsubscribe();
}
export {
  R as EMPTY_SUBSCRIPTION,
  x as Subscription,
  U as isSubscription
};
