import { i18n } from '@osd/i18n' ; import { OSD_FIELD_TYPES } from '../../osd_field_types/types' ; import { FieldFormat } from '../field_format' ; import { TextContextTypeConvert , FIELD_FORMAT_IDS } from '../types' ; function convertLookupEntriesToMap ( lookupEntries ) { return lookupEntries . reduce ( ( lookupMap , lookupEntry ) => { lookupMap [ lookupEntry . key ] = lookupEntry . value ; return lookupMap ; } , { } ) ; } export class StaticLookupFormat extends FieldFormat { static id = FIELD_FORMAT_IDS . STATIC_LOOKUP ; static title = i18n . translate ( 'data.fieldFormats.static_lookup.title' , { defaultMessage : 'Static lookup' , } ) ; static fieldType = [ OSD_FIELD_TYPES . STRING , OSD_FIELD_TYPES . NUMBER , OSD_FIELD_TYPES . IP , OSD_FIELD_TYPES . BOOLEAN , ] ; getParamDefaults ( ) { return { lookupEntries : [ { } ] , unknownKeyValue : null , } ; } textConvert : TextContextTypeConvert = ( val ) => { const lookupEntries = this . param ( 'lookupEntries' ) ; const unknownKeyValue = this . param ( 'unknownKeyValue' ) ; const lookupMap = convertLookupEntriesToMap ( lookupEntries ) ; return lookupMap [ val ] || unknownKeyValue || val ; } ; }