// Package locale provides the shared locale helpers used by every preset
// loader in the module. Locale codes follow BCP 47 on the public API
// (e.g. "zh-CN", "ja-JP") but preset asset directories use the kebab-case
// form ("zh-cn", "ja") for filesystem portability.
package locale

import "strings"

const (
	English           = "en"
	SimplifiedChinese = "zh-CN"
	Japanese          = "ja"
)

// Normalize maps an arbitrary locale string to one of the supported preset
// locales, defaulting to English.
func Normalize(locale string) string {
	normalized := strings.ToLower(strings.ReplaceAll(strings.TrimSpace(locale), "_", "-"))
	switch {
	case normalized == "zh" || strings.HasPrefix(normalized, "zh-cn") || strings.HasPrefix(normalized, "zh-hans"):
		return SimplifiedChinese
	case normalized == "ja" || strings.HasPrefix(normalized, "ja-"):
		return Japanese
	default:
		return English
	}
}

// Dir returns the kebab-case directory name used inside assets/ for the
// given BCP 47 locale.
func Dir(locale string) string {
	switch Normalize(locale) {
	case SimplifiedChinese:
		return "zh-cn"
	case Japanese:
		return "ja"
	default:
		return "en"
	}
}
