import { CoreSetup , PluginInitializerContext } from 'src/core/public' ; import { QuerySuggestionGetFn } from './providers/query_suggestion_provider' ; import { getEmptyValueSuggestions , setupValueSuggestionProvider , ValueSuggestionsGetFn , } from './providers/value_suggestion_provider' ; import { ConfigSchema } from '../../config' ; export class AutocompleteService { autocompleteConfig : ConfigSchema [ 'autocomplete' ] ; constructor ( initializerContext ) { const { autocomplete } = initializerContext . config . get < ConfigSchema > ( ) ; this . autocompleteConfig = autocomplete ; } private readonly querySuggestionProviders : Map < string , QuerySuggestionGetFn > = new Map ( ) ; private getValueSuggestions ? : ValueSuggestionsGetFn ; private addQuerySuggestionProvider = ( language , provider ) : void => { if ( language && provider && this . autocompleteConfig . querySuggestions . enabled ) { this . querySuggestionProviders . set ( language , provider ) ; } } ; private getQuerySuggestions : QuerySuggestionGetFn = ( args ) => { const { language } = args ; const provider = this . querySuggestionProviders . get ( language ) ; if ( provider ) { return provider ( args ) ; } } ; private hasQuerySuggestions = ( language ) => this . querySuggestionProviders . has ( language ) ; public public setup ( core ) { this . getValueSuggestions = this . autocompleteConfig . valueSuggestions . enabled ? setupValueSuggestionProvider ( core ) : getEmptyValueSuggestions ; return { addQuerySuggestionProvider : this . addQuerySuggestionProvider , obsolete /** please use "getProvider" only from the start contract **/ getQuerySuggestions : this . getQuerySuggestions , } ; } public public start ( ) { return { getQuerySuggestions : this . getQuerySuggestions , hasQuerySuggestions : this . hasQuerySuggestions , getValueSuggestions : this . getValueSuggestions ! , } ; } internal public clearProviders ( ) { this . querySuggestionProviders . clear ( ) ; } } public export type AutocompleteSetup = ReturnType < AutocompleteService [ 'setup' ] > ; public export type AutocompleteStart = ReturnType < AutocompleteService [ 'start' ] > ;