import { ErrorString , InvalidOperationException } from "../../shared" ; import { IParallelEnumerable , TypedData } from "../../types" ; import { nextIteration } from "./_nextIteration" ; import { typeDataToArray } from "./_typeDataToArray" ; type MaxFunc = { param source throws returns ( source ) : Promise < number > ; param source param selector throws returns < TSource > ( source , selector ) : Promise < number > ; } ; export const max = async < TSource > ( source , selector ? ) : Promise < number > => { let dataFunc ; if ( selector ) { dataFunc = nextIteration ( source as IParallelEnumerable < TSource > , selector ) ; } else { dataFunc = source . dataFunc as TypedData < number > ; } const data = await typeDataToArray ( dataFunc ) ; if ( data . length === 0 ) { throw new InvalidOperationException ( ErrorString . NoElements ) ; } return Math . max . apply ( null , data ) ; } ;