import { TimeRange , Filter , Query , opensearchFilters , QueryState , RefreshInterval , } from '../../data/public' ; import { setStateToOsdUrl } from '../../opensearch_dashboards_utils/public' ; import { UrlGeneratorsDefinition } from '../../share/public' ; export const DISCOVER_APP_URL_GENERATOR = 'DISCOVER_APP_URL_GENERATOR' ; export interface DiscoverUrlGeneratorState { /**
     * Optionally set saved search ID.
     */ savedSearchId ? : string ; /**
     * Optionally set index pattern ID.
     */ indexPatternId ? : string ; /**
     * Optionally set the time range in the time picker.
     */ timeRange ? : TimeRange ; /**
     * Optionally set the refresh interval.
     */ refreshInterval ? : RefreshInterval ; /**
     * Optionally apply filers.
     */ filters ? : Filter [ ] ; /**
     * Optionally set a query. NOTE: if given and used in conjunction with `dashboardId`, and the
     * saved dashboard has a query saved with it, this will _replace_ that query.
     */ query ? : Query ; /**
     * If not given, will use the uiSettings configuration for `storeInSessionStorage`. useHash determines
     * whether to hash the data in the url to avoid url length issues.
     */ useHash ? : boolean ; } interface Params { appBasePath : string ; useHash : boolean ; } export class DiscoverUrlGenerator implements UrlGeneratorsDefinition < typeof DISCOVER_APP_URL_GENERATOR > { constructor ( private readonly params ) { } public readonly id = DISCOVER_APP_URL_GENERATOR ; public readonly createUrl = async ( { filters , indexPatternId , query , refreshInterval , savedSearchId , timeRange , useHash = this . params . useHash , } ) : Promise < string > => { const savedSearchPath = savedSearchId ? encodeURIComponent ( savedSearchId ) : '' ; const appState = { } ; const queryState = { } ; if ( query ) appState . query = query ; if ( filters && filters . length ) appState . filters = filters ?. filter ( ( f ) => ! opensearchFilters . isFilterPinned ( f ) ) ; if ( indexPatternId ) appState . index = indexPatternId ; if ( timeRange ) queryState . time = timeRange ; if ( filters && filters . length ) queryState . filters = filters ?. filter ( ( f ) => opensearchFilters . isFilterPinned ( f ) ) ; if ( refreshInterval ) queryState . refreshInterval = refreshInterval ; let url = `${ this . params . appBasePath }#/${ savedSearchPath }` ; url = setStateToOsdUrl < QueryState > ( '_g' , queryState , { useHash } , url ) ; url = setStateToOsdUrl ( '_a' , appState , { useHash } , url ) ; return url ; } ; }