import { VEHICLE_STATUSES , VEHICLE_EVENTS , VEHICLE_STATUS , VEHICLE_EVENT } from '@mds-core/mds-types' ; const stateTransitionDict = { [ VEHICLE_STATUSES . available ] : { [ VEHICLE_EVENTS . deregister ] : VEHICLE_STATUSES . inactive , [ VEHICLE_EVENTS . agency_pick_up ] : VEHICLE_STATUSES . removed , [ VEHICLE_EVENTS . service_end ] : VEHICLE_STATUSES . unavailable , [ VEHICLE_EVENTS . trip_start ] : VEHICLE_STATUSES . trip } , [ VEHICLE_STATUSES . elsewhere ] : { [ VEHICLE_EVENTS . trip_enter ] : VEHICLE_STATUSES . trip , [ VEHICLE_EVENTS . provider_pick_up ] : VEHICLE_STATUSES . removed , [ VEHICLE_EVENTS . deregister ] : VEHICLE_STATUSES . inactive , [ VEHICLE_EVENTS . provider_drop_off ] : VEHICLE_STATUSES . available } , [ VEHICLE_STATUSES . inactive ] : { [ VEHICLE_EVENTS . register ] : VEHICLE_STATUSES . removed } , [ VEHICLE_STATUSES . removed ] : { [ VEHICLE_EVENTS . trip_enter ] : VEHICLE_STATUSES . trip , [ VEHICLE_EVENTS . provider_drop_off ] : VEHICLE_STATUSES . available , [ VEHICLE_EVENTS . deregister ] : VEHICLE_STATUSES . inactive } , [ VEHICLE_STATUSES . reserved ] : { [ VEHICLE_EVENTS . trip_start ] : VEHICLE_STATUSES . trip , [ VEHICLE_EVENTS . cancel_reservation ] : VEHICLE_STATUSES . available } , [ VEHICLE_STATUSES . trip ] : { [ VEHICLE_EVENTS . trip_leave ] : VEHICLE_STATUSES . elsewhere , [ VEHICLE_EVENTS . trip_end ] : VEHICLE_STATUSES . available } , [ VEHICLE_STATUSES . unavailable ] : { [ VEHICLE_EVENTS . service_start ] : VEHICLE_STATUSES . available , [ VEHICLE_EVENTS . deregister ] : VEHICLE_STATUSES . inactive , [ VEHICLE_EVENTS . agency_pick_up ] : VEHICLE_STATUSES . removed , [ VEHICLE_EVENTS . provider_pick_up ] : VEHICLE_STATUSES . removed } } ; const getNextState = ( currStatus , nextEvent ) : VEHICLE_STATUS | undefined => { return stateTransitionDict [ currStatus ] ?. [ nextEvent ] ; } ; const generateTransitionLabel = ( status , nextStatus , transitionEvent ) => { return `${ status } -> ${ nextStatus } [ label = ${ transitionEvent } ]` ; } ; const generateGraph = ( ) => { const graphEntries = [ ] ; const statuses = Object . values ( VEHICLE_STATUSES ) ; for ( const status of statuses ) { const eventTransitions = Object . keys ( stateTransitionDict [ status ] ) as VEHICLE_EVENT [ ] ; for ( const event of eventTransitions ) { if ( event ) { const nextStatus = stateTransitionDict [ status ] [ event ] ; if ( nextStatus ) { graphEntries . push ( `\t${ generateTransitionLabel ( status , nextStatus , event ) }` ) ; } } } } return `digraph G {\n${ graphEntries . join ( '\n' ) }\n}` ; } ; export { stateTransitionDict , getNextState , generateGraph } ;