// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information. 
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace System.Linq
{
    public static partial class AsyncEnumerableEx
    {
        /// 
        /// Determines whether an async-enumerable sequence is empty.
        /// 
        /// The type of the elements in the source sequence.
        /// An async-enumerable sequence to check for emptiness.
        /// The optional cancellation token to be used for cancelling the sequence at any time.
        /// An async-enumerable sequence containing a single element determining whether the source sequence is empty.
        ///  is null.
        public static ValueTask IsEmptyAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default)
        {
            if (source == null)
                throw Error.ArgumentNull(nameof(source));
            return Core(source, cancellationToken);
            static async ValueTask Core(IAsyncEnumerable source, CancellationToken cancellationToken)
            {
                await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
                return !await e.MoveNextAsync();
            }
        }
    }
}