import { AutocompleteItem , SearchableAutoCompleteItems } from './AutocompleteItem' ; import { TemplateRef } from '@angular/core' ; export class AutocompleteGroup { initialValue : { [ value ] : AutocompleteItem ; } ; key : string ; keys : { titleKey : string ; childrenKey : string | null ; } ; value : { [ value ] : AutocompleteItem ; } ; remove : string [ ] ; placeholder : string ; parent : string ; completion : boolean ; searchLength : number ; async : ( str : string ) => Promise < { id : string | number ; [ value ] : any ; } [ ] > = null ; noResults : TemplateRef < any > ; dropdownValue : TemplateRef < any > ; placeholderValue : TemplateRef < any > ; private removals : string [ ] = [ ] ; private _copy : { [ value ] : AutocompleteItem ; } ; constructor ( ) { } /**
     *
     */ SetNewValue ( value , titleKey ) { const values = SearchableAutoCompleteItems ( value , titleKey ) ; this . SetCopy ( values ) ; /**
         *
         */ this . value = this . FilterRemovals ( this . removals , values ) ; } /**
     *
     */ Removables ( ids ) { this . removals = ids ; /**
         *
         */ this . value = this . FilterRemovals ( this . removals , this . _copy ) ; } /**
     *
     */ InitialValue ( ) { this . value = this . FilterRemovals ( this . removals , this . initialValue ) ; /**
         *
         */ this . SetCopy ( this . initialValue ) ; } /**
     *
     */ SetCopy ( values ) { this . _copy = Object . assign ( [ ] , values ) ; } /**
     *
     */ SetValues ( value ) { this . value = SearchableAutoCompleteItems ( value , this . keys . titleKey , this . keys . childrenKey ) ; /**
         *
         */ this . initialValue = Object . assign ( { } , this . value ) ; this . SetCopy ( Object . assign ( { } , this . value ) ) ; } /**
     *
     */ FilterRemovals ( removals , value ) { let filtered = Object . assign ( { } , value ) ; let key , keys = [ ] ; for ( key in filtered ) { if ( filtered . hasOwnProperty ( key ) ) { removals . forEach ( ( id ) => { let f = `_id_${ String ( id ) }` ; let c = key . substring ( key . indexOf ( f ) , key . length ) ; if ( f === c ) { keys . push ( key ) ; } } ) ; } } keys . forEach ( ( k ) => { delete filtered [ k ] ; } ) ; return filtered ; } } /**
 *
 */ export function CreateNewAutocompleteGroup < T > ( placeholder , key , value , keys , parent = '' , completion = true , searchLength = 2 ) { const group = new AutocompleteGroup ( ) ; group . key = key ; group . keys = keys ; group . placeholder = placeholder ; group . parent = parent ; group . completion = completion ; group . searchLength = searchLength ; /**
     * Initial value needed, if we empty search box or want to clear it, it needs to be reset.
     * Setting copy, used if user wants to remove values (by id.). This _ list gets filtered.
     */ group . SetValues ( value ) ; return group ; }