import _ from 'lodash' ; import { SavedObjectAttributes , SavedObjectsClientContract } from '../../../../core/public' ; import { SavedObjectLoader } from '../../../saved_objects/public' ; import { VisTypeAlias } from '../vis_types' ; import { VisualizationsAppExtension } from '../vis_types/vis_type_alias_registry' ; /**
 * Search for visualizations and convert them into a list display-friendly format.
 */ export async function findListItems ( { visTypes , search , size , savedObjectsClient , mapSavedObjectApiHits , } ) { const extensions = visTypes . map ( ( v ) => v . appExtensions ?. visualizations ) . filter ( Boolean ) as VisualizationsAppExtension [ ] ; const extensionByType = extensions . reduce ( ( acc , m ) => { return m ! . docTypes . reduce ( ( _acc , type ) => { acc [ type ] = m ; return acc ; } , acc ) ; } , { } as { [ visType ] : VisualizationsAppExtension ; } ) ; const searchOption = ( field , ... defaults ) => _ ( extensions ) . map ( field ) . concat ( defaults ) . compact ( ) . flatten ( ) . uniq ( ) . value ( ) as string [ ] ; const searchOptions = { type : searchOption ( 'docTypes' , 'visualization' ) , searchFields : searchOption ( 'searchFields' , 'title^3' , 'description' ) , search : search ? `${ search }*` : undefined , perPage : size , page : 1 , defaultSearchOperator : 'AND' as 'AND' , } ; const { total , savedObjects } = await savedObjectsClient . find < SavedObjectAttributes > ( searchOptions ) ; return { total , hits : savedObjects . map ( ( savedObject ) => { const config = extensionByType [ savedObject . type ] ; if ( config ) { return config . toListItem ( savedObject ) ; } else { return mapSavedObjectApiHits ( savedObject ) ; } } ) , } ; }