import { beforeEach, describe, expect, it, vi } from 'vitest'

const cookieValues = vi.hoisted(() => new Map<string, string>())

vi.mock('next/headers', () => ({
  cookies: async () => ({
    get: (name: string) => {
      const value = cookieValues.get(name)
      return value === undefined ? undefined : { value }
    },
  }),
}))

import { getCarplaceLanguage } from './i18n.server'
import { SHARED_LANGUAGE_COOKIE } from './i18n'

describe('getCarplaceLanguage', () => {
  beforeEach(() => cookieValues.clear())

  it.each(['en', 'fr', 'ar'] as const)('reads the shared %s language cookie', async (language) => {
    cookieValues.set(SHARED_LANGUAGE_COOKIE, language)
    await expect(getCarplaceLanguage()).resolves.toBe(language)
  })

  it('defaults to English when the shared cookie is absent or invalid', async () => {
    await expect(getCarplaceLanguage()).resolves.toBe('en')
    cookieValues.set(SHARED_LANGUAGE_COOKIE, 'es')
    await expect(getCarplaceLanguage()).resolves.toBe('en')
  })
})
