package restapi

import "golang.org/x/text/language"

const defaultPreferredLanguage = "en"

func resolvePreferredLanguage(value string) string {
	tags, _, err := language.ParseAcceptLanguage(value)
	if err != nil {
		return defaultPreferredLanguage
	}

	for _, tag := range tags {
		canonical := tag.String()
		switch canonical {
		case "", "und", "mul":
			continue
		default:
			return canonical
		}
	}

	return defaultPreferredLanguage
}
