import { forOwn , isFunction , memoize , identity } from 'lodash' ; import { FieldFormatsGetConfigFn , FieldFormatConfig , FIELD_FORMAT_IDS , FieldFormatInstanceType , FieldFormatId , IFieldFormatMetaParams , IFieldFormat , } from './types' ; import { baseFormatters } from './constants/base_formatters' ; import { FieldFormat } from './field_format' ; import { SerializedFieldFormat } from '../../../expressions/common/types' ; import { OPENSEARCH_FIELD_TYPES , OSD_FIELD_TYPES } from '../osd_field_types/types' ; import { UI_SETTINGS } from '../constants' ; import { FieldFormatNotFoundError } from '../field_formats' ; export class FieldFormatsRegistry { protected fieldFormats : Map < FieldFormatId , FieldFormatInstanceType > = new Map ( ) ; protected defaultMap : Record < string , FieldFormatConfig > = { } ; protected metaParamsOptions : Record < string , any > = { } ; protected getConfig ? : FieldFormatsGetConfigFn ; public deserialize : ( mapping : SerializedFieldFormat ) => IFieldFormat = ( ) => { return new ( FieldFormat . from ( identity ) ) ( ) ; } ; init ( getConfig , metaParamsOptions = { } , defaultFieldConverters = baseFormatters ) { const defaultTypeMap = getConfig ( UI_SETTINGS . FORMAT_DEFAULT_TYPE_MAP ) ; this . register ( defaultFieldConverters ) ; this . parseDefaultTypeMap ( defaultTypeMap ) ; this . getConfig = getConfig ; this . metaParamsOptions = metaParamsOptions ; } param { OSD_FIELD_TYPES } fieldType param { OPENSEARCH_FIELD_TYPES [ ] } esTypes return { FieldType } getDefaultConfig = ( fieldType , esTypes ? ) : FieldFormatConfig => { const type = this . getDefaultTypeName ( fieldType , esTypes ) ; return ( ( this . defaultMap && this . defaultMap [ type ] ) || { id : FIELD_FORMAT_IDS . STRING , params : { } } ) ; } ; param { FieldFormatId } formatId return { FieldFormatInstanceType | undefined } getType = ( formatId ) : FieldFormatInstanceType | undefined => { const fieldFormat = this . fieldFormats . get ( formatId ) ; if ( fieldFormat ) { const decoratedFieldFormat = this . fieldFormatMetaParamsDecorator ( fieldFormat ) ; if ( decoratedFieldFormat ) { return decoratedFieldFormat as FieldFormatInstanceType ; } } return undefined ; } ; getTypeWithoutMetaParams = ( formatId ) : FieldFormatInstanceType | undefined => { return this . fieldFormats . get ( formatId ) ; } ; param { OSD_FIELD_TYPES } fieldType param { OPENSEARCH_FIELD_TYPES [ ] } esTypes return { FieldFormatInstanceType | undefined } getDefaultType = ( fieldType , esTypes ? ) : FieldFormatInstanceType | undefined => { const config = this . getDefaultConfig ( fieldType , esTypes ) ; return this . getType ( config . id ) ; } ; param { OPENSEARCH_FIELD_TYPES [ ] } esTypes return { OPENSEARCH_FIELD_TYPES | undefined } getTypeNameByOpenSearchTypes = ( esTypes ) : OPENSEARCH_FIELD_TYPES | undefined => { if ( ! Array . isArray ( esTypes ) ) { return undefined ; } return esTypes . find ( ( type ) => this . defaultMap [ type ] && this . defaultMap [ type ] . opensearch ) ; } ; param { OSD_FIELD_TYPES } fieldType param { OPENSEARCH_FIELD_TYPES [ ] } esTypes return { OPENSEARCH_FIELD_TYPES | OSD_FIELD_TYPES } getDefaultTypeName = ( fieldType , esTypes ? ) : OPENSEARCH_FIELD_TYPES | OSD_FIELD_TYPES => { const opensearchType = this . getTypeNameByOpenSearchTypes ( esTypes ) ; return opensearchType || fieldType ; } ; param { FieldFormatId } formatId return { FieldFormat } getInstance = memoize ( ( formatId , params = { } ) : FieldFormat => { const ConcreteFieldFormat = this . getType ( formatId ) ; if ( ! ConcreteFieldFormat ) { throw new FieldFormatNotFoundError ( `Field Format '${ formatId }' not found!` , formatId ) ; } return new ConcreteFieldFormat ( params , this . getConfig ) ; } , ( formatId , params = { } ) => JSON . stringify ( { formatId , ... params , } ) ) ; param { OSD_FIELD_TYPES } fieldType param { OPENSEARCH_FIELD_TYPES [ ] } esTypes return { FieldFormat } getDefaultInstancePlain = ( fieldType , esTypes ? , params = { } ) : FieldFormat => { const conf = this . getDefaultConfig ( fieldType , esTypes ) ; const instanceParams = { ... conf . params , ... params , } ; return this . getInstance ( conf . id , instanceParams ) ; } ; param { OSD_FIELD_TYPES } fieldType param { OPENSEARCH_FIELD_TYPES [ ] } esTypes return { String } getDefaultInstanceCacheResolver ( fieldType , esTypes ? ) { return Array . isArray ( esTypes ) && esTypes . indexOf ( fieldType ) === - 1 ? [ fieldType , ... esTypes ] . join ( '-' ) : fieldType ; } param { OSD_FIELD_TYPES } fieldType return { FieldFormatInstanceType [ ] } getByFieldType ( fieldType ) { return [ ... this . fieldFormats . values ( ) ] . filter ( ( format ) => format && format . fieldType . indexOf ( fieldType ) !== - 1 ) . map ( ( format ) => this . fieldFormatMetaParamsDecorator ( format ) as FieldFormatInstanceType ) ; } param { OSD_FIELD_TYPES } fieldType param { OPENSEARCH_FIELD_TYPES [ ] } esTypes return { FieldFormat } getDefaultInstance = memoize ( this . getDefaultInstancePlain , this . getDefaultInstanceCacheResolver ) ; parseDefaultTypeMap ( value ) { this . defaultMap = value ; forOwn ( this , ( fn ) => { if ( isFunction ( fn ) && ( fn as any ) . cache ) { fn . cache = new memoize . Cache ( ) ; } } ) ; } register ( fieldFormats ) { fieldFormats . forEach ( ( fieldFormat ) => this . fieldFormats . set ( fieldFormat . id , fieldFormat ) ) ; } private param { FieldFormatInstanceType } fieldFormat return { FieldFormatInstanceType | undefined } private fieldFormatMetaParamsDecorator = ( fieldFormat ) : FieldFormatInstanceType | undefined => { const getMetaParams = ( customParams ) => this . buildMetaParams ( customParams ) ; if ( fieldFormat ) { return class DecoratedFieldFormat extends fieldFormat { static id = fieldFormat . id ; static fieldType = fieldFormat . fieldType ; constructor ( params = { } , getConfig ? ) { super ( getMetaParams ( params ) , getConfig ) ; } } ; } return undefined ; } ; param { Record < string , any > } custom return { Record < string , any > } private buildMetaParams = < T extends IFieldFormatMetaParams > ( customParams ) : T => ( { ... this . metaParamsOptions , ... customParams , } ) ; }