import type { SerializableRecord } from '@kbn/utility-types' ; import { EnhancementRegistryDefinition } from '../../../../src/plugins/embeddable/server' ; import { SavedObjectReference } from '../../../../src/core/types' ; import { ActionFactory , DynamicActionsState , SerializedEvent } from './types' ; import { dynamicActionsCollector } from './telemetry/dynamic_actions_collector' ; import { dynamicActionFactoriesCollector } from './telemetry/dynamic_action_factories_collector' ; export const dynamicActionEnhancement = ( getActionFactory ) : EnhancementRegistryDefinition => { return { id : 'dynamicActions' , telemetry : ( serializableState , stats ) => { const state = serializableState as DynamicActionsState ; stats = dynamicActionsCollector ( state , stats as Record < string , number > ) ; stats = dynamicActionFactoriesCollector ( getActionFactory , state , stats ) ; return stats ; } , extract : ( state ) => { const references = [ ] ; const newState = { events : ( state as DynamicActionsState ) . events . map ( ( event ) => { const factory = getActionFactory ( event . action . factoryId ) ; const result = factory ? factory . extract ( event ) : { state : event , references : [ ] , } ; result . references . forEach ( ( r ) => references . push ( r ) ) ; return result . state ; } ) , } ; return { state : newState , references } ; } , inject : ( state , references ) => { return { events : ( state as DynamicActionsState ) . events . map ( ( event ) => { const factory = getActionFactory ( event . action . factoryId ) ; return factory ? factory . inject ( event , references ) : event ; } ) , } as DynamicActionsState ; } , } as EnhancementRegistryDefinition ; } ;