---
source: framec/tests/kotlin_snapshots.rs
expression: "compile_fixture(\"02_hsm\", \"kotlin\")"
---
class MiniHsmFrameEvent(val _message: String, val _parameters: MutableList<Any?> = mutableListOf())

class MiniHsmFrameContext(val _event: MiniHsmFrameEvent, var _return: Any? = null) {
    val _data: MutableMap<String, Any?> = mutableMapOf()
    var _transitioned: Boolean = false
}

class MiniHsmCompartment(val state: String) {
    val state_args: MutableList<Any?> = mutableListOf()
    val state_vars: MutableMap<String, Any?> = mutableMapOf()
    val enter_args: MutableList<Any?> = mutableListOf()
    val exit_args: MutableList<Any?> = mutableListOf()
    var forward_event: MiniHsmFrameEvent? = null
    var parent_compartment: MiniHsmCompartment? = null

    fun copy(): MiniHsmCompartment {
        val c = MiniHsmCompartment(this.state)
        c.state_args.addAll(this.state_args)
        c.state_vars.putAll(this.state_vars)
        c.enter_args.addAll(this.enter_args)
        c.exit_args.addAll(this.exit_args)
        c.forward_event = this.forward_event
        c.parent_compartment = this.parent_compartment
        return c
    }
}

class MiniHsm {
    private var _state_stack: MutableList<MiniHsmCompartment>
    private var __compartment: MiniHsmCompartment
    private var __next_compartment: MiniHsmCompartment?
    private var _context_stack: MutableList<MiniHsmFrameContext>
    var awakes: Int = 0
    var sleeps: Int = 0
    var last: Int = 0

    init {
        _state_stack = mutableListOf()
        _context_stack = mutableListOf()
        this.__compartment = __prepareEnter("Live", mutableListOf<Any?>(), mutableListOf<Any?>())
        this.__next_compartment = null
    }

    fun __frame_init() {
        val __e = MiniHsmFrameEvent("$>", __compartment.enter_args)
        val __ctx = MiniHsmFrameContext(__e, null)
        _context_stack.add(__ctx)
        __kernel(__e)
        _context_stack.removeAt(_context_stack.size - 1)
    }

    private fun hsm_chain(): Map<String, List<String>> {
        return mapOf(
            "Live" to listOf("Live"),
            "Awake" to listOf("Live", "Awake"),
            "Asleep" to listOf("Live", "Asleep"),
        )
    }
    private fun __prepareEnter(leaf: String, state_args: MutableList<Any?>, enter_args: MutableList<Any?>): MiniHsmCompartment {
        var comp: MiniHsmCompartment? = null
        for (name in hsm_chain()[leaf]!!) {
            val new_comp = MiniHsmCompartment(name)
            new_comp.state_args.addAll(state_args)
            new_comp.enter_args.addAll(enter_args)
            new_comp.parent_compartment = comp
            comp = new_comp
        }
        return comp!!
    }

    private fun __prepareExit(exit_args: MutableList<Any?>) {
        var comp: MiniHsmCompartment? = __compartment
        while (comp != null) {
            comp.exit_args.clear()
            comp.exit_args.addAll(exit_args)
            comp = comp.parent_compartment
        }
    }

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

    private fun __router(__e: MiniHsmFrameEvent) {
        if (__compartment.state == "Live") {
            _state_Live(__e, __compartment)
        } else if (__compartment.state == "Awake") {
            _state_Awake(__e, __compartment)
        } else if (__compartment.state == "Asleep") {
            _state_Asleep(__e, __compartment)
        }
    }

    private fun __transition(next: MiniHsmCompartment) {
        __next_compartment = next
    }

    fun wake() {
        val __e = MiniHsmFrameEvent("wake", mutableListOf())
        val __ctx = MiniHsmFrameContext(__e, null)
        _context_stack.add(__ctx)
        try {
            __kernel(_context_stack[_context_stack.size - 1]._event)
            _context_stack.removeAt(_context_stack.size - 1)
        } catch (__frame_err: Throwable) {
            _context_stack.removeAt(_context_stack.size - 1)
            throw __frame_err
        }
    }

    fun sleep() {
        val __e = MiniHsmFrameEvent("sleep", mutableListOf())
        val __ctx = MiniHsmFrameContext(__e, null)
        _context_stack.add(__ctx)
        try {
            __kernel(_context_stack[_context_stack.size - 1]._event)
            _context_stack.removeAt(_context_stack.size - 1)
        } catch (__frame_err: Throwable) {
            _context_stack.removeAt(_context_stack.size - 1)
            throw __frame_err
        }
    }

    fun signal() {
        val __e = MiniHsmFrameEvent("signal", mutableListOf())
        val __ctx = MiniHsmFrameContext(__e, null)
        _context_stack.add(__ctx)
        try {
            __kernel(_context_stack[_context_stack.size - 1]._event)
            _context_stack.removeAt(_context_stack.size - 1)
        } catch (__frame_err: Throwable) {
            _context_stack.removeAt(_context_stack.size - 1)
            throw __frame_err
        }
    }

    private fun _state_Live(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {
        if (__e._message == "signal") {
            this._s_Live_hdl_user_signal(__e, compartment)
            return
        }
        if (__e._message == "sleep") {
            this._s_Live_hdl_user_sleep(__e, compartment)
            return
        }
        if (__e._message == "wake") {
            this._s_Live_hdl_user_wake(__e, compartment)
            return
        }
    }

    private fun _state_Awake(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {
        if (__e._message == "$>") {
            this._s_Awake_hdl_frame_enter(__e, compartment)
            return
        }
        if (__e._message == "signal") {
            this._s_Awake_hdl_user_signal(__e, compartment)
            return
        }
    }

    private fun _state_Asleep(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {
        if (__e._message == "$>") {
            this._s_Asleep_hdl_frame_enter(__e, compartment)
            return
        }
        if (__e._message == "signal") {
            this._s_Asleep_hdl_user_signal(__e, compartment)
            return
        }
        if (__e._message == "wake") {
            this._s_Asleep_hdl_user_wake(__e, compartment)
            return
        }
    }

    private fun _s_Live_hdl_user_signal(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {

    }

    private fun _s_Live_hdl_user_sleep(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {

    }

    private fun _s_Live_hdl_user_wake(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {

    }

    private fun _s_Awake_hdl_frame_enter(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {
        self.awakes = self.awakes + 1
    }

    private fun _s_Awake_hdl_user_signal(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {
        self.last = 1
        _state_Live(__e, compartment.parent_compartment!!)
    }

    private fun _s_Asleep_hdl_frame_enter(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {
        self.sleeps = self.sleeps + 1
    }

    private fun _s_Asleep_hdl_user_signal(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {
        self.last = 2
        _state_Live(__e, compartment.parent_compartment!!)
    }

    private fun _s_Asleep_hdl_user_wake(__e: MiniHsmFrameEvent, compartment: MiniHsmCompartment) {
        val __compartment = __prepareEnter("Awake", mutableListOf<Any?>(), mutableListOf<Any?>())
        __transition(__compartment)
        return
    }

    companion object {
        @JvmStatic fun __create(): MiniHsm {
            val c = MiniHsm()
            c.__frame_init()
            return c
        }    }
}
