import { ErrorString , InvalidOperationException } from "../../shared" ; type MaxFunc = { param source throws returns ( source ) : Promise < number > ; param source param selector throws returns < TSource > ( source , selector ) : Promise < number > ; } ; export const max = < TSource > ( source , selector ? ) : Promise < number > => { if ( selector ) { return max2 < TSource > ( source as AsyncIterable < TSource > , selector ) ; } else { return max1 ( source as AsyncIterable < number > ) ; } } ; const max1 = async ( source ) => { let maxItem = null ; for await ( const item of source ) { maxItem = Math . max ( maxItem || Number . NEGATIVE_INFINITY , item ) ; } if ( maxItem === null ) { throw new InvalidOperationException ( ErrorString . NoElements ) ; } else { return maxItem ; } } ; const max2 = async < TSource > ( source , selector ) => { let maxItem = null ; for await ( const item of source ) { maxItem = Math . max ( maxItem || Number . NEGATIVE_INFINITY , selector ( item ) ) ; } if ( maxItem === null ) { throw new InvalidOperationException ( ErrorString . NoElements ) ; } else { return maxItem ; } } ;