import { SavedObjectReference } from 'src/core/types' ; import { SearchSourceFields } from './types' ; export const injectReferences = ( searchSourceFields , references ) => { const searchSourceReturnFields = { ... searchSourceFields } ; if ( searchSourceFields . indexRefName ) { const reference = references . find ( ( ref ) => ref . name === searchSourceFields . indexRefName ) ; if ( ! reference ) { throw new Error ( `Could not find reference for ${ searchSourceFields . indexRefName }` ) ; } searchSourceReturnFields . index = reference . id ; delete searchSourceReturnFields . indexRefName ; } if ( searchSourceReturnFields . filter && Array . isArray ( searchSourceReturnFields . filter ) ) { searchSourceReturnFields . filter . forEach ( ( filterRow ) => { if ( ! filterRow . meta || ! filterRow . meta . indexRefName ) { return ; } const reference = references . find ( ( ref ) => ref . name === filterRow . meta . indexRefName ) ; if ( ! reference ) { throw new Error ( `Could not find reference for ${ filterRow . meta . indexRefName }` ) ; } filterRow . meta . index = reference . id ; delete filterRow . meta . indexRefName ; } ) ; } return searchSourceReturnFields ; } ;