import { SavedObjectReference } from '../../../../../../src/core/types' ; import { PersistableStateService } from '../../../../../../src/plugins/kibana_utils/common' ; import { SerializedAction , SerializedEvent } from '../../../../ui_actions_enhanced/common' ; import { DrilldownConfig } from './types' ; type DashboardDrilldownPersistableState = PersistableStateService < SerializedEvent > ; const generateRefName = ( state , id ) => `drilldown:${ id }:${ state . eventId }:dashboardId` ; const injectDashboardId = ( state , dashboardId ) : SerializedEvent => { return { ... state , action : { ... state . action , config : { ... state . action . config , dashboardId , } , } , } ; } ; export const createInject = ( { drilldownId , } ) : DashboardDrilldownPersistableState [ 'inject' ] => { return ( state , references ) => { const action = state . action as SerializedAction < DrilldownConfig > ; const refName = generateRefName ( state , drilldownId ) ; const ref = references . find ( ( r ) => r . name === refName ) ; if ( ! ref ) return state ; if ( ref . id && ref . id === action . config . dashboardId ) return state ; return injectDashboardId ( state , ref . id ) ; } ; } ; export const createExtract = ( { drilldownId , } ) : DashboardDrilldownPersistableState [ 'extract' ] => { return ( state ) => { const action = state . action as SerializedAction < DrilldownConfig > ; const references = action . config . dashboardId ? [ { name : generateRefName ( state , drilldownId ) , type : 'dashboard' , id : action . config . dashboardId , } , ] : [ ] ; const { dashboardId , ... restOfConfig } = action . config ; return { state : { ... state , action : { ... state . action , config : restOfConfig , } as unknown as SerializedAction , } , references , } ; } ; } ;