using System.Collections;
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 : IEnumerable where T : INamedValue
{
///
/// The underlying data storage.
///
protected 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;
///
public IEnumerator GetEnumerator() => _values.GetEnumerator();
///
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}