namespace YMhut.Box.Core.Settings;

public static class LanguagePreference
{
    public const string Chinese = "zh-CN";
    public const string English = "en-US";

    public static string Normalize(string? language)
    {
        var value = (language ?? string.Empty).Trim();
        if (value.Length == 0)
        {
            return Chinese;
        }

        var normalized = value
            .Replace('_', '-')
            .Trim()
            .ToLowerInvariant();

        if (normalized is "en" or "english" ||
            normalized.StartsWith("en-", StringComparison.Ordinal))
        {
            return English;
        }

        if (normalized is "zh" or "cn" or "chinese" or "simplified-chinese" or "zh-hans" ||
            normalized.StartsWith("zh-", StringComparison.Ordinal) ||
            normalized.Contains("中文", StringComparison.Ordinal) ||
            normalized.Contains("简体", StringComparison.Ordinal))
        {
            return Chinese;
        }

        return Chinese;
    }

    public static bool IsEnglish(string? language)
        => string.Equals(Normalize(language), English, StringComparison.OrdinalIgnoreCase);
}
