using System.ComponentModel;
using System.Security.Claims;

namespace DD.Persistence.API;

public static class Extensions
{
    public static T GetUserId<T>(this ClaimsPrincipal principal)
    {
        ArgumentNullException.ThrowIfNull(principal, nameof(principal));

        var loggedInUserId = principal.FindFirstValue(ClaimTypes.NameIdentifier);

        ArgumentNullException.ThrowIfNullOrEmpty(loggedInUserId, nameof(loggedInUserId));

        var result = TypeDescriptor.GetConverter(typeof(T)).ConvertFromInvariantString(loggedInUserId);

        ArgumentNullException.ThrowIfNull(result, nameof(result));

        return (T)result;

    }
}