"use client";
import { jsx } from "react/jsx-runtime";
import * as React from "react";
import { MutationCache, QueryCache, QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { redirectToSessionRefresh, redirectToForbiddenLogin, UnauthorizedError, ForbiddenError, apiFetch, setAuthRedirectConfig } from "../backend/utils/api.js";
function ensureGlobalFetchInterception() {
  if (typeof window === "undefined") return;
  const w = window;
  if (w.__omFetchPatched) return;
  w.__omFetchPatched = true;
  w.__omOriginalFetch = window.fetch;
  window.fetch = ((input, init) => apiFetch(input, init));
}
const client = new QueryClient({
  queryCache: new QueryCache({
    onError: (error) => {
      if (error instanceof UnauthorizedError) redirectToSessionRefresh();
      else if (error instanceof ForbiddenError) redirectToForbiddenLogin();
      else if (error?.status === 401) redirectToSessionRefresh();
      else if (error?.status === 403) redirectToForbiddenLogin();
    }
  }),
  mutationCache: new MutationCache({
    onError: (error) => {
      if (error instanceof UnauthorizedError) redirectToSessionRefresh();
      else if (error instanceof ForbiddenError) redirectToForbiddenLogin();
      else if (error?.status === 401) redirectToSessionRefresh();
      else if (error?.status === 403) redirectToForbiddenLogin();
    }
  })
});
function QueryProvider({ children, defaultForbiddenRoles }) {
  React.useEffect(() => {
    ensureGlobalFetchInterception();
    if (defaultForbiddenRoles && defaultForbiddenRoles.length) {
      setAuthRedirectConfig({ defaultForbiddenRoles });
    }
  }, []);
  return /* @__PURE__ */ jsx(QueryClientProvider, { client, children });
}
export {
  QueryProvider
};
//# sourceMappingURL=QueryProvider.js.map
