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

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

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

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

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

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

class PushPopCompartment {
    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 PushPopFrameEvent? forward_event;
    public PushPopCompartment? parent_compartment;

    public PushPopCompartment(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 PushPopCompartment Copy() {
        PushPopCompartment c = new PushPopCompartment(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 PushPop {
    private List<PushPopCompartment> _state_stack;
    private PushPopCompartment __compartment;
    private PushPopCompartment? __next_compartment;
    private List<PushPopFrameContext> _context_stack;
    public int pings = 0;
    public int leaves = 0;

    public PushPop() {
        _state_stack = new List<PushPopCompartment>();
        _context_stack = new List<PushPopFrameContext>();
        this.__compartment = __prepareEnter("Idle", new List<object>(), new List<object>());
        this.__next_compartment = null;
    }

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

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

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

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

    private void __router(PushPopFrameEvent __e) {
        if (__compartment.state == "Idle") {
            _state_Idle(__e, __compartment);
        } else if (__compartment.state == "Nested") {
            _state_Nested(__e, __compartment);
        }
    }

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

    public void ping() {
        PushPopFrameEvent __e = new PushPopFrameEvent("ping", new List<object>());
        PushPopFrameContext __ctx = new PushPopFrameContext(__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 void nest() {
        PushPopFrameEvent __e = new PushPopFrameEvent("nest", new List<object>());
        PushPopFrameContext __ctx = new PushPopFrameContext(__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 void unnest() {
        PushPopFrameEvent __e = new PushPopFrameEvent("unnest", new List<object>());
        PushPopFrameContext __ctx = new PushPopFrameContext(__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 void leaf() {
        PushPopFrameEvent __e = new PushPopFrameEvent("leaf", new List<object>());
        PushPopFrameContext __ctx = new PushPopFrameContext(__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;
        }
    }

    private void _state_Idle(PushPopFrameEvent __e, PushPopCompartment compartment) {
        if (__e._message == "nest") {
            this._s_Idle_hdl_user_nest(__e, compartment);
            return;
        }
        if (__e._message == "ping") {
            this._s_Idle_hdl_user_ping(__e, compartment);
            return;
        }
    }

    private void _state_Nested(PushPopFrameEvent __e, PushPopCompartment compartment) {
        if (__e._message == "leaf") {
            this._s_Nested_hdl_user_leaf(__e, compartment);
            return;
        }
        if (__e._message == "unnest") {
            this._s_Nested_hdl_user_unnest(__e, compartment);
            return;
        }
    }

    private void _s_Idle_hdl_user_nest(PushPopFrameEvent __e, PushPopCompartment compartment) {
        _state_stack.Add(__compartment);
        { PushPopCompartment __next = __prepareEnter("Nested", new List<object>(), new List<object>());
        __transition(__next); }
        return;
    }

    private void _s_Idle_hdl_user_ping(PushPopFrameEvent __e, PushPopCompartment compartment) {
        self.pings = self.pings + 1
    }

    private void _s_Nested_hdl_user_leaf(PushPopFrameEvent __e, PushPopCompartment compartment) {
        self.leaves = self.leaves + 1
    }

    private void _s_Nested_hdl_user_unnest(PushPopFrameEvent __e, PushPopCompartment compartment) {
        var __saved = _state_stack[_state_stack.Count - 1]; _state_stack.RemoveAt(_state_stack.Count - 1);
        __transition(__saved);
        return;
    }
}
