import { describe, expect, it } from 'vitest';
import { detectLocale } from './locale';

describe('detectLocale', () => {
	it('returns de for a German browser', () => {
		expect(detectLocale('de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7')).toBe('de');
	});

	it('returns en for an English browser', () => {
		expect(detectLocale('en-US,en;q=0.9')).toBe('en');
	});

	it('returns es for a Spanish browser', () => {
		expect(detectLocale('es-MX,es;q=0.9,en-US;q=0.8')).toBe('es');
	});

	it('falls back to a supported language when the primary is unsupported', () => {
		expect(detectLocale('fr-FR,fr;q=0.9,en;q=0.8')).toBe('en');
	});

	it('respects quality values — picks the highest-priority supported locale', () => {
		expect(detectLocale('en-US;q=0.7,de-DE;q=0.9')).toBe('de');
	});

	it('returns null for a completely unsupported language', () => {
		expect(detectLocale('ja-JP,ja;q=0.9,zh-CN;q=0.8')).toBeNull();
	});

	it('returns null for an empty header', () => {
		expect(detectLocale('')).toBeNull();
	});
});
