import { cookies } from 'next/headers'
import { MARKETPLACE_LANGUAGE_COOKIE, SHARED_LANGUAGE_COOKIE, isStorefrontLanguage, type StorefrontLanguage } from './i18n'

export async function getStorefrontLanguage(): Promise<StorefrontLanguage> {
  const cookieStore = await cookies()
  const cookieValue =
    cookieStore.get(SHARED_LANGUAGE_COOKIE)?.value ??
    cookieStore.get(MARKETPLACE_LANGUAGE_COOKIE)?.value
  return isStorefrontLanguage(cookieValue) ? cookieValue : 'en'
}
