import { uniq } from 'lodash' ; import type { Panel , IndexPatternValue , FetchedIndexPattern } from '../common/types' ; import { IndexPatternsService } from '../../../data/common' ; export const isStringTypeIndexPattern = ( indexPatternValue ) : indexPatternValue is string => typeof indexPatternValue === 'string' ; export const getIndexPatternKey = ( indexPatternValue ) => isStringTypeIndexPattern ( indexPatternValue ) ? indexPatternValue : indexPatternValue ?. id ?? '' ; export const extractIndexPatternValues = ( panel , defaultIndexId ? ) => { const patterns = [ ] ; const addIndex = ( value ? ) => { if ( value ) { patterns . push ( value ) ; } else if ( defaultIndexId ) { patterns . push ( { id : defaultIndexId } ) ; } } ; addIndex ( panel . index_pattern ) ; panel . series . forEach ( ( series ) => { if ( series . override_index_pattern ) { addIndex ( series . series_index_pattern ) ; } } ) ; if ( panel . annotations ) { panel . annotations . forEach ( ( item ) => addIndex ( item . index_pattern ) ) ; } return uniq < IndexPatternValue > ( patterns ) . sort ( ) ; } ; export const fetchIndexPattern = async ( indexPatternValue , indexPatternsService , options = { fetchKibanaIndexForStringIndexes : false , } ) : Promise < FetchedIndexPattern > => { let indexPattern ; let indexPatternString = '' ; if ( ! indexPatternValue ) { indexPattern = await indexPatternsService . getDefault ( ) ; } else { if ( isStringTypeIndexPattern ( indexPatternValue ) ) { if ( options . fetchKibanaIndexForStringIndexes ) { indexPattern = ( await indexPatternsService . find ( indexPatternValue ) ) . find ( ( index ) => index . title === indexPatternValue ) ; } if ( ! indexPattern ) { indexPatternString = indexPatternValue ; } indexPatternString = indexPatternValue ; } else if ( indexPatternValue . id ) { indexPattern = await indexPatternsService . get ( indexPatternValue . id ) ; } } return { indexPattern , indexPatternString : indexPattern ?. title ?? indexPatternString , } ; } ;