// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information. 
using System.Collections.Generic;
namespace System.Linq
{
    public static partial class EnumerableEx
    {
        /// 
        ///     Generates a sequence by repeating the given value infinitely.
        /// 
        /// Result sequence element type.
        /// Value to repreat in the resulting sequence.
        /// Sequence repeating the given value infinitely.
        public static IEnumerable Repeat(TResult value)
        {
            while (true)
                yield return value;
        }
        /// 
        ///     Generates a sequence that contains one repeated value.
        /// 
        /// Result sequence element type.
        /// The value to be repeated.
        /// The number of times to repeat the value in the generated sequence.
        /// Sequence that contains a repeated value.
        public static IEnumerable Repeat(TResult element, int count)
        {
            return Enumerable.Repeat(element, count);
        }
        /// 
        ///     Repeats and concatenates the source sequence infinitely.
        /// 
        /// Source sequence element type.
        /// Source sequence.
        /// Sequence obtained by concatenating the source sequence to itself infinitely.
        public static IEnumerable Repeat(this IEnumerable source)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            return Repeat_(source);
        }
        /// 
        ///     Repeats and concatenates the source sequence the given number of times.
        /// 
        /// Source sequence element type.
        /// Source sequence.
        /// Number of times to repeat the source sequence.
        /// Sequence obtained by concatenating the source sequence to itself the specified number of times.
        public static IEnumerable Repeat(this IEnumerable source, int count)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (count < 0)
                throw new ArgumentOutOfRangeException(nameof(count));
            return Repeat_(source, count);
        }
        private static IEnumerable Repeat_(IEnumerable source)
        {
            while (true)
                foreach (var item in source)
                    yield return item;
        }
        private static IEnumerable Repeat_(IEnumerable source, int count)
        {
            for (var i = 0; i < count; i++)
                foreach (var item in source)
                    yield return item;
        }
    }
}