import { SavedObjectReference } from '../../../../../core/types' ; import { VisParams } from '../../../common' ; import { INDEX_PATTERN_SAVED_OBJECT_TYPE } from '../../../../data/common' ; internal const REF_NAME_POSTFIX = '_ref_name' ; internal type Action = ( object : Record < string , any > , key : string ) => void ; const isMetricsVis = ( visType ) => visType === 'metrics' ; const doForExtractedIndices = ( action , visParams ) => { action ( visParams , 'index_pattern' ) ; visParams . series . forEach ( ( series ) => { if ( series . override_index_pattern ) { action ( series , 'series_index_pattern' ) ; } } ) ; if ( visParams . annotations ) { visParams . annotations . forEach ( ( annotation ) => { action ( annotation , 'index_pattern' ) ; } ) ; } } ; export const extractTimeSeriesReferences = ( visType , visParams , references = [ ] , prefix = 'metrics' ) => { let i = 0 ; if ( isMetricsVis ( visType ) ) { doForExtractedIndices ( ( object , key ) => { if ( object [ key ] && object [ key ] . id ) { const name = `${ prefix }_${ i ++ }_index_pattern` ; object [ key + REF_NAME_POSTFIX ] = name ; references . push ( { name , type : INDEX_PATTERN_SAVED_OBJECT_TYPE , id : object [ key ] . id , } ) ; delete object [ key ] ; } } , visParams ) ; } } ; export const injectTimeSeriesReferences = ( visType , visParams , references ) => { if ( isMetricsVis ( visType ) ) { doForExtractedIndices ( ( object , key ) => { const refKey = key + REF_NAME_POSTFIX ; if ( object [ refKey ] ) { const refValue = references . find ( ( ref ) => ref . name === object [ refKey ] ) ; if ( refValue ) { object [ key ] = { id : refValue . id } ; } delete object [ refKey ] ; } } , visParams ) ; } } ;