---
source: framec/tests/typescript_snapshots.rs
expression: "compile_fixture(\"02_hsm\", \"typescript\")"
---
class MiniHsmFrameEvent {
    public _message: string;
    public _parameters: any[];

    constructor(message: string, parameters: any[]) {
        this._message = message;
        this._parameters = parameters;
    }
}


class MiniHsmFrameContext {
    public event: MiniHsmFrameEvent;
    public _return: any;
    public _data: Record<string, any>;
    public _transitioned: boolean;

    constructor(event: MiniHsmFrameEvent, default_return: any) {
        this.event = event;
        this._return = default_return;
        this._data = {  };
        this._transitioned = false;
    }
}


class MiniHsmCompartment {
    public state: string;
    public state_args: any[];
    public state_vars: Record<string, any>;
    public enter_args: any[];
    public exit_args: any[];
    public forward_event: any;
    public parent_compartment: MiniHsmCompartment | null;

    constructor(state: string, parent_compartment: MiniHsmCompartment | null = null) {
        this.state = state;
        this.state_args = [];
        this.state_vars = {  };
        this.enter_args = [];
        this.exit_args = [];
        this.forward_event = null;
        this.parent_compartment = parent_compartment;
    }

    public copy(): MiniHsmCompartment {
        const c = new MiniHsmCompartment(this.state, this.parent_compartment);
        c.state_args = {...this.state_args};
        c.state_vars = {...this.state_vars};
        c.enter_args = {...this.enter_args};
        c.exit_args = {...this.exit_args};
        c.forward_event = this.forward_event;
        return c;
    }
}


export class MiniHsm {
    private _state_stack: Array<any>;
    private __compartment: MiniHsmCompartment;
    private __next_compartment: MiniHsmCompartment | null;
    private _context_stack: Array<any>;
    private awakes: i32 = 0;
    private sleeps: i32 = 0;
    private last: i32 = 0;

    constructor() {
        this._state_stack = [];
        this._context_stack = [];
        this.__compartment = this.__prepareEnter("Live", [], []);
        this.__next_compartment = null;
    }

    _frame_init(): void {
        const __e = new MiniHsmFrameEvent("$>", this.__compartment.enter_args);
        const __ctx = new MiniHsmFrameContext(__e, null);
        this._context_stack.push(__ctx);
        this.__kernel(__e);
        this._context_stack.pop();
    }

    static _create(): MiniHsm {
        const c = new MiniHsm();
        c._frame_init();
        return c;
    }

    static readonly _HSM_CHAIN: Record<string, string[]> = {
        "Live": ["Live"],
        "Awake": ["Live", "Awake"],
        "Asleep": ["Live", "Asleep"],
    };
    private __prepareEnter(leaf: string, state_args: any[], enter_args: any[]): MiniHsmCompartment {
        let comp: MiniHsmCompartment | null = null;
        for (const name of MiniHsm._HSM_CHAIN[leaf]) {
            const new_comp = new MiniHsmCompartment(name);
            new_comp.state_args = [...state_args];
            new_comp.enter_args = [...enter_args];
            new_comp.parent_compartment = comp;
            comp = new_comp;
        }
        return comp!;
    }

    private __prepareExit(exit_args: any[]) {
        let comp: MiniHsmCompartment | null = this.__compartment;
        while (comp !== null) {
            comp.exit_args = [...exit_args];
            comp = comp.parent_compartment;
        }
    }

    private __kernel(__e: MiniHsmFrameEvent) {
        // Route event to current state.
        this.__router(__e);
        // Drain any transitions queued by the handler.
        while (this.__next_compartment !== null) {
            const next_compartment = this.__next_compartment;
            this.__next_compartment = null;
            // Exit the current (leaf) state.
            const exit_event = new MiniHsmFrameEvent("<$", this.__compartment.exit_args);
            this.__router(exit_event);
            // Switch to the new compartment.
            this.__compartment = next_compartment;
            // Three-branch forward-event handling.
            const forward_event = next_compartment.forward_event;
            next_compartment.forward_event = null;
            if (forward_event === null) {
                // No forwarded event — synthesize a fresh $>.
                const enter_event = new MiniHsmFrameEvent("$>", this.__compartment.enter_args);
                this.__router(enter_event);
            } else if (forward_event._message === "$>") {
                // Forwarded event IS $> — dispatch directly so the
                // destination's $> handler receives the caller's payload.
                this.__router(forward_event);
            } else {
                // Forwarded event is not $> — initialize the destination
                // with a fresh $>, then dispatch the forward.
                const enter_event = new MiniHsmFrameEvent("$>", this.__compartment.enter_args);
                this.__router(enter_event);
                this.__router(forward_event);
            }
            for (const ctx of this._context_stack) {
                ctx._transitioned = true;
            }
        }
    }

    private __router(__e: MiniHsmFrameEvent) {
        const handler_name = `_state_${this.__compartment.state}`;
        const handler = (this as any)[handler_name];
        if (handler) {
            handler.call(this, __e, this.__compartment);
        }
    }

    private __transition(next_compartment: MiniHsmCompartment) {
        this.__next_compartment = next_compartment;
    }

    public wake() {
        const __e = new MiniHsmFrameEvent("wake", []);
        const __ctx = new MiniHsmFrameContext(__e, null);
        this._context_stack.push(__ctx);
        this.__kernel(__e);
        this._context_stack.pop();
    }

    public sleep() {
        const __e = new MiniHsmFrameEvent("sleep", []);
        const __ctx = new MiniHsmFrameContext(__e, null);
        this._context_stack.push(__ctx);
        this.__kernel(__e);
        this._context_stack.pop();
    }

    public signal() {
        const __e = new MiniHsmFrameEvent("signal", []);
        const __ctx = new MiniHsmFrameContext(__e, null);
        this._context_stack.push(__ctx);
        this.__kernel(__e);
        this._context_stack.pop();
    }

    private _state_Live(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {
        if (__e._message === "signal") {
            this._s_Live_hdl_user_signal(__e, compartment);
            return;
        }
        if (__e._message === "sleep") {
            this._s_Live_hdl_user_sleep(__e, compartment);
            return;
        }
        if (__e._message === "wake") {
            this._s_Live_hdl_user_wake(__e, compartment);
            return;
        }
    }

    private _state_Awake(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {
        if (__e._message === "$>") {
            this._s_Awake_hdl_frame_enter(__e, compartment);
            return;
        }
        if (__e._message === "signal") {
            this._s_Awake_hdl_user_signal(__e, compartment);
            return;
        }
    }

    private _state_Asleep(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {
        if (__e._message === "$>") {
            this._s_Asleep_hdl_frame_enter(__e, compartment);
            return;
        }
        if (__e._message === "signal") {
            this._s_Asleep_hdl_user_signal(__e, compartment);
            return;
        }
        if (__e._message === "wake") {
            this._s_Asleep_hdl_user_wake(__e, compartment);
            return;
        }
    }

    private _s_Live_hdl_user_signal(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {

    }

    private _s_Live_hdl_user_sleep(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {

    }

    private _s_Live_hdl_user_wake(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {

    }

    private _s_Awake_hdl_frame_enter(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {
        self.awakes = self.awakes + 1
    }

    private _s_Awake_hdl_user_signal(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {
        self.last = 1;
        this._state_Live(__e, compartment.parent_compartment!);
    }

    private _s_Asleep_hdl_frame_enter(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {
        self.sleeps = self.sleeps + 1
    }

    private _s_Asleep_hdl_user_signal(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {
        self.last = 2;
        this._state_Live(__e, compartment.parent_compartment!);
    }

    private _s_Asleep_hdl_user_wake(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {
        const __compartment = this.__prepareEnter("Awake", [], []);
        this.__transition(__compartment);
        return;
    }
}
