namespace RpgRoller.Services;

public static class RoleSerializer
{
    public static IReadOnlyList<string> Parse(string serializedRoles)
    {
        return Normalize(serializedRoles.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries));
    }

    public static string Serialize(IReadOnlyList<string> roles)
    {
        return string.Join(",", Normalize(roles));
    }

    public static string[] Normalize(IEnumerable<string> roles)
    {
        return roles.Where(role => !string.IsNullOrWhiteSpace(role)).Select(role => role.Trim().ToLowerInvariant()).Distinct(StringComparer.Ordinal).OrderBy(role => role, StringComparer.Ordinal).ToArray();
    }

    public static bool HasRole(string serializedRoles, string role)
    {
        return Parse(serializedRoles).Contains(role, StringComparer.OrdinalIgnoreCase);
    }
}