using System.Text.Json;

namespace ZB.MOM.WW.MxGateway.Server.Security.Authentication;

public static class ApiKeyConstraintSerializer
{
    private static readonly JsonSerializerOptions JsonOptions = new()
    {
        PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
        WriteIndented = false,
    };

    public static string? Serialize(ApiKeyConstraints constraints)
    {
        ArgumentNullException.ThrowIfNull(constraints);
        return constraints.IsEmpty ? null : JsonSerializer.Serialize(constraints, JsonOptions);
    }

    public static ApiKeyConstraints Deserialize(string? json)
    {
        if (string.IsNullOrWhiteSpace(json))
        {
            return ApiKeyConstraints.Empty;
        }

        return JsonSerializer.Deserialize<ApiKeyConstraints>(json, JsonOptions) ?? ApiKeyConstraints.Empty;
    }
}
