using System.Diagnostics.CodeAnalysis;

namespace NeonSprawl.Server.Game.Skills;

/// <summary>Adapter over <see cref="SkillDefinitionCatalog"/> (NEO-35).</summary>
public sealed class SkillDefinitionRegistry(SkillDefinitionCatalog catalog) : ISkillDefinitionRegistry
{
    /// <inheritdoc />
    public bool TryGetDefinition(string? skillId, [NotNullWhen(true)] out SkillDefRow? definition)
    {
        if (skillId is null)
        {
            definition = null;
            return false;
        }

        if (catalog.ById.TryGetValue(skillId, out var row))
        {
            definition = row;
            return true;
        }

        definition = null;
        return false;
    }

    /// <inheritdoc />
    public IReadOnlyList<SkillDefRow> GetDefinitionsInIdOrder()
    {
        var ids = catalog.ById.Keys.OrderBy(k => k, StringComparer.Ordinal).ToArray();
        var list = new List<SkillDefRow>(ids.Length);
        foreach (var id in ids)
        {
            list.Add(catalog.ById[id]);
        }

        return list;
    }
}
