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

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


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

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


class ConstsCompartment {
    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: ConstsCompartment | null;

    constructor(state: string, parent_compartment: ConstsCompartment | 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(): ConstsCompartment {
        const c = new ConstsCompartment(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 Consts {
    private _state_stack: Array<any>;
    private __compartment: ConstsCompartment;
    private __next_compartment: ConstsCompartment | null;
    private _context_stack: Array<any>;
    private step: number = 5;
    private limit: number = 20;
    private count: number = 0;

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

    _frame_init(step: number = 5, limit: number = 20): void {
        this.step = step;
        this.limit = limit;
        const __e = new ConstsFrameEvent("$>", this.__compartment.enter_args);
        const __ctx = new ConstsFrameContext(__e, null);
        this._context_stack.push(__ctx);
        this.__kernel(__e);
        this._context_stack.pop();
    }

    static _create(step: number = 5, limit: number = 20): Consts {
        const c = new Consts();
        c._frame_init(step, limit);
        return c;
    }

    static readonly _HSM_CHAIN: Record<string, string[]> = {
        "Running": ["Running"],
    };
    private __prepareEnter(leaf: string, state_args: any[], enter_args: any[]): ConstsCompartment {
        let comp: ConstsCompartment | null = null;
        for (const name of Consts._HSM_CHAIN[leaf]) {
            const new_comp = new ConstsCompartment(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: ConstsCompartment | null = this.__compartment;
        while (comp !== null) {
            comp.exit_args = [...exit_args];
            comp = comp.parent_compartment;
        }
    }

    private __kernel(__e: ConstsFrameEvent) {
        // 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 ConstsFrameEvent("<$", 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 ConstsFrameEvent("$>", 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 ConstsFrameEvent("$>", 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: ConstsFrameEvent) {
        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: ConstsCompartment) {
        this.__next_compartment = next_compartment;
    }

    public tick() {
        const __e = new ConstsFrameEvent("tick", []);
        const __ctx = new ConstsFrameContext(__e, null);
        this._context_stack.push(__ctx);
        try {
            this.__kernel(__e);
        } finally {
            this._context_stack.pop();
        }
    }

    public get_count(): number {
        const __e = new ConstsFrameEvent("get_count", []);
        const __ctx = new ConstsFrameContext(__e, null);
        this._context_stack.push(__ctx);
        try {
            this.__kernel(__e);
            return this._context_stack.pop()!._return;
        } catch (__frame_err) {
            this._context_stack.pop();
            throw __frame_err;
        }
    }

    private _state_Running(__e: ConstsFrameEvent, compartment: ConstsCompartment) {
        if (__e._message === "get_count") {
            this._s_Running_hdl_user_get_count(__e, compartment);
            return;
        }
        if (__e._message === "tick") {
            this._s_Running_hdl_user_tick(__e, compartment);
            return;
        }
    }

    private _s_Running_hdl_user_get_count(__e: ConstsFrameEvent, compartment: ConstsCompartment) {
        this._context_stack[this._context_stack.length - 1]._return = self.count;
    }

    private _s_Running_hdl_user_tick(__e: ConstsFrameEvent, compartment: ConstsCompartment) {
        self.count = self.count + self.step;
        if self.count >= self.limit {
            self.count = 0;
        }
    }
}
