"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { useState, useRef, useCallback, useEffect } from "react";
import { QueryClient, QueryClientProvider, MutationCache } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { useTranslations } from "next-intl";
import { toast } from "sonner";
import { ApiError } from "../lib/api/api-error.js";
function QueryProvider({ children }) {
  const t = useTranslations("permissions");
  const tRef = useRef(t);
  useEffect(() => {
    tRef.current = t;
  }, [t]);
  const handlePermissionDenied = useCallback(() => {
    toast.info(tRef.current("roleChanged"), {
      description: tRef.current("roleChangedDescription")
    });
    setTimeout(() => {
      window.location.reload();
    }, 1500);
  }, []);
  const [queryClient] = useState(
    () => new QueryClient({
      defaultOptions: {
        queries: {
          staleTime: 60 * 1e3,
          refetchOnWindowFocus: false
        }
      },
      mutationCache: new MutationCache({
        onError: (error) => {
          if (error instanceof ApiError && error.isPermissionDenied()) {
            handlePermissionDenied();
          }
        }
      })
    })
  );
  return /* @__PURE__ */ jsxs(QueryClientProvider, { client: queryClient, children: [
    children,
    process.env.NEXT_PUBLIC_RQ_DEVTOOLS === "true" && /* @__PURE__ */ jsx(ReactQueryDevtools, { initialIsOpen: false })
  ] });
}
export {
  QueryProvider
};
