'use client'

import { createContext, useContext, type ReactNode } from 'react'
import type { RouterAdapter } from '../adapters/router'

const Context = createContext<RouterAdapter | null>(null)

interface RouterContextProps {
    children: ReactNode
    router: RouterAdapter
}

export function RouterContext({ children, router }: RouterContextProps) {
    return (
        <Context value={router}>
            {children}
        </Context>
    )
}

/**
 * Hook to access the router adapter.
 * Must be used within AllauthContext.
 */
export function useRouter(): RouterAdapter {
    const router = useContext(Context)
    if (!router) {
        throw new Error('useRouter must be used within AllauthContext')
    }
    return router
}
