import _ from 'lodash' ; import type { KibanaExecutionContext } from 'src/core/public' ; import { DashboardSavedObject } from '../../saved_dashboards' ; import { getTagsFromSavedDashboard , migrateAppState } from '.' ; import { EmbeddablePackageState , ViewMode } from '../../services/embeddable' ; import { convertPanelStateToSavedDashboardPanel } from '../../../common/embeddable/embeddable_saved_object_converters' ; import { DashboardState , RawDashboardState , DashboardAppServices , DashboardContainerInput , DashboardBuildContext , } from '../../types' ; import { convertSavedPanelsToPanelMap } from './convert_dashboard_panels' ; import { deserializeControlGroupFromDashboardSavedObject } from './dashboard_control_group' ; import { ControlGroupInput } from '../../../../controls/public' ; interface SavedObjectToDashboardStateProps { version : string ; showWriteControls : boolean ; savedDashboard : DashboardSavedObject ; usageCollection : DashboardAppServices [ 'usageCollection' ] ; savedObjectsTagging : DashboardAppServices [ 'savedObjectsTagging' ] ; } interface StateToDashboardContainerInputProps { searchSessionId ? : string ; isEmbeddedExternally ? : boolean ; dashboardState : DashboardState ; savedDashboard : DashboardSavedObject ; query : DashboardBuildContext [ 'query' ] ; incomingEmbeddable ? : EmbeddablePackageState ; dashboardCapabilities : DashboardBuildContext [ 'dashboardCapabilities' ] ; executionContext ? : KibanaExecutionContext ; } interface StateToRawDashboardStateProps { version : string ; state : DashboardState ; } /**
 * Converts a dashboard saved object to a dashboard state by extracting raw state from the given Dashboard
 * Saved Object migrating the panel states to the latest version, then converting each panel from a saved
 * dashboard panel to a panel state.
 */ export const savedObjectToDashboardState = ( { version , savedDashboard , usageCollection , showWriteControls , savedObjectsTagging , } ) : DashboardState => { const rawState = migrateAppState ( { fullScreenMode : false , title : savedDashboard . title , query : savedDashboard . getQuery ( ) , filters : savedDashboard . getFilters ( ) , timeRestore : savedDashboard . timeRestore , description : savedDashboard . description || '' , tags : getTagsFromSavedDashboard ( savedDashboard , savedObjectsTagging ) , panels : savedDashboard . panelsJSON ? JSON . parse ( savedDashboard . panelsJSON ) : [ ] , viewMode : savedDashboard . id || showWriteControls ? ViewMode . EDIT : ViewMode . VIEW , options : savedDashboard . optionsJSON ? JSON . parse ( savedDashboard . optionsJSON ) : { } , } , version , usageCollection ) ; rawState . controlGroupInput = deserializeControlGroupFromDashboardSavedObject ( savedDashboard ) as ControlGroupInput ; return { ... rawState , panels : convertSavedPanelsToPanelMap ( rawState . panels ) } ; } ; /**
 * Converts a dashboard state object to dashboard container input
 */ export const stateToDashboardContainerInput = ( { dashboardCapabilities , isEmbeddedExternally , query : queryService , searchSessionId , savedDashboard , dashboardState , executionContext , } ) : DashboardContainerInput => { const { filterManager , timefilter : timefilterService } = queryService ; const { timefilter } = timefilterService ; const { controlGroupInput , expandedPanelId , fullScreenMode , description , options , viewMode , panels , query , title , } = dashboardState ; return { refreshConfig : timefilter . getRefreshInterval ( ) , filters : filterManager . getFilters ( ) , isFullScreenMode : fullScreenMode , id : savedDashboard . id || '' , dashboardCapabilities , isEmbeddedExternally , ... ( options || { } ) , controlGroupInput , searchSessionId , expandedPanelId , description , viewMode , panels , query , title , timeRange : { ... _ . cloneDeep ( timefilter . getTime ( ) ) , } , executionContext , } ; } ; /**
 * Converts a given dashboard state object to raw dashboard state. This is useful for sharing, and session restoration, as
 * they require panels to be formatted as an array.
 */ export const stateToRawDashboardState = ( { version , state , } ) : RawDashboardState => { const savedDashboardPanels = Object . values ( state . panels ) . map ( ( panel ) => convertPanelStateToSavedDashboardPanel ( panel , version ) ) ; return { ... _ . omit ( state , 'panels' ) , panels : savedDashboardPanels } ; } ;