import { ErrorString , InvalidOperationException } from "../../shared" ; import { IParallelEnumerable , ParallelGeneratorType } from "../../types" ; param source param predicate throws returns export const last = < TSource > ( source , predicate ? ) : Promise < TSource > => { if ( predicate ) { return last2 ( source , predicate ) ; } else { return last1 ( source ) ; } } ; const last1 = async < TSource > ( source ) : Promise < TSource > => { const dataFunc = source . dataFunc ; switch ( dataFunc . type ) { case ParallelGeneratorType . PromiseToArray : { const values = await dataFunc . generator ( ) ; if ( values . length === 0 ) { throw new InvalidOperationException ( ErrorString . NoElements ) ; } else { return values [ values . length - 1 ] ; } } case ParallelGeneratorType . ArrayOfPromises : { const promises = dataFunc . generator ( ) ; if ( promises . length === 0 ) { throw new InvalidOperationException ( ErrorString . NoElements ) ; } else { return await promises [ promises . length - 1 ] ; } } case ParallelGeneratorType . PromiseOfPromises : { const promises = await dataFunc . generator ( ) ; if ( promises . length === 0 ) { throw new InvalidOperationException ( ErrorString . NoElements ) ; } else { return await promises [ promises . length - 1 ] ; } } } } ; const last2 = async < TSource > ( source , predicate ) : Promise < TSource > => { const dataFunc = source . dataFunc ; switch ( dataFunc . type ) { case ParallelGeneratorType . PromiseToArray : { const values = await dataFunc . generator ( ) ; for ( let i = values . length - 1 ; i >= 0 ; i -- ) { const value = values [ i ] ; if ( predicate ( value ) ) { return value ; } } break ; } case ParallelGeneratorType . ArrayOfPromises : { const promises = dataFunc . generator ( ) ; for ( let i = promises . length - 1 ; i >= 0 ; i -- ) { const value = await promises [ i ] ; if ( predicate ( value ) ) { return value ; } } break ; } case ParallelGeneratorType . PromiseOfPromises : { const promises = await dataFunc . generator ( ) ; for ( let i = promises . length - 1 ; i >= 0 ; i -- ) { const value = await promises [ i ] ; if ( predicate ( value ) ) { return value ; } } break ; } } throw new InvalidOperationException ( ErrorString . NoMatch ) ; } ;