﻿"use client"
import { useEffect, useState } from 'react'

type Props = { initial: 'en' | 'de'; labels: { en: string; de: string } }

export default function LanguageToggle({ initial, labels }: Props) {
  const [lang, setLang] = useState<'en' | 'de'>(initial)
  useEffect(() => { setLang(initial) }, [initial])
  function setCookieLang(next: 'en' | 'de') {
    document.cookie = `lang=${next}; path=/; max-age=31536000; SameSite=Lax`
    const url = new URL(window.location.href)
    if (next === 'de') url.searchParams.set('lang', 'de')
    else url.searchParams.delete('lang')
    window.location.replace(url.toString())
  }
  return (
    <button className="btn ghost" aria-pressed={lang === 'de'} aria-label="Switch language" onClick={() => setCookieLang(lang === 'en' ? 'de' : 'en')}>
      {lang === 'en' ? labels.de : labels.en}
    </button>
  )
}
