---
source: framec/tests/dart_snapshots.rs
expression: "compile_fixture(\"13_lifecycle_args\", \"dart\")"
---
class LifecycleArgsFrameEvent {
    late String _message;
    late List<dynamic> _parameters;

    LifecycleArgsFrameEvent(String message, List<dynamic> parameters) {
        this._message = message;
        this._parameters = parameters;
    }
}


class LifecycleArgsFrameContext {
    late LifecycleArgsFrameEvent event;
    dynamic _return;
    late Map<String, dynamic> _data;
    late bool _transitioned;

    LifecycleArgsFrameContext(LifecycleArgsFrameEvent event, dynamic default_return) {
        this.event = event;
        this._return = default_return;
        this._data = <String, dynamic>{};
        this._transitioned = false;
    }
}


class LifecycleArgsCompartment {
    late String state;
    late List<dynamic> state_args;
    late Map<String, dynamic> state_vars;
    late List<dynamic> enter_args;
    late List<dynamic> exit_args;
    dynamic forward_event;
    LifecycleArgsCompartment? parent_compartment;

    LifecycleArgsCompartment(String state, [LifecycleArgsCompartment? parent_compartment = null]) {
        this.state = state;
        this.state_args = [];
        this.state_vars = <String, dynamic>{};
        this.enter_args = [];
        this.exit_args = [];
        this.forward_event = null;
        this.parent_compartment = parent_compartment;
    }

    LifecycleArgsCompartment copy() {
        final c = LifecycleArgsCompartment(this.state, this.parent_compartment);
        c.state_args = List<dynamic>.from(this.state_args);
        c.state_vars = Map<String, dynamic>.from(this.state_vars);
        c.enter_args = List<dynamic>.from(this.enter_args);
        c.exit_args = List<dynamic>.from(this.exit_args);
        c.forward_event = this.forward_event;
        return c;
    }
}


class LifecycleArgs {
    late List<LifecycleArgsCompartment> _state_stack;
    late LifecycleArgsCompartment __compartment;
    LifecycleArgsCompartment? __next_compartment;
    late List<LifecycleArgsFrameContext> _context_stack;
    int sum = 0;
    String label = "";

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

    void _frame_init() {
        final __e = LifecycleArgsFrameEvent("\$>", __compartment.enter_args);
        final __ctx = LifecycleArgsFrameContext(__e, null);
        _context_stack.add(__ctx);
        __kernel(__e);
        _context_stack.removeLast();
    }

    static LifecycleArgs _create() {
        final c = LifecycleArgs();
        c._frame_init();
        return c;
    }

    Map<String, List<String>> hsm_chain() {
        return {
            "Idle": ["Idle"],
            "Active": ["Active"],
        };
    }
    LifecycleArgsCompartment __prepareEnter(String leaf, List<dynamic> state_args, List<dynamic> enter_args) {
        LifecycleArgsCompartment? comp = null;
        for (final name in hsm_chain()[leaf]!) {
            final new_comp = LifecycleArgsCompartment(name);
            new_comp.state_args = List<dynamic>.from(state_args);
            new_comp.enter_args = List<dynamic>.from(enter_args);
            new_comp.parent_compartment = comp;
            comp = new_comp;
        }
        return comp!;
    }

    void __prepareExit(List<dynamic> exit_args) {
        LifecycleArgsCompartment? comp = __compartment;
        while (comp != null) {
            comp.exit_args = List<dynamic>.from(exit_args);
            comp = comp.parent_compartment;
        }
    }

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

    void __router(LifecycleArgsFrameEvent __e) {
        switch (__compartment.state) {
            case "Idle":
                _state_Idle(__e, __compartment);
                break;
            case "Active":
                _state_Active(__e, __compartment);
                break;
        }
    }

    void __transition(LifecycleArgsCompartment next_compartment) {
        __next_compartment = next_compartment;
    }

    void load(int n, String label) {
        final __e = LifecycleArgsFrameEvent("load", [n, label]);
        final __ctx = LifecycleArgsFrameContext(__e, null);
        _context_stack.add(__ctx);
        try {
            __kernel(__e);
        } finally {
            _context_stack.removeLast();
        }
    }

    int total() {
        final __e = LifecycleArgsFrameEvent("total", []);
        final __ctx = LifecycleArgsFrameContext(__e, 0);
        _context_stack.add(__ctx);
        try {
            __kernel(__e);
            return _context_stack.removeLast()._return;
        } catch (__frame_err) {
            _context_stack.removeLast();
            rethrow;
        }
    }

    String tag() {
        final __e = LifecycleArgsFrameEvent("tag", []);
        final __ctx = LifecycleArgsFrameContext(__e, '');
        _context_stack.add(__ctx);
        try {
            __kernel(__e);
            return _context_stack.removeLast()._return;
        } catch (__frame_err) {
            _context_stack.removeLast();
            rethrow;
        }
    }

    void _state_Idle(LifecycleArgsFrameEvent __e, LifecycleArgsCompartment compartment) {
        if (__e._message == "load") {
            this._s_Idle_hdl_user_load(__e, compartment);
            return;
        }
    }

    void _state_Active(LifecycleArgsFrameEvent __e, LifecycleArgsCompartment compartment) {
        if (__e._message == "\$>") {
            this._s_Active_hdl_frame_enter(__e, compartment);
            return;
        }
        if (__e._message == "tag") {
            this._s_Active_hdl_user_tag(__e, compartment);
            return;
        }
        if (__e._message == "total") {
            this._s_Active_hdl_user_total(__e, compartment);
            return;
        }
    }

    void _s_Idle_hdl_user_load(LifecycleArgsFrameEvent __e, LifecycleArgsCompartment compartment) {
        final n = __e._parameters[0] as int;
        final label = __e._parameters[1] as String;
        final __compartment = this.__prepareEnter("Active", [], [n, label]);
        this.__transition(__compartment);
        return;
    }

    void _s_Active_hdl_frame_enter(LifecycleArgsFrameEvent __e, LifecycleArgsCompartment compartment) {
        final count = compartment.enter_args[0] as int;
        final name = compartment.enter_args[1] as String;
        self.sum = count + 1;
        self.label = name;
    }

    void _s_Active_hdl_user_tag(LifecycleArgsFrameEvent __e, LifecycleArgsCompartment compartment) {
        this._context_stack[this._context_stack.length - 1]._return = self.label;
                        return
    }

    void _s_Active_hdl_user_total(LifecycleArgsFrameEvent __e, LifecycleArgsCompartment compartment) {
        this._context_stack[this._context_stack.length - 1]._return = self.sum;
                        return
    }
}
