<|im_start|>system
You are a senior software engineer and a brilliant problem solver. Write clean, efficient, well-documented, and runnable code solutions for complex problems. Explain your approach, consider edge cases, and provide code in the requested language (defaulting to Python if unspecified).<|im_end|>
<|im_start|>user
Build a state machine to simulate a vending machine.
<|im_end|>
<|im_start|>assistant
// States
const IDLE = 'IDLE';
const COIN_INPUT = 'COIN_INPUT';
const SELECTION = 'SELECTION';
const DISPENSE = 'DISPENSE';
 
// Transitions
const transitions = {
    [IDLE]: [COIN_INPUT],
    [COIN_INPUT]: [SELECTION],
    [SELECTION]: [DISPENSE],
    [DISPENSE]: [IDLE]
};
 
// Actions
const ENTER_COIN = 'enter_coin';
const SELECT_ITEM = 'select_item'; 
const DISPENSE_ITEM = 'dispense_item';

// State machine
const stateMachine = createStateMachine({
    transitions: transitions,
    actions: {
        [ENTER_COIN]: (args) => enterCoin(args),
        [SELECT_ITEM]: (args) => selectItem(args),
        [DISPENSE_ITEM]: (args) => dispenseItem(args)
    }
});

// Vending machine
const vendingMachine = {
    // Initial state
    currentState: IDLE,

    // Methods
    enterCoin: (args) => stateMachine.invoke(ENTER_COIN, args),
    selectItem: (args) => stateMachine.invoke(SELECT_ITEM, args),
    dispenseItem: (args) => stateMachine.invoke(DISPENSE_ITEM, args)
};<|im_end|>
