import { describe, it, expect } from 'vitest';
import { detectLanguage } from './accept-language';

describe('detectLanguage (Accept-Language style strings)', () => {
	it('AL-01: bare "es" maps to es', () => {
		expect(detectLanguage('es')).toBe('es');
	});

	it('AL-02: bare "en" maps to en', () => {
		expect(detectLanguage('en')).toBe('en');
	});

	it('AL-03: regional variant "es-ES" maps to es', () => {
		expect(detectLanguage('es-ES')).toBe('es');
	});

	it('AL-04: es;q=0.9,en;q=0.8 — es wins (higher q)', () => {
		expect(detectLanguage('es;q=0.9,en;q=0.8')).toBe('es');
	});

	it('AL-05: en-US,es;q=0.3 — en wins (no q means 1.0)', () => {
		expect(detectLanguage('en-US,es;q=0.3')).toBe('en');
	});

	it('AL-06: empty string falls back to en', () => {
		expect(detectLanguage('')).toBe('en');
	});

	it('AL-07: null falls back to en', () => {
		expect(detectLanguage(null)).toBe('en');
	});

	it('AL-08: undefined falls back to en', () => {
		expect(detectLanguage(undefined)).toBe('en');
	});

	it('AL-09: malformed string falls back to en', () => {
		expect(detectLanguage('!!!@#$;q=??')).toBe('en');
	});

	it('AL-10: es with q=0.5 — borderline accepted', () => {
		// Per plan §10.8.1: q-score >= 0.5 still counts as Spanish preference.
		expect(detectLanguage('en;q=0.4,es;q=0.5')).toBe('es');
	});

	it('AL-11: array of language tags (navigator.languages) — first es wins', () => {
		expect(detectLanguage(['en-GB', 'es-ES', 'fr'])).toBe('en');
		expect(detectLanguage(['es-ES', 'en-GB'])).toBe('es');
	});

	it('AL-12: unknown language (fr) falls back to en', () => {
		expect(detectLanguage('fr-FR')).toBe('en');
	});
});
