---
source: framec/tests/lua_snapshots.rs
expression: "compile_fixture(\"13_lifecycle_args\", \"lua\")"
---
local LifecycleArgsFrameEvent = {}
LifecycleArgsFrameEvent.__index = LifecycleArgsFrameEvent

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


local LifecycleArgsFrameContext = {}
LifecycleArgsFrameContext.__index = LifecycleArgsFrameContext

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


local LifecycleArgsCompartment = {}
LifecycleArgsCompartment.__index = LifecycleArgsCompartment

function LifecycleArgsCompartment.new(state, parent_compartment)
    local self = setmetatable({}, LifecycleArgsCompartment)
    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 LifecycleArgsCompartment:copy()
    local c = LifecycleArgsCompartment.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 LifecycleArgs = {}
LifecycleArgs.__index = LifecycleArgs

function LifecycleArgs.new()
    local self = setmetatable({}, LifecycleArgs)
    self._state_stack = {}
    self._context_stack = {}
    self.sum = 0
    self.label = ""
    self.__compartment = self:__prepareEnter("Idle", nil, nil)
    self.__next_compartment = nil
    return self
end

function LifecycleArgs:_frame_init()
    local __e = LifecycleArgsFrameEvent.new("$>", self.__compartment.enter_args)
    local __ctx = LifecycleArgsFrameContext.new(__e, nil)
    self._context_stack[#self._context_stack + 1] = __ctx
    self:__kernel(__e)
    table.remove(self._context_stack)
end

function LifecycleArgs._create()
    local c = LifecycleArgs.new()
    c:_frame_init()
    return c
end

function LifecycleArgs:hsm_chain()
    local t = {}
    t["Idle"] = {"Idle"}
    t["Active"] = {"Active"}
    return t
end
function LifecycleArgs:__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 = LifecycleArgsCompartment.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 LifecycleArgs:__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 LifecycleArgs:__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 = LifecycleArgsFrameEvent.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 = LifecycleArgsFrameEvent.new("$>", self.__compartment.enter_args)
            self:__router(enter_event)
        elseif forward_event._message == "$>" then
            self:__router(forward_event)
        else
            local enter_event = LifecycleArgsFrameEvent.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 LifecycleArgs:__router(__e)
    local handler = self["_state_" .. self.__compartment.state]
    if handler then
        handler(self, __e, self.__compartment)
    end
end

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

function LifecycleArgs:load(n, label)
    local __e = LifecycleArgsFrameEvent.new("load", {n, label})
    local __ctx = LifecycleArgsFrameContext.new(__e, nil)
    self._context_stack[#self._context_stack + 1] = __ctx
    local __ok, __err = pcall(self.__kernel, self, __e)
    if not __ok then
        self._context_stack[#self._context_stack] = nil
        error(__err)
    end
    return table.remove(self._context_stack)._return
end

function LifecycleArgs:total()
    local __e = LifecycleArgsFrameEvent.new("total", {})
    local __ctx = LifecycleArgsFrameContext.new(__e, nil)
    self._context_stack[#self._context_stack + 1] = __ctx
    local __ok, __err = pcall(self.__kernel, self, __e)
    if not __ok then
        self._context_stack[#self._context_stack] = nil
        error(__err)
    end
    return table.remove(self._context_stack)._return
end

function LifecycleArgs:tag()
    local __e = LifecycleArgsFrameEvent.new("tag", {})
    local __ctx = LifecycleArgsFrameContext.new(__e, nil)
    self._context_stack[#self._context_stack + 1] = __ctx
    local __ok, __err = pcall(self.__kernel, self, __e)
    if not __ok then
        self._context_stack[#self._context_stack] = nil
        error(__err)
    end
    return table.remove(self._context_stack)._return
end

function LifecycleArgs:_state_Idle(__e, compartment)
    if __e._message == "load" then
        self:_s_Idle_hdl_user_load(__e, compartment)
        return
    end
end

function LifecycleArgs:_state_Active(__e, compartment)
    if __e._message == "$>" then
        self:_s_Active_hdl_frame_enter(__e, compartment)
        return
    end
    if __e._message == "tag" then
        self:_s_Active_hdl_user_tag(__e, compartment)
        return
    end
    if __e._message == "total" then
        self:_s_Active_hdl_user_total(__e, compartment)
        return
    end
end

function LifecycleArgs:_s_Idle_hdl_user_load(__e, compartment)
    local n = __e._parameters[1]
    local label = __e._parameters[2]
    local __compartment = self:__prepareEnter("Active", nil, table.pack(n, label))
    self:__transition(__compartment)
    return
end

function LifecycleArgs:_s_Active_hdl_frame_enter(__e, compartment)
    local count = compartment.enter_args[1]
    local name = compartment.enter_args[2]
    self.sum = count + 1;
    self.label = name;
end

function LifecycleArgs:_s_Active_hdl_user_tag(__e, compartment)
    self._context_stack[#self._context_stack]._return = self.label
                    return
end

function LifecycleArgs:_s_Active_hdl_user_total(__e, compartment)
    self._context_stack[#self._context_stack]._return = self.sum
                    return
end
