import { useState as y, useRef as g, useCallback as S, useEffect as P, useMemo as _ } from "../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { isNumber as b, isFunction as k } from "../../utils/value/is.js";
import { EMPTY_OBJECT as I } from "../../utils/value/constants.js";
import { ALREADY_RESOLVED_PROMISE as x } from "../../utils/async/constants.js";
import { tryResolve as u } from "../../utils/async/main.js";
const a = (s) => {
  setTimeout(() => {
    throw s;
  }, 0);
};
function V({
  queryFn: s,
  options: A
}) {
  const { retry: i = !1, retryDelay: o = 1e3, onSuccess: l, onError: f, onSettled: r, shouldRetry: m } = A || I, [p, M] = y(null), [w, d] = y(null), [e, c] = y("idle"), h = g(!0), n = g(0), T = S(() => {
    M(null), d(null), c("idle"), n.current = 0;
  }, []), E = S(() => {
    n.current = 0;
  }, []), R = S(
    async (...C) => {
      try {
        c("loading"), d(null);
        const t = await s?.(...C);
        return h.current && (M(t), c("success")), x.then(() => {
          l && u(l, t).catch(a), r && u(r, t, null).catch(a), E();
        }), t;
      } catch (t) {
        let D = 0;
        if (b(i) && (!m || m(t)) && (D = Math.max(0, Math.floor(i))), n.current++ < D) {
          const L = k(o) ? o(n.current) : o ?? 1e3;
          return await new Promise((O) => setTimeout(O, L)), R(...C);
        }
        throw h.current && (d(t), c("error")), x.then(() => {
          f && u(f, t).catch(a), r && u(r, void 0, t).catch(a), E();
        }), t;
      }
    },
    [s, l, r, i, m, o, E, f]
  );
  return P(() => () => {
    h.current = !1;
  }, []), _(
    () => ({
      data: p,
      error: w,
      status: e,
      isIdle: e === "idle",
      isLoading: e === "loading",
      isSuccess: e === "success",
      isError: e === "error",
      mutate: R,
      reset: T
    }),
    [p, w, e, R, T]
  );
}
export {
  V as default
};
