using System.Runtime.CompilerServices;

namespace InnovEnergy.Lib.Utils;

public static class ValueTupleUtils
{

    // https://stackoverflow.com/a/34006336/141397
    public static Int32 CombineHashes(this ITuple tupled, Int32 seed = 1009, Int32 factor = 9176)
    {
        var hash = seed;

        for (var i = 0; i < tupled.Length; i++)
        {
            if (tupled[i] == null)
                continue;

            unchecked
            {
                hash = hash * factor + tupled[i]!.GetHashCode();
            }
        }

        return hash;
    }

    // mixed type tuples
    public static IEnumerable<Object?> ToEnumerable(this ITuple t)
    {
        for (var i = 0; i < t.Length; i++)
            yield return t[i];
    }

    public static IEnumerable<T> ToEnumerable<T>(this ValueTuple<T, T> tuple)
    {
        yield return tuple.Item1;
        yield return tuple.Item2;
    }

    public static IEnumerable<T> ToEnumerable<T>(this ValueTuple<T, T, T> tuple)
    {
        yield return tuple.Item1;
        yield return tuple.Item2;
        yield return tuple.Item3;
    }

    public static IEnumerable<T> ToEnumerable<T>(this ValueTuple<T, T, T, T> tuple)
    {
        yield return tuple.Item1;
        yield return tuple.Item2;
        yield return tuple.Item3;
        yield return tuple.Item4;
    }

    public static IEnumerable<T> ToEnumerable<T>(this ValueTuple<T, T, T, T, T> tuple)
    {
        yield return tuple.Item1;
        yield return tuple.Item2;
        yield return tuple.Item3;
        yield return tuple.Item4;
        yield return tuple.Item5;
    }
}