using System.Text;

namespace adas_core.Domain.Utils;

/// <summary>
/// Represents a comparable collection of key/value pairs that extends the standard dictionary functionality.
/// </summary>
/// <typeparam name="TKey">The type of the keys in the dictionary, constrained to non-nullable types.</typeparam>
/// <typeparam name="TValue">The type of the values in the dictionary, constrained to non-nullable types.</typeparam>
/// <remarks>
/// Inherits from <see cref="Dictionary{TKey, TValue}"/> and enforces that both keys and values are non-nullable.
/// </remarks>
public class ComparableDictionary<TKey, TValue> : Dictionary<TKey, TValue> where TKey : notnull where TValue : notnull
{
    /// <summary>
    /// Generates a hash code for the collection by concatenating the string representations of all key-value pairs, separated by underscores and double percent signs, and returning the hash code of the resulting string.
    /// </summary>
    /// <returns>An integer hash code derived from the concatenated key-value pairs of the collection.</returns>
    public override int GetHashCode()
        {
            StringBuilder str = new();
            foreach (var item in this)
            {
                str.Append(item.Key);
                str.Append('_');
                str.Append(item.Value);
                str.Append("%%");
            }
    
            return str.ToString().GetHashCode();
        }
}