using System.Diagnostics.CodeAnalysis;

namespace NeonSprawl.Server.Game.Items;

/// <summary>Adapter over <see cref="ItemDefinitionCatalog"/> (NEO-52).</summary>
public sealed class ItemDefinitionRegistry(ItemDefinitionCatalog catalog) : IItemDefinitionRegistry
{
    /// <inheritdoc />
    public bool TryGetDefinition(string? itemId, [NotNullWhen(true)] out ItemDefRow? definition)
    {
        if (itemId is null)
        {
            definition = null;
            return false;
        }

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

        definition = null;
        return false;
    }

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

        return list;
    }
}
