import { environmentManager as m } from "../../../../../../@tanstack_query-core@5.95.2/node_modules/@tanstack/query-core/build/modern/environmentManager.js";
import { notifyManager as h } from "../../../../../../@tanstack_query-core@5.95.2/node_modules/@tanstack/query-core/build/modern/notifyManager.js";
import { noop as c } from "../../../../../../@tanstack_query-core@5.95.2/node_modules/@tanstack/query-core/build/modern/utils.js";
import { useState as d, useCallback as g, useEffect as q } from "../../../../../../preact@10.29.0/node_modules/preact/hooks/dist/hooks.module.js";
import { useIsRestoring as w } from "./IsRestoringProvider.js";
import { useQueryClient as b } from "./QueryClientProvider.js";
import { useQueryErrorResetBoundary as E } from "./QueryErrorResetBoundary.js";
import { ensurePreventErrorBoundaryRetry as R, useClearResetErrorBoundary as O, getHasError as C } from "./errorBoundaryUtils.js";
import { ensureSuspenseTimers as Q, shouldSuspend as v, fetchOptimistic as l, willFetch as S } from "./suspense.js";
import { useSyncExternalStore as _ } from "./utils.js";
function I(n, p, k) {
  if (process.env.NODE_ENV !== "production" && (typeof n != "object" || Array.isArray(n)))
    throw new Error(
      'Bad argument type. Starting with v5, only the "Object" form is allowed when calling query related functions. Please use the error stack to find the culprit call. More info here: https://tanstack.com/query/latest/docs/react/guides/migrating-to-v5#supports-a-single-signature-one-object'
    );
  const u = w(), o = E(), t = b(), e = t.defaultQueryOptions(n);
  t.getDefaultOptions().queries?._experimental_beforeQuery?.(
    e
  ), process.env.NODE_ENV !== "production" && (e.queryFn || console.error(
    `[${e.queryHash}]: No queryFn was passed as an option, and no default queryFn was found. The queryFn parameter is only optional when using a default queryFn. More info here: https://tanstack.com/query/latest/docs/framework/react/guides/default-query-function`
  )), e._optimisticResults = u ? "isRestoring" : "optimistic", Q(e), R(e, o), O(o);
  const f = !t.getQueryCache().get(e.queryHash), [r] = d(
    () => new p(
      t,
      e
    )
  ), s = r.getOptimisticResult(e), i = !u && n.subscribed !== !1;
  if (_(
    g(
      (a) => {
        const y = i ? r.subscribe(h.batchCalls(a)) : c;
        return r.updateResult(), y;
      },
      [r, i]
    ),
    () => r.getCurrentResult()
  ), q(() => {
    r.setOptions(e);
  }, [e, r]), v(e, s))
    throw l(e, r, o);
  if (C({
    result: s,
    errorResetBoundary: o,
    throwOnError: e.throwOnError,
    query: t.getQueryCache().get(e.queryHash),
    suspense: e.suspense
  }))
    throw s.error;
  return t.getDefaultOptions().queries?._experimental_afterQuery?.(
    e,
    s
  ), e.experimental_prefetchInRender && !m.isServer() && S(s, u) && (f ? (
    // Fetch immediately on render in order to ensure `.promise` is resolved even if the component is unmounted
    l(e, r, o)
  ) : (
    // subscribe to the "cache promise" so that we can finalize the currentThenable once data comes in
    t.getQueryCache().get(e.queryHash)?.promise
  ))?.catch(c).finally(() => {
    r.updateResult();
  }), e.notifyOnChangeProps ? s : r.trackResult(s);
}
export {
  I as useBaseQuery
};
