import { StateNodeConfig , DoneInvokeEvent , TransitionConfig } from 'xstate' ; export function unreachable ( x ) { return x ; } export const exists = < T > ( t ) : t is T => ! ! t ; const throwError = ( fn , t ) => { throw new Error ( `not valid, ${ fn . name } failed on ${ t }` ) ; } ; type TypeGuard < T , S > = ( t1 : T | S ) => t1 is T ; export function checkThat < T , S = undefined > ( t , isTypeT ) { if ( ! isTypeT ( t ) ) { throwError ( isTypeT , t ) ; throw 'Unreachable' ; } return t ; } type Opts < T > = { onDone ? : string | TransitionConfig < T , any > ; id ? : string ; onError ? : string | TransitionConfig < T , any > ; entry ? : string ; exit ? : string ; } ; export function getDataAndInvoke < T , Services extends string = string > ( data , service , onDone ? ) { return { initial : data . src , states : { [ data . src ] : { invoke : { src : data . src , onDone : service . src , onError : data . opts ?. onError } } , [ service . src ] : { invoke : { id : service ?. opts ?. id , src : service . src , data : ( _ , { data } ) => data , onDone : 'done' , onError : service . opts ?. onError } , entry : service . opts ?. entry } , done : { type : 'final' } } , onDone } ; } export function arrayToRecord < T , K extends keyof T > ( array , idProperty ) { return array . reduce ( ( obj , item ) => { obj [ item [ idProperty ] ] = item ; return obj ; } , { } as any ) ; } export function recordToArray < T > ( record ) { return Object . keys ( record ) . map ( k => record [ k ] ) . filter ( e => e !== undefined ) as Array < T > ; }