---
source: framec/tests/swift_snapshots.rs
expression: "compile_fixture(\"09_return_explicit\", \"swift\")"
---
import Foundation

class ReturnExplicitFrameEvent {
    var _message: String
    var _parameters: [Any]

    init(message: String, parameters: [Any] = []) {
        self._message = message
        self._parameters = parameters
    }
}

class ReturnExplicitFrameContext {
    var _event: ReturnExplicitFrameEvent
    var _return: Any?
    var _data: [String: Any] = [:]
    var _transitioned: Bool = false

    init(event: ReturnExplicitFrameEvent, defaultReturn: Any? = nil) {
        self._event = event
        self._return = defaultReturn
    }
}

class ReturnExplicitCompartment {
    var state: String
    var state_args: [Any] = []
    var state_vars: [String: Any] = [:]
    var enter_args: [Any] = []
    var exit_args: [Any] = []
    var forward_event: ReturnExplicitFrameEvent?
    var parent_compartment: ReturnExplicitCompartment?

    init(state: String) {
        self.state = state
    }

    func copy() -> ReturnExplicitCompartment {
        let c = ReturnExplicitCompartment(state: self.state)
        c.state_args = self.state_args
        c.state_vars = self.state_vars
        c.enter_args = self.enter_args
        c.exit_args = self.exit_args
        c.forward_event = self.forward_event
        c.parent_compartment = self.parent_compartment
        return c
    }
}

public class ReturnExplicit {
    private var _state_stack: [ReturnExplicitCompartment] = []
    private var __compartment: ReturnExplicitCompartment
    private var __next_compartment: ReturnExplicitCompartment? = nil
    private var _context_stack: [ReturnExplicitFrameContext] = []

    init() {
        _state_stack = []
        _context_stack = []
        self.__compartment = ReturnExplicit.__prepareEnter("Judging", [], [])
        self.__next_compartment = nil
    }

    func __frame_init() {
        let __e = ReturnExplicitFrameEvent(message: "$>", parameters: __compartment.enter_args)
        let __ctx = ReturnExplicitFrameContext(event: __e)
        _context_stack.append(__ctx)
        __kernel(__e)
        _context_stack.removeLast()
    }

    static func __create() -> ReturnExplicit {
        let c = ReturnExplicit()
        c.__frame_init()
        return c
    }

    private static func hsm_chain() -> [String: [String]] {
        return [
            "Judging": ["Judging"],
        ]
    }
    private static func __prepareEnter(_ leaf: String, _ state_args: [Any], _ enter_args: [Any]) -> ReturnExplicitCompartment {
        var comp: ReturnExplicitCompartment? = nil
        for name in ReturnExplicit.hsm_chain()[leaf]! {
            let new_comp = ReturnExplicitCompartment(state: name)
            new_comp.state_args = state_args
            new_comp.enter_args = enter_args
            new_comp.parent_compartment = comp
            comp = new_comp
        }
        return comp!
    }
    private func __prepareExit(_ exit_args: [Any]) {
        var comp: ReturnExplicitCompartment? = __compartment
        while comp != nil {
            comp!.exit_args = exit_args
            comp = comp!.parent_compartment
        }
    }

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

    private func __router(_ __e: ReturnExplicitFrameEvent) {
        if __compartment.state == "Judging" {
            _state_Judging(__e, __compartment)
        }
    }

    private func __transition(_ next: ReturnExplicitCompartment) {
        __next_compartment = next
    }

    public func decide(_ score: Int) -> String {
        let __e = ReturnExplicitFrameEvent(message: "decide", parameters: [score])
        let __ctx = ReturnExplicitFrameContext(event: __e, defaultReturn: "")
        _context_stack.append(__ctx)
        __kernel(_context_stack[_context_stack.count - 1]._event)
        let __result = _context_stack[_context_stack.count - 1]._return as! String
        _context_stack.removeLast()
        return __result
    }

    private func _state_Judging(_ __e: ReturnExplicitFrameEvent, _ compartment: ReturnExplicitCompartment) {
        if __e._message == "decide" {
            self._s_Judging_hdl_user_decide(__e, compartment)
            return
        }
    }

    private func _s_Judging_hdl_user_decide(_ __e: ReturnExplicitFrameEvent, _ compartment: ReturnExplicitCompartment) {
        let score = __e._parameters[0] as! Int
                        if score >= 60 {
        _context_stack[_context_stack.count - 1]._return = "pass"
                            return
                        }
        _context_stack[_context_stack.count - 1]._return = "fail"
                        return
    }
}
