import { cloneDeep } from 'lodash' ; import { ScopedHistory , ApplicationStart , PublicAppInfo } from '../../../../../core/public' ; import { EmbeddableEditorState , isEmbeddableEditorState , EmbeddablePackageState , isEmbeddablePackageState , } from './types' ; /**
 * A wrapper around the state object in  ScopedHistory which provides
 * strongly typed helper methods for common incoming and outgoing states used by the embeddable infrastructure.
 *
 *  public export class EmbeddableStateTransfer { constructor ( private navigateToApp , private scopedHistory ? , private appList ? ) { } param appId public getAppNameFromId = ( appId ) : string | undefined => this . appList ?. get ( appId ) ?. title ; /**
     * Fetches an  EmbeddableEditorState argument from the scoped
     * history's location state.
     *
     *  param history param options . keysToRemoveAfterFetch public getIncomingEditorState ( options ? ) { return this . getIncomingState < EmbeddableEditorState > ( isEmbeddableEditorState , { keysToRemoveAfterFetch : options ?. keysToRemoveAfterFetch , } ) ; } /**
     * Fetches an  EmbeddablePackageState argument from the scoped
     * history's location state.
     *
     *  param history param options . keysToRemoveAfterFetch public getIncomingEmbeddablePackage ( options ? ) { return this . getIncomingState < EmbeddablePackageState > ( isEmbeddablePackageState , { keysToRemoveAfterFetch : options ?. keysToRemoveAfterFetch , } ) ; } /**
     * A wrapper around the  ApplicationStart . navigateToApp method which navigates to the specified appId
     * with  EmbeddableEditorState public async navigateToEditor ( appId , options ? ) { await this . navigateToWithState < EmbeddableEditorState > ( appId , options ) ; } /**
     * A wrapper around the  ApplicationStart . navigateToApp method which navigates to the specified appId
     * with  EmbeddablePackageState public async navigateToWithEmbeddablePackage ( appId , options ? ) { await this . navigateToWithState < EmbeddablePackageState > ( appId , options ) ; } private getIncomingState < IncomingStateType > ( guard , options ? ) { if ( ! this . scopedHistory ) { throw new TypeError ( 'ScopedHistory is required to fetch incoming state' ) ; } const incomingState = this . scopedHistory . location ?. state ; const castState = ! guard || guard ( incomingState ) ? ( cloneDeep ( incomingState ) as IncomingStateType ) : undefined ; if ( castState && options ?. keysToRemoveAfterFetch ) { const stateReplace = { ... ( this . scopedHistory . location . state as { [ key ] : unknown ; } ) } ; options . keysToRemoveAfterFetch . forEach ( ( key ) => { delete stateReplace [ key ] ; } ) ; this . scopedHistory . replace ( { ... this . scopedHistory . location , state : stateReplace } ) ; } return castState ; } private async navigateToWithState < OutgoingStateType = unknown > ( appId , options ? ) { const stateObject = options ?. appendToExistingState && this . scopedHistory ? { ... ( this . scopedHistory ?. location . state as { [ key ] : unknown ; } ) , ... options . state , } : options ?. state ; await this . navigateToApp ( appId , { path : options ?. path , state : stateObject } ) ; } }