import { GuardPredicate , StateMachine , EventObject , MachineConfig , Machine , DoneInvokeEvent , StateNodeConfig , assign , ActionFunction , ActionFunctionMap } from 'xstate' ; import { Store } from '../store' ; import { MessagingServiceInterface } from '../messaging' ; export function createMockGuard ( guardName ) { return { name : guardName , predicate : ( ) => true , type : 'xstate.guard' } ; } export type MachineFactory < I , E extends EventObject > = ( store : Store , context ? : I ) => StateMachine < I , any , E > ; type Options = ( store : Store ) => any ; type Config < T > = MachineConfig < T , any , any > ; export const connectToStore = < T > ( config , options ) => ( store , context ? ) => Machine ( config ) . withConfig ( options ( store ) , context ) ; export function getDataAndInvoke2 < T > ( data , src , onDone ? , id ? ) { return { initial : data , states : { [ data ] : { invoke : { src : data , onDone : src } } , [ src ] : { invoke : { id , src , data : ( _ , { data } ) => data , onDone : 'done' , autoForward : true } } , done : { type : 'final' as const } } , onDone } ; } export const assignError = assign ( { error : ( _ , event ) => event . data . message } ) ; export const debugAction = ( c , e , { state } ) => { debugger ; } ; export const displayUI = ( messagingService ) : ActionFunction < any , any > => ( ) => messagingService . sendDisplayMessage ( 'Show' ) ; export const hideUI = ( messagingService ) : ActionFunction < any , any > => ( ) => messagingService . sendDisplayMessage ( 'Hide' ) ; export const sendUserDeclinedResponse = ( messageService ) : ActionFunction < any , any > => context => { if ( ! context . requestId ) { throw new Error ( `No request id in context ${ JSON . stringify ( context ) }` ) ; } messageService . sendError ( context . requestId , { code : 200 , message : 'User declined' } ) ; } ; export const sendGenericResponse = ( messageService ) : ActionFunction < any , any > => context => { if ( ! context . requestId ) { throw new Error ( `No request id in context ${ JSON . stringify ( context ) }` ) ; } messageService . sendError ( context . requestId , { code : 200 , message : 'User declined' } ) ; } ; export enum CommonActions { sendUserDeclinedErrorResponse = 'sendUserDeclinedErrorResponse' , hideUI = 'hideUI' , displayUI = 'displayUI' } export interface CommonWorkflowActions extends ActionFunctionMap < any , any > { sendUserDeclinedErrorResponse : ActionFunction < { requestId : number ; } , any > ; hideUI : ActionFunction < any , any > ; displayUI : ActionFunction < any , any > ; } export const commonWorkflowActions = ( messageService ) : CommonWorkflowActions => ( { displayUI : displayUI ( messageService ) , hideUI : hideUI ( messageService ) , sendUserDeclinedErrorResponse : sendUserDeclinedResponse ( messageService ) } ) ;