using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using StellaOps.Authority.Plugins.Abstractions;

namespace StellaOps.Authority;

internal sealed class AuthorityPluginRegistry : IAuthorityPluginRegistry
{
    private readonly IReadOnlyDictionary<string, AuthorityPluginContext> registry;

    public AuthorityPluginRegistry(IEnumerable<AuthorityPluginContext> contexts)
    {
        registry = contexts.ToDictionary(c => c.Manifest.Name, StringComparer.OrdinalIgnoreCase);
        Plugins = registry.Values.ToArray();
    }

    public IReadOnlyCollection<AuthorityPluginContext> Plugins { get; }

    public bool TryGet(string name, [NotNullWhen(true)] out AuthorityPluginContext? context)
        => registry.TryGetValue(name, out context);
}
