import { Definition } from '@ulangi/ulangi-common/interfaces' ; import { pickRandomFromImmutableMap } from '@ulangi/ulangi-common/utils' ; import { OrderedMap } from 'immutable' ; export class DefinitionPool < D extends Definition = Definition > { private definitionPool : OrderedMap < string , D > ; public constructor ( definitions ? ) { this . definitionPool = OrderedMap ( definitions || [ ] ) ; } public getPoolSize ( ) { return this . definitionPool . size ; } public merge ( definitions ) { this . definitionPool = OrderedMap ( definitions || [ ] ) ; } public getRandomDefinitionsFromPool ( limit , unique , except ? ) { let picked = [ ] ; let definitionPool = this . definitionPool ; if ( typeof except !== 'undefined' ) { except . forEach ( ( definitionId ) : void => { definitionPool = definitionPool . remove ( definitionId ) ; } ) ; } while ( picked . length < limit && ! definitionPool . isEmpty ( ) ) { const [ definitionId , definition ] = pickRandomFromImmutableMap ( definitionPool ) ; if ( unique === true ) { definitionPool = definitionPool . remove ( definitionId ) ; } picked . push ( definition ) ; } return picked ; } }