import De, { useRef as S, useMemo as Ve, useCallback as G, useDebugValue as we } from "react";
import { s as Ce } from "../../../../../../../_virtual/index8.js";
import { withArgs as Ie, subscribeCallback as Le } from "../_internal/index.js";
import { useSWRConfig as Ke } from "../_internal/index.js";
import { b as Oe, s as Ae, z as _e, e as s, U as B, B as ke, o as Ne, a as ce, n as be, u as Y, r as le, t as Fe, I as fe, m as xe } from "../_internal/config-context-client-v7VOFo66.js";
import { j as Xe } from "../_internal/config-context-client-v7VOFo66.js";
import { ERROR_REVALIDATE_EVENT as de, FOCUS_EVENT as ye, RECONNECT_EVENT as Me, MUTATE_EVENT as Pe } from "../_internal/events.js";
const J = De.use || // This extra generic is to avoid TypeScript mixing up the generic and JSX sytax
// and emitting an error.
// We assume that this is only for the `use(thenable)` case, not `use(context)`.
// https://github.com/facebook/react/blob/aed00dacfb79d17c53218404c52b1c7aa59c4a89/packages/react-server/src/ReactFizzThenable.js#L45
((c) => {
  switch (c.status) {
    case "pending":
      throw c;
    case "fulfilled":
      return c.value;
    case "rejected":
      throw c.reason;
    default:
      throw c.status = "pending", c.then((g) => {
        c.status = "fulfilled", c.value = g;
      }, (g) => {
        c.status = "rejected", c.reason = g;
      }), c;
  }
}), K = {
  dedupe: !0
}, Ue = (c, g, f) => {
  const { cache: v, compare: F, suspense: x, fallbackData: Q, revalidateOnMount: L, revalidateIfStale: y, refreshInterval: O, refreshWhenHidden: X, refreshWhenOffline: Z, keepPreviousData: ge } = f, [A, Re, E, me] = Oe.get(v), [e, $] = Ae(c), M = S(!1), w = S(!1), _ = S(e), P = S(g), U = S(f), n = () => U.current, q = () => n().isVisible() && n().isOnline(), [p, W, Ee, pe] = _e(v, e), C = S({}).current, k = s(Q) ? s(f.fallback) ? B : f.fallback[e] : Q, ee = (r, t) => {
    for (const o in C) {
      const a = o;
      if (a === "data") {
        if (!F(r[a], t[a]) && (!s(r[a]) || !F(b, t[a])))
          return !1;
      } else if (t[a] !== r[a])
        return !1;
    }
    return !0;
  }, te = Ve(() => {
    const r = !e || !g ? !1 : s(L) ? n().isPaused() || x ? !1 : y !== !1 : L, t = (u) => {
      const R = xe(u);
      return delete R._k, r ? {
        isValidating: !0,
        isLoading: !0,
        ...R
      } : R;
    }, o = p(), a = pe(), d = t(o), V = o === a ? d : t(a);
    let i = d;
    return [
      () => {
        const u = t(p());
        return ee(u, i) ? (i.data = u.data, i.isLoading = u.isLoading, i.isValidating = u.isValidating, i.error = u.error, i) : (i = u, u);
      },
      () => V
    ];
  }, [
    v,
    e
  ]), T = Ce.useSyncExternalStore(G(
    (r) => Ee(e, (t, o) => {
      ee(o, t) || r();
    }),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [
      v,
      e
    ]
  ), te[0], te[1]), re = !M.current, he = A[e] && A[e].length > 0, D = T.data, h = s(D) ? k && ke(k) ? J(k) : k : D, N = T.error, H = S(h), b = ge ? s(D) ? s(H.current) ? h : H.current : D : h, se = he && !s(N) ? !1 : re && !s(L) ? L : n().isPaused() ? !1 : x ? s(h) ? !1 : y : s(h) || y, ae = !!(e && g && re && se), Se = s(T.isValidating) ? ae : T.isValidating, ve = s(T.isLoading) ? ae : T.isLoading, I = G(
    async (r) => {
      const t = P.current;
      if (!e || !t || w.current || n().isPaused())
        return !1;
      let o, a, d = !0;
      const V = r || {}, i = !E[e] || !V.dedupe, u = () => fe ? !w.current && e === _.current && M.current : e === _.current, R = {
        isValidating: !1,
        isLoading: !1
      }, ie = () => {
        W(R);
      }, oe = () => {
        const l = E[e];
        l && l[1] === a && delete E[e];
      }, ue = {
        isValidating: !0
      };
      s(p().data) && (ue.isLoading = !0);
      try {
        if (i && (W(ue), f.loadingTimeout && s(p().data) && setTimeout(() => {
          d && u() && n().onLoadingSlow(e, f);
        }, f.loadingTimeout), E[e] = [
          t($),
          Ne()
        ]), [o, a] = E[e], o = await o, i && setTimeout(oe, f.dedupingInterval), !E[e] || E[e][1] !== a)
          return i && u() && n().onDiscarded(e), !1;
        R.error = B;
        const l = Re[e];
        if (!s(l) && // case 1
        (a <= l[0] || // case 2
        a <= l[1] || // case 3
        l[1] === 0))
          return ie(), i && u() && n().onDiscarded(e), !1;
        const m = p().data;
        R.data = F(m, o) ? m : o, i && u() && n().onSuccess(o, e, f);
      } catch (l) {
        oe();
        const m = n(), { shouldRetryOnError: j } = m;
        m.isPaused() || (R.error = l, i && u() && (m.onError(l, e, m), (j === !0 || ce(j) && j(l)) && (!n().revalidateOnFocus || !n().revalidateOnReconnect || q()) && m.onErrorRetry(l, e, m, (Te) => {
          const z = A[e];
          z && z[0] && z[0](de, Te);
        }, {
          retryCount: (V.retryCount || 0) + 1,
          dedupe: !0
        })));
      }
      return d = !1, ie(), !0;
    },
    // `setState` is immutable, and `eventsCallback`, `fnArg`, and
    // `keyValidating` are depending on `key`, so we can exclude them from
    // the deps array.
    //
    // FIXME:
    // `fn` and `config` might be changed during the lifecycle,
    // but they might be changed every render like this.
    // `useSWR('key', () => fetch('/api/'), { suspense: true })`
    // So we omit the values from the deps array
    // even though it might cause unexpected behaviors.
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [
      e,
      v
    ]
  ), ne = G(
    // Use callback to make sure `keyRef.current` returns latest result every time
    (...r) => be(v, _.current, ...r),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    []
  );
  if (Y(() => {
    P.current = g, U.current = f, s(D) || (H.current = D);
  }), Y(() => {
    if (!e) return;
    const r = I.bind(B, K);
    let t = 0;
    n().revalidateOnFocus && (t = Date.now() + n().focusThrottleInterval);
    const a = Le(e, A, (d, V = {}) => {
      if (d == ye) {
        const i = Date.now();
        n().revalidateOnFocus && i > t && q() && (t = i + n().focusThrottleInterval, r());
      } else if (d == Me)
        n().revalidateOnReconnect && q() && r();
      else {
        if (d == Pe)
          return I();
        if (d == de)
          return I(V);
      }
    });
    return w.current = !1, _.current = e, M.current = !0, W({
      _k: $
    }), se && (s(h) || le ? r() : Fe(r)), () => {
      w.current = !0, a();
    };
  }, [
    e
  ]), Y(() => {
    let r;
    function t() {
      const a = ce(O) ? O(p().data) : O;
      a && r !== -1 && (r = setTimeout(o, a));
    }
    function o() {
      !p().error && (X || n().isVisible()) && (Z || n().isOnline()) ? I(K).then(t) : t();
    }
    return t(), () => {
      r && (clearTimeout(r), r = -1);
    };
  }, [
    O,
    X,
    Z,
    e
  ]), we(b), x && s(h) && e) {
    if (!fe && le)
      throw new Error("Fallback data is required when using Suspense in SSR.");
    P.current = g, U.current = f, w.current = !1;
    const r = me[e];
    if (!s(r)) {
      const t = ne(r);
      J(t);
    }
    if (s(N)) {
      const t = I(K);
      s(b) || (t.status = "fulfilled", t.value = !0), J(t);
    } else
      throw N;
  }
  return {
    mutate: ne,
    get data() {
      return C.data = !0, b;
    },
    get error() {
      return C.error = !0, N;
    },
    get isValidating() {
      return C.isValidating = !0, Se;
    },
    get isLoading() {
      return C.isLoading = !0, ve;
    }
  };
}, Be = Ie(Ue);
export {
  Be as default,
  Xe as mutate,
  Ke as useSWRConfig
};
//# sourceMappingURL=index.js.map
