import { auto } from 'angular' ; import { i18n } from '@osd/i18n' ; import { UiActionsStart } from 'src/plugins/ui_actions/public' ; import { getServices } from '../../opensearch_dashboards_services' ; import { EmbeddableFactoryDefinition , Container , ErrorEmbeddable , } from '../../../../embeddable/public' ; import { TimeRange } from '../../../../data/public' ; import { SearchInput , SearchOutput , SearchEmbeddable } from './types' ; import { SEARCH_EMBEDDABLE_TYPE } from './constants' ; interface StartServices { executeTriggerActions : UiActionsStart [ 'executeTriggerActions' ] ; isEditable : ( ) => boolean ; } export class SearchEmbeddableFactory implements EmbeddableFactoryDefinition < SearchInput , SearchOutput , SearchEmbeddable > { public readonly type = SEARCH_EMBEDDABLE_TYPE ; private $injector : auto . IInjectorService | null ; private getInjector : ( ) => Promise < auto . IInjectorService > | null ; public readonly savedObjectMetaData = { name : i18n . translate ( 'discover.savedSearch.savedObjectName' , { defaultMessage : 'Saved search' , } ) , type : 'search' , getIconForSavedObject : ( ) => 'search' , } ; constructor ( private getStartServices , getInjector ) { this . $injector = null ; this . getInjector = getInjector ; } public canCreateNew ( ) { return false ; } public isEditable = async ( ) => { return ( await this . getStartServices ( ) ) . isEditable ( ) ; } ; public getDisplayName ( ) { return i18n . translate ( 'discover.embeddable.search.displayName' , { defaultMessage : 'search' , } ) ; } public createFromSavedObject = async ( savedObjectId , input , parent ? ) : Promise < SearchEmbeddable | ErrorEmbeddable > => { if ( ! this . $injector ) { this . $injector = await this . getInjector ( ) ; } const $injector = this . $injector as auto . IInjectorService ; const $compile = $injector . get < ng . ICompileService > ( '$compile' ) ; const $rootScope = $injector . get < ng . IRootScopeService > ( '$rootScope' ) ; const filterManager = getServices ( ) . filterManager ; const url = await getServices ( ) . getSavedSearchUrlById ( savedObjectId ) ; const editUrl = getServices ( ) . addBasePath ( `/app/discover${ url }` ) ; try { const savedObject = await getServices ( ) . getSavedSearchById ( savedObjectId ) ; const indexPattern = savedObject . searchSource . getField ( 'index' ) ; const { executeTriggerActions } = await this . getStartServices ( ) ; const { SearchEmbeddable : SearchEmbeddableClass } = await import ( './search_embeddable' ) ; return new SearchEmbeddableClass ( { savedSearch : savedObject , $rootScope , $compile , editUrl , editPath : url , filterManager , editable : getServices ( ) . capabilities . discover . save as boolean , indexPatterns : indexPattern ? [ indexPattern ] : [ ] , } , input , executeTriggerActions , parent ) ; } catch ( e ) { console . error ( e ) ; return new ErrorEmbeddable ( e , input , parent ) ; } } ; public async create ( input ) { return new ErrorEmbeddable ( 'Saved searches can only be created from a saved object' , input ) ; } }