---
source: framec/tests/csharp_snapshots.rs
expression: "compile_fixture(\"04_state_args\", \"csharp\")"
---
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

class StateArgsFrameEvent {
    public string _message;
    public List<object> _parameters;

    public StateArgsFrameEvent(string message) {
        this._message = message;
        this._parameters = new List<object>();
    }

    public StateArgsFrameEvent(string message, List<object> parameters) {
        this._message = message;
        this._parameters = parameters;
    }
}

class StateArgsFrameContext {
    public StateArgsFrameEvent _event;
    public object? _return;
    public Dictionary<string, object> _data;
    public bool _transitioned = false;

    public StateArgsFrameContext(StateArgsFrameEvent ev, object? defaultReturn = null) {
        this._event = ev;
        this._return = defaultReturn;
        this._data = new Dictionary<string, object>();
        this._transitioned = false;
    }
}

class StateArgsCompartment {
    public string state;
    public List<object> state_args;
    public Dictionary<string, object> state_vars;
    public List<object> enter_args;
    public List<object> exit_args;
    public StateArgsFrameEvent? forward_event;
    public StateArgsCompartment? parent_compartment;

    public StateArgsCompartment(string state) {
        this.state = state;
        this.state_args = new List<object>();
        this.state_vars = new Dictionary<string, object>();
        this.enter_args = new List<object>();
        this.exit_args = new List<object>();
        this.forward_event = null;
        this.parent_compartment = null;
    }

    public StateArgsCompartment Copy() {
        StateArgsCompartment c = new StateArgsCompartment(this.state);
        c.state_args = new List<object>(this.state_args);
        c.state_vars = new Dictionary<string, object>(this.state_vars);
        c.enter_args = new List<object>(this.enter_args);
        c.exit_args = new List<object>(this.exit_args);
        c.forward_event = this.forward_event;
        c.parent_compartment = this.parent_compartment;
        return c;
    }
}

public class StateArgs {
    private List<StateArgsCompartment> _state_stack;
    private StateArgsCompartment __compartment;
    private StateArgsCompartment? __next_compartment;
    private List<StateArgsFrameContext> _context_stack;

    public StateArgs() {
        _state_stack = new List<StateArgsCompartment>();
        _context_stack = new List<StateArgsFrameContext>();
        this.__compartment = __prepareEnter("Idle", new List<object>(), new List<object>());
        this.__next_compartment = null;
    }

    public void __frame_init() {
        StateArgsFrameEvent __e = new StateArgsFrameEvent("$>", __compartment.enter_args);
        StateArgsFrameContext __ctx = new StateArgsFrameContext(__e, null);
        _context_stack.Add(__ctx);
        __kernel(__e);
        _context_stack.RemoveAt(_context_stack.Count - 1);
    }

    public static StateArgs __create() {
        StateArgs c = new StateArgs();
        c.__frame_init();
        return c;
    }

    private Dictionary<string, List<string>> hsm_chain() {
        return new Dictionary<string, List<string>> {
            { "Idle", new List<string> { "Idle" } },
            { "Holding", new List<string> { "Holding" } },
        };
    }
    private StateArgsCompartment __prepareEnter(string leaf, List<object> state_args, List<object> enter_args) {
        StateArgsCompartment? comp = null;
        foreach (string name in hsm_chain()[leaf]) {
            StateArgsCompartment new_comp = new StateArgsCompartment(name);
            new_comp.state_args = new List<object>(state_args);
            new_comp.enter_args = new List<object>(enter_args);
            new_comp.parent_compartment = comp;
            comp = new_comp;
        }
        return comp!;
    }

    private void __prepareExit(List<object> exit_args) {
        StateArgsCompartment? comp = __compartment;
        while (comp != null) {
            comp.exit_args = new List<object>(exit_args);
            comp = comp.parent_compartment;
        }
    }

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

    private void __router(StateArgsFrameEvent __e) {
        if (__compartment.state == "Idle") {
            _state_Idle(__e, __compartment);
        } else if (__compartment.state == "Holding") {
            _state_Holding(__e, __compartment);
        }
    }

    private void __transition(StateArgsCompartment next) {
        __next_compartment = next;
    }

    public void load(int initial) {
        StateArgsFrameEvent __e = new StateArgsFrameEvent("load", new List<object> { initial });
        StateArgsFrameContext __ctx = new StateArgsFrameContext(__e, null);
        _context_stack.Add(__ctx);
        __kernel(_context_stack[_context_stack.Count - 1]._event);
        _context_stack.RemoveAt(_context_stack.Count - 1);
    }

    public void adjust(int delta) {
        StateArgsFrameEvent __e = new StateArgsFrameEvent("adjust", new List<object> { delta });
        StateArgsFrameContext __ctx = new StateArgsFrameContext(__e, null);
        _context_stack.Add(__ctx);
        __kernel(_context_stack[_context_stack.Count - 1]._event);
        _context_stack.RemoveAt(_context_stack.Count - 1);
    }

    public int peek() {
        StateArgsFrameEvent __e = new StateArgsFrameEvent("peek", new List<object>());
        StateArgsFrameContext __ctx = new StateArgsFrameContext(__e, null);
        _context_stack.Add(__ctx);
        __kernel(_context_stack[_context_stack.Count - 1]._event);
        var __result = (int) _context_stack[_context_stack.Count - 1]._return;
        _context_stack.RemoveAt(_context_stack.Count - 1);
        return __result;
    }

    private void _state_Idle(StateArgsFrameEvent __e, StateArgsCompartment compartment) {
        if (__e._message == "load") {
            this._s_Idle_hdl_user_load(__e, compartment);
            return;
        }
    }

    private void _state_Holding(StateArgsFrameEvent __e, StateArgsCompartment compartment) {
        int value = System.Convert.ToInt32(__compartment.state_args[0]);
        if (__e._message == "adjust") {
            this._s_Holding_hdl_user_adjust(__e, compartment);
            return;
        }
        if (__e._message == "peek") {
            this._s_Holding_hdl_user_peek(__e, compartment);
            return;
        }
    }

    private void _s_Idle_hdl_user_load(StateArgsFrameEvent __e, StateArgsCompartment compartment) {
        int initial = System.Convert.ToInt32(__e._parameters[0]);
        { StateArgsCompartment __next = __prepareEnter("Holding", new List<object> { initial }, new List<object>());
        __transition(__next); }
        return;
    }

    private void _s_Holding_hdl_user_adjust(StateArgsFrameEvent __e, StateArgsCompartment compartment) {
        int value = System.Convert.ToInt32(compartment.state_args[0]);
        int delta = System.Convert.ToInt32(__e._parameters[0]);
        { StateArgsCompartment __next = __prepareEnter("Holding", new List<object> { value + delta }, new List<object>());
        __transition(__next); }
        return;
    }

    private void _s_Holding_hdl_user_peek(StateArgsFrameEvent __e, StateArgsCompartment compartment) {
        int value = System.Convert.ToInt32(compartment.state_args[0]);
        _context_stack[_context_stack.Count - 1]._return = value;
    }
}
