using System.Diagnostics.CodeAnalysis;
using PkmnLib.Static.Utils;
namespace PkmnLib.Static.Libraries;
///
/// A basic library for data types. Stores data both by name and by index.
///
public abstract class DataLibrary
where T : INamedValue
{
private readonly Dictionary _data = new();
private readonly List _values = [];
///
/// Adds a value to the library.
///
public void Add(T value)
{
_data.Add(value.Name, value);
_values.Add(value);
}
///
/// Removes a value from the library.
///
public void Remove(T value)
{
_data.Remove(value.Name);
_values.Remove(value);
}
///
/// Try to get a value from the library. Returns false if the value is not found.
///
public bool TryGet(StringKey key, [MaybeNullWhen(false)] out T value) => _data.TryGetValue(key, out value);
///
/// Get a random value from the library.
///
public T GetRandom(IRandom random) => _values[random.GetInt(_values.Count)];
///
/// The number of values in the library.
///
public int Count => _values.Count;
///
/// Whether the library is empty.
///
public bool IsEmpty => _values.Count == 0;
}