import { Vocabulary } from '@ulangi/ulangi-common/interfaces' ; import { pickRandomFromImmutableMap } from '@ulangi/ulangi-common/utils' ; import { OrderedMap } from 'immutable' ; export class VocabularyPool < V extends Vocabulary = Vocabulary > { private vocabularyPool : OrderedMap < string , V > ; public constructor ( vocabularyList ? ) { this . vocabularyPool = OrderedMap ( vocabularyList || [ ] ) ; } public getPoolSize ( ) { return this . vocabularyPool . size ; } public merge ( vocabularyList ) { this . vocabularyPool = this . vocabularyPool . merge ( vocabularyList ) ; } public getRandomVocabularyFromPool ( limit , unique , except ? ) { let picked = [ ] ; let vocabularyPool = this . vocabularyPool ; if ( typeof except !== 'undefined' ) { except . forEach ( ( vocabularyId ) : void => { vocabularyPool = vocabularyPool . remove ( vocabularyId ) ; } ) ; } while ( picked . length < limit && ! vocabularyPool . isEmpty ( ) ) { const [ vocabularyId , vocabulary ] = pickRandomFromImmutableMap ( vocabularyPool ) ; if ( unique === true ) { vocabularyPool = vocabularyPool . remove ( vocabularyId ) ; } picked . push ( vocabulary ) ; } return picked ; } }