using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using StellaOps.Auth.Abstractions;
namespace StellaOps.Auth.ServerIntegration;
/// 
/// Authorisation requirement enforcing StellaOps scope membership.
/// 
public sealed class StellaOpsScopeRequirement : IAuthorizationRequirement
{
    /// 
    /// Initialises a new instance of the  class.
    /// 
    /// Scopes that satisfy the requirement.
    public StellaOpsScopeRequirement(IEnumerable scopes)
    {
        ArgumentNullException.ThrowIfNull(scopes);
        var normalized = new HashSet(StringComparer.Ordinal);
        foreach (var scope in scopes)
        {
            var value = StellaOpsScopes.Normalize(scope);
            if (value is null)
            {
                continue;
            }
            normalized.Add(value);
        }
        if (normalized.Count == 0)
        {
            throw new ArgumentException("At least one scope must be provided.", nameof(scopes));
        }
        RequiredScopes = normalized.OrderBy(static scope => scope, StringComparer.Ordinal).ToArray();
    }
    /// 
    /// Gets the required scopes.
    /// 
    public IReadOnlyCollection RequiredScopes { get; }
}