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

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

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

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

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

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

class ForwardCompartment {
    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 ForwardFrameEvent? forward_event;
    public ForwardCompartment? parent_compartment;

    public ForwardCompartment(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 ForwardCompartment Copy() {
        ForwardCompartment c = new ForwardCompartment(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 Forward {
    private List<ForwardCompartment> _state_stack;
    private ForwardCompartment __compartment;
    private ForwardCompartment? __next_compartment;
    private List<ForwardFrameContext> _context_stack;
    public int starts = 0;
    public int middles = 0;

    public Forward() {
        _state_stack = new List<ForwardCompartment>();
        _context_stack = new List<ForwardFrameContext>();
        this.__compartment = __prepareEnter("Start", new List<object>(), new List<object>());
        this.__next_compartment = null;
    }

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

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

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

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

    private void __router(ForwardFrameEvent __e) {
        if (__compartment.state == "Start") {
            _state_Start(__e, __compartment);
        } else if (__compartment.state == "Middle") {
            _state_Middle(__e, __compartment);
        } else if (__compartment.state == "End") {
            _state_End(__e, __compartment);
        }
    }

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

    public void run() {
        ForwardFrameEvent __e = new ForwardFrameEvent("run", new List<object>());
        ForwardFrameContext __ctx = new ForwardFrameContext(__e, null);
        _context_stack.Add(__ctx);
        __kernel(_context_stack[_context_stack.Count - 1]._event);
        _context_stack.RemoveAt(_context_stack.Count - 1);
    }

    public void finish() {
        ForwardFrameEvent __e = new ForwardFrameEvent("finish", new List<object>());
        ForwardFrameContext __ctx = new ForwardFrameContext(__e, null);
        _context_stack.Add(__ctx);
        __kernel(_context_stack[_context_stack.Count - 1]._event);
        _context_stack.RemoveAt(_context_stack.Count - 1);
    }

    private void _state_Start(ForwardFrameEvent __e, ForwardCompartment compartment) {
        if (__e._message == "run") {
            this._s_Start_hdl_user_run(__e, compartment);
            return;
        }
    }

    private void _state_Middle(ForwardFrameEvent __e, ForwardCompartment compartment) {
        if (__e._message == "finish") {
            this._s_Middle_hdl_user_finish(__e, compartment);
            return;
        }
        if (__e._message == "run") {
            this._s_Middle_hdl_user_run(__e, compartment);
            return;
        }
    }

    private void _state_End(ForwardFrameEvent __e, ForwardCompartment compartment) {
        if (__e._message == "finish") {
            this._s_End_hdl_user_finish(__e, compartment);
            return;
        }
    }

    private void _s_Start_hdl_user_run(ForwardFrameEvent __e, ForwardCompartment compartment) {
        self.starts = self.starts + 1;
        { ForwardCompartment __next = __prepareEnter("Middle", new List<object>(), new List<object>());
        __next.forward_event = __e;
        __transition(__next); }
        return;
    }

    private void _s_Middle_hdl_user_finish(ForwardFrameEvent __e, ForwardCompartment compartment) {
        { ForwardCompartment __next = __prepareEnter("End", new List<object>(), new List<object>());
        __transition(__next); }
        return;
    }

    private void _s_Middle_hdl_user_run(ForwardFrameEvent __e, ForwardCompartment compartment) {
        self.middles = self.middles + 1
    }

    private void _s_End_hdl_user_finish(ForwardFrameEvent __e, ForwardCompartment compartment) {

    }
}
