import type Mode from './GameModes/Mode';

/** Lightweight stack for managing active game modes. */
export default class ModeStack
{
    private stack: Mode[];

    public constructor()
    {
        this.stack = [];
    }

    /** Clears every mode from the stack. */
    public reset(): void
    {
        this.stack = [];
    }

    /** Current number of modes. */
    public get length(): number
    {
        return this.stack.length;
    }

    /**
     * Appends modes to the top of the stack.
     * @param {...any} modes
     */
    public push(...modes: Mode[]): void
    {
        this.stack.push(...modes);
    }

    /** Removes the active mode and re-enters the previous one. */
    public pop(): void
    {
        const currentMode = this.stack.pop();

        currentMode?.onExit();

        const previousMode = this.getCurrentMode();

        previousMode?.onEnter();
    }

    /** Returns the currently active mode. */
    public getCurrentMode(): Mode | undefined
    {
        return this.stack[this.stack.length - 1];
    }

    /** Checks whether the stack is empty. */
    public isEmpty(): boolean
    {
        return this.stack.length === 0;
    }
}
