import { i18n } from '@osd/i18n' ; import { SerializedFieldFormat } from 'src/plugins/expressions/common/types' ; import { FieldFormat , FieldFormatsContentType , IFieldFormat , } from '../../../../common/field_formats' ; import { convertDateRangeToString , DateRangeKey } from '../buckets/lib/date_range' ; import { convertIPRangeToString , IpRangeKey } from '../buckets/lib/ip_range' ; type GetFieldFormat = ( mapping : SerializedFieldFormat ) => IFieldFormat ; internal export function getFormatWithAggs ( getFieldFormat ) { return ( mapping ) => { const { id , params = { } } = mapping ; const customFormats = { range : ( ) => { const RangeFormat = FieldFormat . from ( ( range ) => { const nestedFormatter = params as SerializedFieldFormat ; const format = getFieldFormat ( { id : nestedFormatter . id , params : nestedFormatter . params , } ) ; const gte = '\u2265' ; const lt = '\u003c' ; return i18n . translate ( 'data.aggTypes.buckets.ranges.rangesFormatMessage' , { defaultMessage : '{gte} {from} and {lt} {to}' , values : { gte , from : format . convert ( range . gte ) , lt , to : format . convert ( range . lt ) , } , } ) ; } ) ; return new RangeFormat ( ) ; } , date_range : ( ) => { const nestedFormatter = params as SerializedFieldFormat ; const DateRangeFormat = FieldFormat . from ( ( range ) => { const format = getFieldFormat ( { id : nestedFormatter . id , params : nestedFormatter . params , } ) ; return convertDateRangeToString ( range , format . convert . bind ( format ) ) ; } ) ; return new DateRangeFormat ( ) ; } , ip_range : ( ) => { const nestedFormatter = params as SerializedFieldFormat ; const IpRangeFormat = FieldFormat . from ( ( range ) => { const format = getFieldFormat ( { id : nestedFormatter . id , params : nestedFormatter . params , } ) ; return convertIPRangeToString ( range , format . convert . bind ( format ) ) ; } ) ; return new IpRangeFormat ( ) ; } , terms : ( ) => { const convert = ( val , type ) => { const format = getFieldFormat ( { id : params . id , params } ) ; if ( val === '__other__' ) { return params . otherBucketLabel ; } if ( val === '__missing__' ) { return params . missingBucketLabel ; } return format . convert ( val , type ) ; } ; return { convert , getConverterFor : ( type ) => ( val ) => convert ( val , type ) , } as IFieldFormat ; } , } ; if ( ! id || ! ( id in customFormats ) ) { return getFieldFormat ( mapping ) ; } return customFormats [ id ] ( ) ; } ; }