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

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

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

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

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

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

class ConstsCompartment {
    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 ConstsFrameEvent? forward_event;
    public ConstsCompartment? parent_compartment;

    public ConstsCompartment(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 ConstsCompartment Copy() {
        ConstsCompartment c = new ConstsCompartment(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 Consts {
    private List<ConstsCompartment> _state_stack;
    private ConstsCompartment __compartment;
    private ConstsCompartment? __next_compartment;
    private List<ConstsFrameContext> _context_stack;
    public int step = 5;
    public int limit = 20;
    public int count = 0;

    public Consts() {
        _state_stack = new List<ConstsCompartment>();
        _context_stack = new List<ConstsFrameContext>();
        this.__compartment = __prepareEnter("Running", new List<object>(), new List<object>());
        this.__next_compartment = null;
    }

    public void __frame_init(int step, int limit) {
        this.step = step;
        this.limit = limit;
        ConstsFrameEvent __e = new ConstsFrameEvent("$>", __compartment.enter_args);
        ConstsFrameContext __ctx = new ConstsFrameContext(__e, null);
        _context_stack.Add(__ctx);
        __kernel(__e);
        _context_stack.RemoveAt(_context_stack.Count - 1);
    }

    public static Consts __create(int step, int limit) {
        Consts c = new Consts();
        c.__frame_init(step, limit);
        return c;
    }

    private Dictionary<string, List<string>> hsm_chain() {
        return new Dictionary<string, List<string>> {
            { "Running", new List<string> { "Running" } },
        };
    }
    private ConstsCompartment __prepareEnter(string leaf, List<object> state_args, List<object> enter_args) {
        ConstsCompartment? comp = null;
        foreach (string name in hsm_chain()[leaf]) {
            ConstsCompartment new_comp = new ConstsCompartment(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) {
        ConstsCompartment? comp = __compartment;
        while (comp != null) {
            comp.exit_args = new List<object>(exit_args);
            comp = comp.parent_compartment;
        }
    }

    private void __kernel(ConstsFrameEvent __e) {
        // Route event to current state.
        __router(__e);
        // Drain any transitions queued by the handler.
        while (__next_compartment != null) {
            ConstsCompartment next_compartment = __next_compartment!;
            __next_compartment = null;
            // Exit the current (leaf) state.
            ConstsFrameEvent exit_event = new ConstsFrameEvent("<$", __compartment.exit_args);
            __router(exit_event);
            // Switch to the new compartment.
            __compartment = next_compartment;
            // Three-branch forward-event handling.
            ConstsFrameEvent? forward_event = next_compartment.forward_event;
            next_compartment.forward_event = null;
            if (forward_event == null) {
                // No forwarded event — synthesize a fresh $>.
                ConstsFrameEvent enter_event = new ConstsFrameEvent("$>", __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.
                ConstsFrameEvent enter_event = new ConstsFrameEvent("$>", __compartment.enter_args);
                __router(enter_event);
                __router(forward_event);
            }
            foreach (var ctx in _context_stack) {
                ctx._transitioned = true;
            }
        }
    }

    private void __router(ConstsFrameEvent __e) {
        if (__compartment.state == "Running") {
            _state_Running(__e, __compartment);
        }
    }

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

    public void tick() {
        ConstsFrameEvent __e = new ConstsFrameEvent("tick", new List<object>());
        ConstsFrameContext __ctx = new ConstsFrameContext(__e, null);
        _context_stack.Add(__ctx);
        try {
            __kernel(_context_stack[_context_stack.Count - 1]._event);
            _context_stack.RemoveAt(_context_stack.Count - 1);
        } catch {
            _context_stack.RemoveAt(_context_stack.Count - 1);
            throw;
        }
    }

    public int get_count() {
        ConstsFrameEvent __e = new ConstsFrameEvent("get_count", new List<object>());
        ConstsFrameContext __ctx = new ConstsFrameContext(__e, null);
        _context_stack.Add(__ctx);
        try {
            __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;
        } catch {
            _context_stack.RemoveAt(_context_stack.Count - 1);
            throw;
        }
    }

    private void _state_Running(ConstsFrameEvent __e, ConstsCompartment compartment) {
        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 void _s_Running_hdl_user_get_count(ConstsFrameEvent __e, ConstsCompartment compartment) {
        _context_stack[_context_stack.Count - 1]._return = self.count;
    }

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