"use client";

import { HeroUIProvider } from "@heroui/react";
import { AppProvider } from "@/contexts/AppContext";
import { WagmiProvider } from 'wagmi'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { ReactNode, useState } from "react";
import { config } from '@/lib/wagmi'
import { Toaster } from "sonner";
import { useContractRegistry } from '@/hooks/useContractRegistry'

// Populates the dynamic contract registry from /api/contracts.
// Must be inside QueryClientProvider.
function ContractRegistryInit() {
  useContractRegistry()
  return null
}

export function Providers({ children }: { children: ReactNode }) {
  const [queryClient] = useState(() => new QueryClient())

  return (
    <WagmiProvider config={config}>
      <QueryClientProvider client={queryClient}>
        <ContractRegistryInit />
        <HeroUIProvider>
          <AppProvider>
            <Toaster
              position="top-right"
              richColors
              closeButton
              duration={4000}
            />
            {children}
          </AppProvider>
        </HeroUIProvider>
      </QueryClientProvider>
    </WagmiProvider>
  );
}