package restapi

import "testing"

func TestResolvePreferredLanguage(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name  string
		value string
		want  string
	}{
		{
			name:  "canonical valid tag",
			value: "fr-FR, en;q=0.8",
			want:  "fr-FR",
		},
		{
			name:  "quality ordering",
			value: "en-US;q=0.9, fr",
			want:  "fr",
		},
		{
			name:  "wildcard falls back",
			value: "*",
			want:  "en",
		},
		{
			name:  "malformed falls back",
			value: "fr-FR, @@",
			want:  "en",
		},
		{
			name:  "missing falls back",
			value: "",
			want:  "en",
		},
	}

	for _, tt := range tests {
		tt := tt

		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			if got := resolvePreferredLanguage(tt.value); got != tt.want {
				t.Fatalf("resolvePreferredLanguage(%q) = %q, want %q", tt.value, got, tt.want)
			}
		})
	}
}
