/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import "./index.css";

import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import App from "./App";
import { AuthProvider } from "./core/providers/context/AuthProvider";
import { MessageProvider } from "./shared/components/MessageProvider";

if (import.meta.env.DEV) {
    const reactScanScript = document.createElement("script");
    reactScanScript.src = "//unpkg.com/react-scan/dist/auto.global.js";
    reactScanScript.crossOrigin = "anonymous";
    document.head.appendChild(reactScanScript);
}

const queryClient = new QueryClient({
    defaultOptions: {
        queries: {
            retry: 1,
            retryDelay: 1000,
            // refetchOnWindowFocus: true,
            refetchOnReconnect: true,
            refetchOnMount: true,
            throwOnError: (error, query) => {
                console.error(`Query hatası oluştu (${query.queryKey}):`, error);
                return false; // false döndürerek hatayı try catch ile yakalamış oluyoruz, UI patlamıyor böylece
            },
        },
        mutations: {
            onError: (error: any) => {
                console.error("Mutation Error:", error);
            },
            retry: false,
        },
    },
});

createRoot(document.getElementById("root")!).render(
    <StrictMode>
        <MessageProvider>
            <QueryClientProvider client={queryClient}>
                <AuthProvider>
                    <App />
                    {import.meta.env.DEV && <ReactQueryDevtools initialIsOpen={false} />}
                </AuthProvider>
            </QueryClientProvider>
        </MessageProvider>
    </StrictMode>,
);
