import { useAuth } from "@/hooks/useAuth"
import { Href, useRouter } from "expo-router"
import { useRouteInfo } from "expo-router/build/hooks"
import { ReactNode, useEffect } from "react"

type Props = {
  loginRedirect: Href
  children: ReactNode
}

export default function LoginProvider({ loginRedirect, children }: Props) {
  const router = useRouter()
  const route = useRouteInfo()

  // Si on est pas connecté⋅e, on reste sur la fenêtre de connexion
  const auth = useAuth()
  useEffect(() => {
    if (!auth.loggedIn && route.pathname !== loginRedirect)
      router.navigate(loginRedirect)
  }, [auth, route, router])

  return <>
    {children}
  </>
}