import { SWR as w } from "swrev";
import { onDestroy as D } from "svelte";
const v = () => {
};
function E(t) {
  for (var e = 0; e < t.length; e++)
    t[e]();
}
function q(t, e) {
  return t != t ? e == e : t !== e || t !== null && typeof t == "object" || typeof t == "function";
}
let m = !1;
function x(t) {
  var e = m;
  try {
    return m = !0, t();
  } finally {
    m = e;
  }
}
function K(t, e, n) {
  if (t == null)
    return e(void 0), n && n(void 0), v;
  const r = x(
    () => t.subscribe(
      e,
      // @ts-expect-error
      n
    )
  );
  return r.unsubscribe ? () => r.unsubscribe() : r;
}
const b = [];
function z(t, e) {
  return {
    subscribe: S(t, e).subscribe
  };
}
function S(t, e = v) {
  let n = null;
  const r = /* @__PURE__ */ new Set();
  function c(i) {
    if (q(t, i) && (t = i, n)) {
      const f = !b.length;
      for (const s of r)
        s[1](), b.push(s, t);
      if (f) {
        for (let s = 0; s < b.length; s += 2)
          b[s][0](b[s + 1]);
        b.length = 0;
      }
    }
  }
  function l(i) {
    c(i(
      /** @type {T} */
      t
    ));
  }
  function d(i, f = v) {
    const s = [i, f];
    return r.add(s), r.size === 1 && (n = e(c, l) || v), i(
      /** @type {T} */
      t
    ), () => {
      r.delete(s), r.size === 0 && n && (n(), n = null);
    };
  }
  return { set: c, update: l, subscribe: d };
}
function W(t, e, n) {
  const r = !Array.isArray(t), c = r ? [t] : t;
  if (!c.every(Boolean))
    throw new Error("derived() expects stores as input, got a falsy value");
  const l = e.length < 2;
  return z(n, (d, i) => {
    let f = !1;
    const s = [];
    let p = 0, g = v;
    const h = () => {
      if (p)
        return;
      g();
      const u = e(r ? s[0] : s, d, i);
      l ? d(u) : g = typeof u == "function" ? u : v;
    }, o = c.map(
      (u, y) => K(
        u,
        (R) => {
          s[y] = R, p &= ~(1 << y), f && h();
        },
        () => {
          p |= 1 << y;
        }
      )
    );
    return f = !0, h(), function() {
      E(o), g(), f = !1;
    };
  });
}
class A extends w {
  /**
   * Svelte specific use of SWR.
   */
  useSWR(e, n) {
    let r;
    const c = S(void 0, () => () => r == null ? void 0 : r()), l = S(void 0, () => () => r == null ? void 0 : r()), d = (o) => {
      l.set(void 0), c.set(o);
    }, i = (o) => l.set(o);
    r = this.subscribe(e, d, i, {
      loadInitialCache: !0,
      ...n
    }).unsubscribe, D(() => r == null ? void 0 : r());
    const f = (o, u) => this.mutate(this.resolveKey(e), o, {
      revalidateOptions: n,
      ...u
    }), s = (o) => this.revalidate(this.resolveKey(e), { ...n, ...o }), p = (o) => this.clear(this.resolveKey(e), o), g = W([c, l], ([o, u]) => o === void 0 && u === void 0), h = W([c, l], ([o, u]) => o !== void 0 && u === void 0);
    return { data: c, error: l, mutate: f, revalidate: s, clear: p, isLoading: g, isValid: h };
  }
}
const _ = (t) => new A(t);
let a = _();
const B = (t) => (a = _(t), a), C = (t, e) => a.subscribeData(t, e), I = (t, e) => a.subscribeErrors(t, e), L = (t) => a.get(t), V = (t) => a.getWait(t), k = (t, e, n, r) => a.subscribe(t, e, n, r), F = (t, e) => a.useSWR(t, e), G = (t, e, n) => a.mutate(t, e, n), H = (t, e) => a.revalidate(t, e), J = (t, e) => a.clear(t, e);
export {
  A as SSWR,
  J as clear,
  B as createDefaultSWR,
  _ as createSWR,
  L as get,
  V as getOrWait,
  G as mutate,
  H as revalidate,
  C as subscribe,
  I as subscribeErrors,
  a as swr,
  k as use,
  F as useSWR
};
