package domain

import "strings"

// NormalizeLanguage maps FE / Accept-Language / preferred_language → template Language.
// FE locales: zh-TW | en
func NormalizeLanguage(raw string) Language {
	s := strings.ToLower(strings.TrimSpace(raw))
	if s == "" {
		return LangZhTW
	}
	// take first tag only (e.g. "en-US,en;q=0.9")
	if i := strings.IndexAny(s, ",;"); i >= 0 {
		s = strings.TrimSpace(s[:i])
	}
	s = strings.ReplaceAll(s, "_", "-")
	switch {
	case s == "en" || strings.HasPrefix(s, "en-"):
		return LangEnUS
	case s == "zh" || strings.HasPrefix(s, "zh-"):
		// zh-TW / zh-Hant / zh-CN → 目前模板只有繁中，統一 zh-tw
		return LangZhTW
	default:
		return LangZhTW
	}
}
