"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StateMachine = void 0;
const events_1 = require("events");
const types_1 = require("./types");
const transitionTable = {
    [types_1.AppState.GAME_MINIMIZED]: {
        [types_1.StateTransition.TOGGLE]: types_1.AppState.GAME_ACTIVE,
        [types_1.StateTransition.CHILD_EXIT]: types_1.AppState.EXITING,
        [types_1.StateTransition.FATAL_ERROR]: types_1.AppState.EXITING,
    },
    [types_1.AppState.GAME_ACTIVE]: {
        [types_1.StateTransition.TOGGLE]: types_1.AppState.GAME_MINIMIZED,
        [types_1.StateTransition.MANUAL_PAUSE]: types_1.AppState.GAME_PAUSED,
        [types_1.StateTransition.ESC_PAUSE]: types_1.AppState.ESC_PAUSED,
        [types_1.StateTransition.MINIMIZE]: types_1.AppState.GAME_MINIMIZED,
        [types_1.StateTransition.CHILD_EXIT]: types_1.AppState.EXITING,
        [types_1.StateTransition.FATAL_ERROR]: types_1.AppState.EXITING,
    },
    [types_1.AppState.ESC_PAUSED]: {
        [types_1.StateTransition.TOGGLE]: types_1.AppState.GAME_MINIMIZED,
        [types_1.StateTransition.RESUME]: types_1.AppState.GAME_ACTIVE,
        [types_1.StateTransition.MINIMIZE]: types_1.AppState.GAME_MINIMIZED,
        [types_1.StateTransition.CHILD_EXIT]: types_1.AppState.EXITING,
        [types_1.StateTransition.FATAL_ERROR]: types_1.AppState.EXITING,
    },
    [types_1.AppState.GAME_PAUSED]: {
        [types_1.StateTransition.TOGGLE]: types_1.AppState.GAME_ACTIVE,
        [types_1.StateTransition.RESUME]: types_1.AppState.GAME_ACTIVE,
        [types_1.StateTransition.MINIMIZE]: types_1.AppState.GAME_MINIMIZED,
        [types_1.StateTransition.CHILD_EXIT]: types_1.AppState.EXITING,
        [types_1.StateTransition.FATAL_ERROR]: types_1.AppState.EXITING,
    },
    [types_1.AppState.EXITING]: {},
};
class StateMachine extends events_1.EventEmitter {
    _state = types_1.AppState.GAME_MINIMIZED;
    get state() { return this._state; }
    get snapshot() {
        return {
            state: this._state,
            inputFocus: this.getInputFocus(),
            gameVisible: this._state === types_1.AppState.GAME_ACTIVE || this._state === types_1.AppState.GAME_PAUSED || this._state === types_1.AppState.ESC_PAUSED,
        };
    }
    transition(t) {
        const nextState = transitionTable[this._state]?.[t];
        if (nextState === undefined)
            return this._state;
        const prev = this._state;
        this._state = nextState;
        this.emit('transition', { from: prev, to: nextState, trigger: t });
        return nextState;
    }
    getInputFocus() {
        if (this._state === types_1.AppState.GAME_ACTIVE || this._state === types_1.AppState.ESC_PAUSED)
            return types_1.InputFocus.GAME;
        return types_1.InputFocus.CHILD;
    }
}
exports.StateMachine = StateMachine;
//# sourceMappingURL=state.js.map