import { SavedObjectReference } from 'src/core/types' ; import { Filter } from '../../opensearch_query/filters' ; import { SearchSourceFields } from './types' ; export const extractReferences = ( state ) : [ SearchSourceFields & { indexRefName ? : string ; } , SavedObjectReference [ ] ] => { let searchSourceFields = { ... state } ; const references = [ ] ; if ( searchSourceFields . index ) { const indexId = searchSourceFields . index . id || ( ( searchSourceFields . index as any ) as string ) ; const refName = 'kibanaSavedObjectMeta.searchSourceJSON.index' ; references . push ( { name : refName , type : 'index-pattern' , id : indexId , } ) ; searchSourceFields = { ... searchSourceFields , indexRefName : refName , index : undefined , } ; } if ( searchSourceFields . filter ) { searchSourceFields = { ... searchSourceFields , filter : ( searchSourceFields . filter as Filter [ ] ) . map ( ( filterRow , i ) => { if ( ! filterRow . meta || ! filterRow . meta . index ) { return filterRow ; } const refName = `kibanaSavedObjectMeta.searchSourceJSON.filter[${ i }].meta.index` ; references . push ( { name : refName , type : 'index-pattern' , id : filterRow . meta . index , } ) ; return { ... filterRow , meta : { ... filterRow . meta , indexRefName : refName , index : undefined , } , } ; } ) , } ; } return [ searchSourceFields , references ] ; } ;