import { ErrorString , InvalidOperationException } from "../../shared" ; type AggregateFunc = { param source param func returns < TSource > ( source , func ) : Promise < TSource > ; param source param seed param func returns < TSource , TAccumulate > ( source , seed , func ) : Promise < TAccumulate > ; param source param seed param func param resultSelector returns < TSource , TAccumulate , TResult > ( source , seed , func , resultSelector ) : Promise < TResult > ; } ; export const aggregate = < TSource , TAccumulate , TResult > ( source , seedOrFunc , func ? , resultSelector ? ) : Promise < TSource | TAccumulate | TResult | null > => { if ( resultSelector ) { if ( ! func ) { throw new ReferenceError ( `TAccumulate function is undefined` ) ; } return aggregate3 ( source , seedOrFunc as TAccumulate , func , resultSelector ) ; } else if ( func ) { return aggregate2 ( source , seedOrFunc as TAccumulate , func ) ; } else { return aggregate1 ( source , seedOrFunc as ( ( x : TSource , y : TSource ) => TSource ) ) ; } } ; const aggregate1 = async < TSource > ( source , func ) : Promise < TSource > => { let aggregateValue ; for await ( const value of source ) { if ( aggregateValue ) { aggregateValue = func ( aggregateValue , value ) ; } else { aggregateValue = value ; } } if ( aggregateValue === undefined ) { throw new InvalidOperationException ( ErrorString . NoElements ) ; } return aggregateValue ; } ; const aggregate2 = async < TSource , TAccumulate > ( source , seed , func ) : Promise < TAccumulate > => { let aggregateValue = seed ; for await ( const value of source ) { aggregateValue = func ( aggregateValue , value ) ; } return aggregateValue ; } ; const aggregate3 = async < TSource , TAccumulate , TResult > ( source , seed , func , resultSelector ) : Promise < TResult > => { let aggregateValue = seed ; for await ( const value of source ) { aggregateValue = func ( aggregateValue , value ) ; } return resultSelector ( aggregateValue ) ; } ;