"use client"
import { useSteamStore } from "@/store/steam"
import { useEffect } from "react"
import "./globals.css"
import Providers from "./providers"
import { init } from "@/store"
import { useDebounce } from "ahooks"

export default function RootLayout({ children }: { children: React.ReactNode }) {
  useEffect(() => {
    void init()
  })

  // 检测steam路径和游戏路径是否有效
  const steam = useSteamStore()
  const debounceSteamDir = useDebounce(steam.state.steamDir, {wait: 500, leading: true, trailing: true, maxWait: 2500})
  const debounceCs2Dir = useDebounce(steam.state.cs2Dir, {wait: 500, leading: true, trailing: true, maxWait: 2500})
  const debounceSteamDirValid = useDebounce(steam.state.steamDirValid, {wait: 500, leading: true, trailing: true, maxWait: 2500})
  useEffect(() => {
    void steam.checkSteamDirValid()
  }, [debounceSteamDir])
  useEffect(() => {
    void steam.checkCs2DirValid()
  }, [debounceCs2Dir])
  useEffect(() => {
    if (debounceSteamDirValid) {
      void steam.getUsers()
    }
  }, [debounceSteamDirValid])

  return (
    <html lang="en">
      <body>
        <Providers>{children}</Providers>
      </body>
    </html>
  )
}
