'use client'

import {
  QueryCache,
  QueryClient,
  QueryClientProvider as TanstackQueryProvider
} from '@tanstack/react-query'
import type { ReactNode } from 'react'
import { toast } from 'sonner'

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: 5 * 60 * 1000,
    },
  },
  queryCache: new QueryCache({
    onError: (error) => {
      toast.error(error.message)
    },
  })
})

export const QueryClientProvider =
  ({ children }: { children: ReactNode }) => (
    <TanstackQueryProvider client={queryClient}>
      {children}
    </TanstackQueryProvider>
  )
