import {Redirect} from 'expo-router'
import {type ReactNode} from 'react'

import {useAuth} from '@/stores/auth'

// the signed-in gate for a screen or a whole layout: blank while the
// session hydrates, sign-in when it resolves to no user
export function AuthGate({children}: {children: ReactNode}) {
  const user = useAuth((s) => s.user)
  if (user === undefined) return null
  if (user === null) return <Redirect href="/sign-in" />
  return children
}
