using OpenIddict.Abstractions;
using System.Security.Claims;

namespace MemberCenter.Api.Extensions;

public static class ClaimsExtensions
{
    public static IEnumerable<string> GetDestinations(System.Security.Claims.Claim claim)
    {
        return claim.Type switch
        {
            OpenIddictConstants.Claims.Name or OpenIddictConstants.Claims.Email =>
                new[] { OpenIddictConstants.Destinations.AccessToken, OpenIddictConstants.Destinations.IdentityToken },
            _ => new[] { OpenIddictConstants.Destinations.AccessToken }
        };
    }

    public static bool HasScope(this ClaimsPrincipal user, string scope)
    {
        var values = user.FindAll(OpenIddictConstants.Claims.Scope)
            .SelectMany(c => c.Value.Split(' ', StringSplitOptions.RemoveEmptyEntries));
        return values.Contains(scope, StringComparer.Ordinal);
    }
}
