<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
// 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;
using System.Threading;
using System.Threading.Tasks;

namespace System.Linq
{
    public static partial class AsyncEnumerable
    {
<#
var os = new[]
{
    new { type = "int", res = "double" },
    new { type = "long", res = "double" },
    new { type = "float", res = "float" },
    new { type = "double", res = "double" },
    new { type = "decimal", res = "decimal" },
    new { type = "int?", res = "double?" },
    new { type = "long?", res = "double?" },
    new { type = "float?", res = "float?" },
    new { type = "double?", res = "double?" },
    new { type = "decimal?", res = "decimal?" },
};

foreach (var o in os)
{
    var n = o.type.EndsWith("?") ? ".GetValueOrDefault()" : "";
#>
        public static Task<<#=o.res#>> Average(this IAsyncEnumerable<<#=o.type#>> source)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));

            return Average_(source, CancellationToken.None);
        }

        public static Task<<#=o.res#>> Average(this IAsyncEnumerable<<#=o.type#>> source, CancellationToken cancellationToken)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));

            return Average_(source, cancellationToken);
        }

        public static Task<<#=o.res#>> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (selector == null)
                throw new ArgumentNullException(nameof(selector));

            return source.Select(selector).Average(CancellationToken.None);
        }

        public static Task<<#=o.res#>> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (selector == null)
                throw new ArgumentNullException(nameof(selector));

            return source.Select(selector).Average(cancellationToken);
        }

        public static Task<<#=o.res#>> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<<#=o.type#>>> selector)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (selector == null)
                throw new ArgumentNullException(nameof(selector));

            return source.Select(selector).Average(CancellationToken.None);
        }

        public static Task<<#=o.res#>> Average<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<<#=o.type#>>> selector, CancellationToken cancellationToken)
        {
            if (source == null)
                throw new ArgumentNullException(nameof(source));
            if (selector == null)
                throw new ArgumentNullException(nameof(selector));

            return source.Select(selector).Average(cancellationToken);
        }

<#
}
#>
    }
}
