'use client'

import { useState, useEffect } from 'react'
import { defaultLanguage, type LanguageCode } from '@/lib/i18n/config'

export function useLanguage() {
  const [language, setLanguageState] = useState<LanguageCode>(() => {
    if (typeof window === 'undefined') return defaultLanguage
    const saved = localStorage.getItem('language')
    return (saved as LanguageCode) || defaultLanguage
  })

  useEffect(() => {
    if (typeof window !== 'undefined') {
      localStorage.setItem('language', language)
      document.documentElement.lang = language
    }
  }, [language])

  const setLanguage = (lang: LanguageCode) => {
    setLanguageState(lang)
  }

  return { language, setLanguage }
}

