import { BehaviorSubject } from 'rxjs' ; import { skip } from 'rxjs/operators' ; import { CoreStart } from 'opensearch-dashboards/public' ; import { IStorageWrapper } from 'src/plugins/opensearch_dashboards_utils/public' ; import { Query , UI_SETTINGS } from '../../../common' ; export class QueryStringManager { private query$ : BehaviorSubject < Query > ; constructor ( private readonly storage , private readonly uiSettings ) { this . query$ = new BehaviorSubject < Query > ( this . getDefaultQuery ( ) ) ; } private getDefaultLanguage ( ) { return ( this . storage . get ( 'opensearchDashboards.userQueryLanguage' ) || this . uiSettings . get ( UI_SETTINGS . SEARCH_QUERY_LANGUAGE ) ) ; } public getDefaultQuery ( ) { return { query : '' , language : this . getDefaultLanguage ( ) , } ; } public formatQuery ( query ) { if ( ! query ) { return this . getDefaultQuery ( ) ; } else if ( typeof query === 'string' ) { return { query , language : this . getDefaultLanguage ( ) , } ; } else { return query ; } } public getUpdates$ = ( ) => { return this . query$ . asObservable ( ) . pipe ( skip ( 1 ) ) ; } ; public getQuery = ( ) : Query => { return this . query$ . getValue ( ) ; } ; param { Query } query public setQuery = ( query ) => { const curQuery = this . query$ . getValue ( ) ; if ( query ?. language !== curQuery . language || query ?. query !== curQuery . query ) { this . query$ . next ( query ) ; } } ; /**
     * Resets the query to the default one.
     */ public clearQuery = ( ) => { this . setQuery ( this . getDefaultQuery ( ) ) ; } ; } export type QueryStringContract = PublicMethodsOf < QueryStringManager > ;