import { AdminProvidersWrapper } from '@/components/admin-providers-wrapper'
import { headers } from 'next/headers'
import { detectUserLanguage, parseAcceptLanguage } from '@/lib/i18n/detect-user-language'
import { loadTranslations } from '@/lib/i18n/load-translations'

// No <Suspense> here intentionally: combining a Suspense boundary with <Link>
// components inside the subtree triggers a React production-only bug (React
// issue #33580, Next.js issue #63388) causing Error #310 "too many re-renders".
export default async function AdminGroupLayout({
  children,
}: {
  children: React.ReactNode
}) {
  const headersList = await headers()
  const browserLang = parseAcceptLanguage(headersList.get('accept-language'))
  const initialLanguage = await detectUserLanguage(browserLang)
  const initialTranslations = await loadTranslations(initialLanguage)

  return (
    <AdminProvidersWrapper
      initialLanguage={initialLanguage}
      initialTranslations={initialTranslations}
    >
      {children}
    </AdminProvidersWrapper>
  )
}
