using System.Text.RegularExpressions;

namespace YMhut.Box.Core;

public static class SensitiveText
{
    public static string Sanitize(string? message, int maxLength = 160)
    {
        if (string.IsNullOrWhiteSpace(message))
        {
            return "未知错误";
        }

        var sanitized = Regex.Replace(
            message,
            @"https?://[^\s\]\)""'<>]+",
            "远程服务",
            RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

        sanitized = Regex.Replace(
            sanitized,
            @"update\.ymhut\.cn[^\s\]\)""'<>]*",
            "远程服务",
            RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

        sanitized = sanitized
            .Replace("update-info.json", "发布服务", StringComparison.OrdinalIgnoreCase)
            .Replace("media-types.json", "媒体配置", StringComparison.OrdinalIgnoreCase)
            .Replace("api_url", "媒体源", StringComparison.OrdinalIgnoreCase)
            .Replace("download_url", "下载源", StringComparison.OrdinalIgnoreCase);

        return sanitized.Length <= maxLength
            ? sanitized
            : sanitized[..Math.Max(0, maxLength - 3)] + "...";
    }
}
