import { h, createContext, type FunctionComponent } from 'preact'
import { useSignal } from '@preact/signals'
import { useContext } from 'preact/hooks'

type AuthContextType = { user: ANY }

const AuthContext = createContext<AuthContextType | null>(null)

export const AuthProvider: FunctionComponent<{ user: ANY }> = ({ children, user }) => {
  const userSignal = useSignal(user)

  return <AuthContext.Provider value={{ user: userSignal }}>{children}</AuthContext.Provider>
}

export const useAuth = () => useContext(AuthContext) as AuthContextType
