using ZB.MOM.WW.MxGateway.Server.Diagnostics;

namespace ZB.MOM.WW.MxGateway.Server.Dashboard;

internal static class DashboardRedactor
{
    private static readonly string[] SensitiveTextMarkers =
    [
        "apikey",
        "api_key",
        "authorization",
        "credential",
        "password",
        "secret",
        "token",
    ];

    /// <summary>
    /// Redacts sensitive content from a value for dashboard display.
    /// </summary>
    /// <param name="value">Value to redact.</param>
    /// <returns>Redacted value or original value if not sensitive.</returns>
    public static string? Redact(string? value)
    {
        if (string.IsNullOrWhiteSpace(value))
        {
            return value;
        }

        if (value.Contains("mxgw_", StringComparison.OrdinalIgnoreCase))
        {
            return GatewayLogRedactor.RedactClientIdentity(value);
        }

        return SensitiveTextMarkers.Any(marker => value.Contains(marker, StringComparison.OrdinalIgnoreCase))
            ? GatewayLogRedactor.RedactedValue
            : value;
    }
}
