import { useRef as p, useReducer as y, useCallback as g, useEffect as v } from "../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { boolOrTrue as f } from "../utils/value/bool.js";
function F({
  fetchOptions: { keepPrevData: d, onSuccess: o, onError: u, enabled: c } = { keepPrevData: !0 },
  queryFn: n
}) {
  const t = p(!1), r = {
    error: void 0,
    data: void 0,
    isFetching: f(c),
    refetch: () => s()
  }, h = (e, i) => {
    switch (i.type) {
      case "loading":
        return { ...r, isFetching: !0, data: d ? e.data : void 0 };
      case "fetched":
        return { ...r, data: i.payload, isFetching: !1 };
      case "error":
        return { ...r, error: i.payload, isFetching: !1 };
      default:
        return e;
    }
  }, [l, a] = y(h, r), s = g(async () => {
    a({ type: "loading" });
    try {
      if (t.current) return;
      const e = await n();
      o?.(e), a({ type: "fetched", payload: e });
    } catch (e) {
      if (t.current) return;
      u?.(e), a({ type: "error", payload: e });
    }
  }, [a, n, o, u]);
  return v(() => (t.current = !1, f(c) && s(), () => {
    t.current = !0;
  }), [c, s]), l;
}
export {
  F as useFetch
};
