using System;
using System.Collections.Generic;
using System.Linq;

namespace Wayne.Lib
{
    /// <summary>
    /// Various extension methods for Enumerables.
    /// </summary>
    public static class EnumerableExtensions
    {
        /// <summary>
        /// Apply an action for each item in an enumerable.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumerable"></param>
        /// <param name="action"></param>
        public static void ForEach<T>(this  IEnumerable<T> enumerable, Action<T> action)
        {
            foreach (var item in enumerable)
            {
                action(item);
            }
        }

        /// <summary>
        /// Checks wether a given value is contained in an array of values. Similar to
        /// Linq Contains, but the readability of the code is different (Better) depending on the domain.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static bool IsIn<T>(this T value, params T[] values)
        {
            return values.Contains(value);
        }

        /// <summary>
        /// Returns a sequence that is guaranteed to not be null and not contain null values.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumerable"></param>
        /// <returns>Returns all not null items or an empty IEnumerable</returns>
        public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T> enumerable)
        {
            if (enumerable == null)
                return new T[] {};
            return enumerable.Where(x=> x != null);
        }
    }
}