var E = /* @__PURE__ */ Symbol.for("preact-signals");
function l() {
  if (e > 1)
    e--;
  else {
    var t, i = !1;
    for ((function() {
      var s = a;
      for (a = void 0; s !== void 0; )
        s.S.v === s.v && (s.S.i = s.i), s = s.o;
    })(); u !== void 0; ) {
      var o = u;
      for (u = void 0, d++; o !== void 0; ) {
        var r = o.u;
        if (o.u = void 0, o.f &= -3, !(8 & o.f) && m(o)) try {
          o.c();
        } catch (s) {
          i || (t = s, i = !0);
        }
        o = r;
      }
    }
    if (d = 0, e--, i) throw t;
  }
}
function C(t) {
  if (e > 0) return t();
  p = ++U, e++;
  try {
    return t();
  } finally {
    l();
  }
}
var n = void 0;
function S(t) {
  var i = n;
  n = void 0;
  try {
    return t();
  } finally {
    n = i;
  }
}
var u = void 0, e = 0, d = 0, U = 0, p = 0, a = void 0, c = 0;
function w(t) {
  if (n !== void 0) {
    var i = t.n;
    if (i === void 0 || i.t !== n)
      return i = { i: 0, S: t, p: n.s, n: void 0, t: n, e: void 0, x: void 0, r: i }, n.s !== void 0 && (n.s.n = i), n.s = i, t.n = i, 32 & n.f && t.S(i), i;
    if (i.i === -1)
      return i.i = 0, i.n !== void 0 && (i.n.p = i.p, i.p !== void 0 && (i.p.n = i.n), i.p = n.s, i.n = void 0, n.s.n = i, n.s = i), i;
  }
}
function f(t, i) {
  this.v = t, this.i = 0, this.n = void 0, this.t = void 0, this.l = 0, this.W = i?.watched, this.Z = i?.unwatched, this.name = i?.name;
}
f.prototype.brand = E;
f.prototype.h = function() {
  return !0;
};
f.prototype.S = function(t) {
  var i = this, o = this.t;
  o !== t && t.e === void 0 && (t.x = o, this.t = t, o !== void 0 ? o.e = t : S(function() {
    var r;
    (r = i.W) == null || r.call(i);
  }));
};
f.prototype.U = function(t) {
  var i = this;
  if (this.t !== void 0) {
    var o = t.e, r = t.x;
    o !== void 0 && (o.x = r, t.e = void 0), r !== void 0 && (r.e = o, t.x = void 0), t === this.t && (this.t = r, r === void 0 && S(function() {
      var s;
      (s = i.Z) == null || s.call(i);
    }));
  }
};
f.prototype.subscribe = function(t) {
  var i = this;
  return O(function() {
    var o = i.value, r = n;
    n = void 0;
    try {
      t(o);
    } finally {
      n = r;
    }
  }, { name: "sub" });
};
f.prototype.valueOf = function() {
  return this.value;
};
f.prototype.toString = function() {
  return this.value + "";
};
f.prototype.toJSON = function() {
  return this.value;
};
f.prototype.peek = function() {
  var t = n;
  n = void 0;
  try {
    return this.value;
  } finally {
    n = t;
  }
};
Object.defineProperty(f.prototype, "value", { get: function() {
  var t = w(this);
  return t !== void 0 && (t.i = this.i), this.v;
}, set: function(t) {
  if (t !== this.v) {
    if (d > 100) throw new Error("Cycle detected");
    (function(o) {
      e !== 0 && d === 0 && o.l !== p && (o.l = p, a = { S: o, v: o.v, i: o.i, o: a });
    })(this), this.v = t, this.i++, c++, e++;
    try {
      for (var i = this.t; i !== void 0; i = i.x) i.t.N();
    } finally {
      l();
    }
  }
} });
function j(t, i) {
  return new f(t, i);
}
function m(t) {
  for (var i = t.s; i !== void 0; i = i.n) if (i.S.i !== i.i || !i.S.h() || i.S.i !== i.i) return !0;
  return !1;
}
function x(t) {
  for (var i = t.s; i !== void 0; i = i.n) {
    var o = i.S.n;
    if (o !== void 0 && (i.r = o), i.S.n = i, i.i = -1, i.n === void 0) {
      t.s = i;
      break;
    }
  }
}
function b(t) {
  for (var i = t.s, o = void 0; i !== void 0; ) {
    var r = i.p;
    i.i === -1 ? (i.S.U(i), r !== void 0 && (r.n = i.n), i.n !== void 0 && (i.n.p = r)) : o = i, i.S.n = i.r, i.r !== void 0 && (i.r = void 0), i = r;
  }
  t.s = o;
}
function h(t, i) {
  f.call(this, void 0), this.x = t, this.s = void 0, this.g = c - 1, this.f = 4, this.W = i?.watched, this.Z = i?.unwatched, this.name = i?.name;
}
h.prototype = new f();
h.prototype.h = function() {
  if (this.f &= -3, 1 & this.f) return !1;
  if ((36 & this.f) == 32 || (this.f &= -5, this.g === c)) return !0;
  if (this.g = c, this.f |= 1, this.i > 0 && !m(this))
    return this.f &= -2, !0;
  var t = n;
  try {
    x(this), n = this;
    var i = this.x();
    (16 & this.f || this.v !== i || this.i === 0) && (this.v = i, this.f &= -17, this.i++);
  } catch (o) {
    this.v = o, this.f |= 16, this.i++;
  }
  return n = t, b(this), this.f &= -2, !0;
};
h.prototype.S = function(t) {
  if (this.t === void 0) {
    this.f |= 36;
    for (var i = this.s; i !== void 0; i = i.n) i.S.S(i);
  }
  f.prototype.S.call(this, t);
};
h.prototype.U = function(t) {
  if (this.t !== void 0 && (f.prototype.U.call(this, t), this.t === void 0)) {
    this.f &= -33;
    for (var i = this.s; i !== void 0; i = i.n) i.S.U(i);
  }
};
h.prototype.N = function() {
  if (!(2 & this.f)) {
    this.f |= 6;
    for (var t = this.t; t !== void 0; t = t.x) t.t.N();
  }
};
Object.defineProperty(h.prototype, "value", { get: function() {
  if (1 & this.f) throw new Error("Cycle detected");
  var t = w(this);
  if (this.h(), t !== void 0 && (t.i = this.i), 16 & this.f) throw this.v;
  return this.v;
} });
function k(t, i) {
  return new h(t, i);
}
function g(t) {
  var i = t.m;
  if (t.m = void 0, typeof i == "function") {
    e++;
    var o = n;
    n = void 0;
    try {
      i();
    } catch (r) {
      throw t.f &= -2, t.f |= 8, y(t), r;
    } finally {
      n = o, l();
    }
  }
}
function y(t) {
  for (var i = t.s; i !== void 0; i = i.n) i.S.U(i);
  t.x = void 0, t.s = void 0, g(t);
}
function N(t) {
  if (n !== this) throw new Error("Out-of-order effect");
  b(this), n = t, this.f &= -2, 8 & this.f && y(this), l();
}
function v(t, i) {
  this.x = t, this.m = void 0, this.s = void 0, this.u = void 0, this.f = 32, this.name = i?.name;
}
v.prototype.c = function() {
  var t = this.S();
  try {
    if (8 & this.f || this.x === void 0) return;
    var i = this.x();
    typeof i == "function" && (this.m = i);
  } finally {
    t();
  }
};
v.prototype.S = function() {
  if (1 & this.f) throw new Error("Cycle detected");
  this.f |= 1, this.f &= -9, g(this), x(this), e++;
  var t = n;
  return n = this, N.bind(this, t);
};
v.prototype.N = function() {
  2 & this.f || (this.f |= 2, this.u = u, u = this);
};
v.prototype.d = function() {
  this.f |= 8, 1 & this.f || y(this);
};
v.prototype.dispose = function() {
  this.d();
};
function O(t, i) {
  var o = new v(t, i);
  try {
    o.c();
  } catch (s) {
    throw o.d(), s;
  }
  var r = o.d.bind(o);
  return r[Symbol.dispose] = r, r;
}
export {
  h as Computed,
  v as Effect,
  f as Signal,
  C as batch,
  k as computed,
  O as effect,
  j as signal,
  S as untracked
};
