import { FieldSpec } from '../../../data/common' ; import { isNestedField } from '../../../data/common' ; import { FetchedIndexPattern , SanitizedFieldType } from './types' ; import { FieldNotFoundError } from './errors' ; export const extractFieldLabel = ( fields , name , isThrowErrorOnFieldNotFound = true ) => { if ( fields . length && name ) { const field = fields . find ( ( f ) => f . name === name ) ; if ( field ) { return field . label || field . name ; } if ( isThrowErrorOnFieldNotFound ) { throw new FieldNotFoundError ( name ) ; } } return name ; } ; export function validateField ( name , index ) { if ( name && index . indexPattern ) { const field = index . indexPattern . fields . find ( ( f ) => f . name === name ) ; if ( ! field ) { throw new FieldNotFoundError ( name ) ; } } } export const toSanitizedFieldType = ( fields ) => fields . filter ( ( field ) => field . aggregatable && ! isNestedField ( field ) ) . map ( ( field ) => ( { name : field . name , label : field . customLabel ?? field . name , type : field . type , } as SanitizedFieldType ) ) ;