import { assertExists } from '@ulangi/assert' ; import { ObservableVocabularyListState } from '@ulangi/ulangi-observable' ; import { runInAction } from 'mobx' ; export class VocabularySelectionDelegate { private vocabularyListState : ObservableVocabularyListState ; public constructor ( vocabularyListState ) { this . vocabularyListState = vocabularyListState ; } public toggleSelection ( vocabularyId ) { if ( this . vocabularyListState . vocabularyList !== null ) { if ( this . vocabularyListState . vocabularyList . has ( vocabularyId ) ) { const vocabulary = assertExists ( this . vocabularyListState . vocabularyList . get ( vocabularyId ) ) ; vocabulary . isSelected . set ( ! vocabulary . isSelected . get ( ) ) ; } if ( this . vocabularyListState . isSelectionModeOn . get ( ) === false ) { this . vocabularyListState . isSelectionModeOn . set ( true ) ; } } } public clearSelections ( ) { this . vocabularyListState . isSelectionModeOn . set ( false ) ; runInAction ( ( ) : void => { if ( this . vocabularyListState . vocabularyList !== null ) { return Array . from ( this . vocabularyListState . vocabularyList . values ( ) ) . forEach ( ( vocabulary ) : void => { vocabulary . isSelected . set ( false ) ; } ) ; } } ) ; } }