import { i18n } from '@kbn/i18n' ; import { getCoreStart , getDataStart } from '../../services' ; import { ROUTES } from '../../../common/constants' ; import type { SanitizedFieldType , IndexPatternValue } from '../../../common/types' ; import { getIndexPatternKey } from '../../../common/index_patterns_utils' ; import { toSanitizedFieldType } from '../../../common/fields_utils' ; export type VisFields = Record < string , SanitizedFieldType [ ] > ; export async function fetchFields ( indexes = [ ] , signal ? ) { const patterns = Array . isArray ( indexes ) ? indexes : [ indexes ] ; const coreStart = getCoreStart ( ) ; const dataStart = getDataStart ( ) ; const defaultIndex = coreStart . uiSettings . get ( 'defaultIndex' ) ; try { const indexFields = await Promise . all ( patterns . map ( async ( pattern ) => { if ( typeof pattern !== 'string' && pattern ?. id ) { return toSanitizedFieldType ( ( await dataStart . indexPatterns . get ( pattern . id ) ) . getNonScriptedFields ( ) ) ; } else { return coreStart . http . get ( ROUTES . FIELDS , { query : { index : `${ pattern ?? '' }` , } , signal , } ) ; } } ) ) ; const fields = patterns . reduce ( ( cumulatedFields , currentPattern , index ) => { const key = getIndexPatternKey ( currentPattern ) ; return { ... cumulatedFields , [ key ] : indexFields [ index ] , ... ( key === defaultIndex ? { '' : indexFields [ index ] } : { } ) , } ; } , { } ) ; return fields ; } catch ( error ) { if ( error . name !== 'AbortError' ) { getCoreStart ( ) . notifications . toasts . addDanger ( { title : i18n . translate ( 'visTypeTimeseries.fetchFields.loadIndexPatternFieldsErrorMessage' , { defaultMessage : 'Unable to load index_pattern fields' , } ) , text : error . message , } ) ; } } return { } ; }