import { type Ref, ref } from 'vue' interface Machine { [k: string]: { [k: string]: S } } type MachineState = keyof T type MachineEvent = keyof UnionToIntersection // 🤯 https://fettblog.eu/typescript-union-to-intersection/ type UnionToIntersection = (T extends any ? (x: T) => any : never) extends ( x: infer R ) => any ? R : never /** * The `useStateMachine` function is a TypeScript function that creates a state machine and returns the * current state and a dispatch function to update the state based on events. * @param initialState - The `initialState` parameter is the initial state of the state machine. It * represents the starting point of the state machine's state. * @param machine - The `machine` parameter is an object that represents a state machine. It should * have keys that correspond to the possible states of the machine, and the values should be objects * that represent the possible events and their corresponding next states. * @returns The `useStateMachine` function returns an object with two properties: `state` and * `dispatch`. */ export function useStateMachine( initialState: MachineState, machine: M & Machine>, ) { const state = ref(initialState) as Ref> function reducer(event: MachineEvent) { // @ts-expect-error state.value is keyof M const nextState = machine[state.value][event] return nextState ?? state.value } const dispatch = (event: MachineEvent) => { state.value = reducer(event) } return { state, dispatch, } }