---
source: framec/tests/lua_snapshots.rs
expression: "compile_fixture(\"11_consts\", \"lua\")"
---
local ConstsFrameEvent = {}
ConstsFrameEvent.__index = ConstsFrameEvent

function ConstsFrameEvent.new(message, parameters)
    local self = setmetatable({}, ConstsFrameEvent)
    self._message = message
    self._parameters = parameters or {}
    return self
end


local ConstsFrameContext = {}
ConstsFrameContext.__index = ConstsFrameContext

function ConstsFrameContext.new(event, default_return)
    local self = setmetatable({}, ConstsFrameContext)
    self._event = event
    self._return = default_return
    self._data = {}
    self._transitioned = false
    return self
end


local ConstsCompartment = {}
ConstsCompartment.__index = ConstsCompartment

function ConstsCompartment.new(state, parent_compartment)
    local self = setmetatable({}, ConstsCompartment)
    self.state = state
    self.state_args = {}
    self.state_vars = {}
    self.enter_args = {}
    self.exit_args = {}
    self.forward_event = nil
    self.parent_compartment = parent_compartment
    return self
end

function ConstsCompartment:copy()
    local c = ConstsCompartment.new(self.state, self.parent_compartment)
    c.state_args = {}
    for k, v in pairs(self.state_args) do c.state_args[k] = v end
    c.state_vars = {}
    for k, v in pairs(self.state_vars) do c.state_vars[k] = v end
    c.enter_args = {}
    for k, v in pairs(self.enter_args) do c.enter_args[k] = v end
    c.exit_args = {}
    for k, v in pairs(self.exit_args) do c.exit_args[k] = v end
    c.forward_event = self.forward_event
    return c
end


local Consts = {}
Consts.__index = Consts

function Consts.new()
    local self = setmetatable({}, Consts)
    self._state_stack = {}
    self._context_stack = {}
    self.count = 0
    self.__compartment = self:__prepareEnter("Running", nil, nil)
    self.__next_compartment = nil
    return self
end

function Consts:_frame_init(step, limit)
    self.step = 5
    self.limit = 20
    self.step = step
    self.limit = limit
    local __e = ConstsFrameEvent.new("$>", self.__compartment.enter_args)
    local __ctx = ConstsFrameContext.new(__e, nil)
    self._context_stack[#self._context_stack + 1] = __ctx
    self:__kernel(__e)
    table.remove(self._context_stack)
end

function Consts._create(step, limit)
    local c = Consts.new()
    c:_frame_init(step, limit)
    return c
end

function Consts:hsm_chain()
    local t = {}
    t["Running"] = {"Running"}
    return t
end
function Consts:__prepareEnter(leaf, state_args, enter_args)
    state_args = state_args or {}
    enter_args = enter_args or {}
    local comp = nil
    local chain = self:hsm_chain()[leaf]
    for i = 1, #chain do
        local new_comp = ConstsCompartment.new(chain[i])
        new_comp.state_args = {}
        for j = 1, #state_args do new_comp.state_args[j] = state_args[j] end
        new_comp.enter_args = {}
        for j = 1, #enter_args do new_comp.enter_args[j] = enter_args[j] end
        new_comp.parent_compartment = comp
        comp = new_comp
    end
    return comp
end

function Consts:__prepareExit(exit_args)
    local comp = self.__compartment
    while comp ~= nil do
        comp.exit_args = {}
        for j = 1, #exit_args do comp.exit_args[j] = exit_args[j] end
        comp = comp.parent_compartment
    end
end

function Consts:__kernel(__e)
    -- Route event to current state.
    self:__router(__e)
    -- Drain any transitions queued by the handler.
    while self.__next_compartment ~= nil do
        local next_compartment = self.__next_compartment
        self.__next_compartment = nil
        local exit_event = ConstsFrameEvent.new("<$", self.__compartment.exit_args)
        self:__router(exit_event)
        self.__compartment = next_compartment
        local forward_event = next_compartment.forward_event
        next_compartment.forward_event = nil
        if forward_event == nil then
            local enter_event = ConstsFrameEvent.new("$>", self.__compartment.enter_args)
            self:__router(enter_event)
        elseif forward_event._message == "$>" then
            self:__router(forward_event)
        else
            local enter_event = ConstsFrameEvent.new("$>", self.__compartment.enter_args)
            self:__router(enter_event)
            self:__router(forward_event)
        end
        for _, ctx in ipairs(self._context_stack) do
            ctx._transitioned = true
        end
    end
end

function Consts:__router(__e)
    local handler = self["_state_" .. self.__compartment.state]
    if handler then
        handler(self, __e, self.__compartment)
    end
end

function Consts:__transition(next_compartment)
    self.__next_compartment = next_compartment
end

function Consts:tick()
    local __e = ConstsFrameEvent.new("tick", {})
    local __ctx = ConstsFrameContext.new(__e, nil)
    self._context_stack[#self._context_stack + 1] = __ctx
    self:__kernel(__e)
    return table.remove(self._context_stack)._return
end

function Consts:get_count()
    local __e = ConstsFrameEvent.new("get_count", {})
    local __ctx = ConstsFrameContext.new(__e, nil)
    self._context_stack[#self._context_stack + 1] = __ctx
    self:__kernel(__e)
    return table.remove(self._context_stack)._return
end

function Consts:_state_Running(__e, compartment)
    if __e._message == "get_count" then
        self:_s_Running_hdl_user_get_count(__e, compartment)
        return
    end
    if __e._message == "tick" then
        self:_s_Running_hdl_user_tick(__e, compartment)
        return
    end
end

function Consts:_s_Running_hdl_user_get_count(__e, compartment)
    self._context_stack[#self._context_stack]._return = self.count
end

function Consts:_s_Running_hdl_user_tick(__e, compartment)
    self.count = self.count + self.step;
    if self.count >= self.limit then
        self.count = 0;
    end
end
