local N={}N.WILDCARD="\042"N.DEFERRED=0 N.SUCCEEDED=1 N.NO_TRANSITION=2 N.PENDING=3 N.CANCELLED=4 local function R(N,R)if N then return N(unpack(R))end end local function s(N,s,v,g,b)local K=R(N["\111\110\095\098\101\102\111\114\101\095"..s],b)local c=R(N.on_before_event,b)if K==false or c==false then return false end end local function v(s,v,g,b,K)local c=R(s["\111\110\095\108\101\097\118\101\095"..g],K)local P=R(s.on_leave_state,K)if c==false or P==false then return false end if c==N.DEFERRED or P==N.DEFERRED then return N.DEFERRED end end local function g(N,s,v,g,b)R(N["\111\110\095\101\110\116\101\114\095"..g]or N["\111\110\095"..g],b)R(N.on_enter_state or N.on_state,b)end local function b(N,s,v,g,b)R(N["\111\110\095\097\102\116\101\114\095"..s]or N["\111\110\095"..s],b)R(N.on_after_event or N.on_event,b)end local function K(R,K,c)return function(...)local P=R.current local x=c[P]or c[N.WILDCARD]or P local p={R,K,P,x;...}assert(not R.is_pending(),"\112\114\101\118\105\111\117\115 \116\114\097\110\115\105\116\105\111\110 \115\116\105\108\108 \112\101\110\100\105\110\103")assert(R.can(K),"\105\110\118\097\108\105\100 \116\114\097\110\115\105\116\105\111\110 \102\114\111\109 \115\116\097\116\101 \'"..(P..("\' \119\105\116\104 \101\118\101\110\116 \'"..(K.."\'"))))local z=s(R,K,P,x,p)if z==false then return N.CANCELLED end if P==x then b(R,K,P,x,p)return N.NO_TRANSITION end R.confirm=function()R.confirm=nil R.cancel=nil R.current=x g(R,K,P,x,p)b(R,K,P,x,p)return N.SUCCEEDED end R.cancel=function()R.confirm=nil R.cancel=nil b(R,K,P,x,p)return N.CANCELLED end local r=v(R,K,P,x,p)if r==false then return N.CANCELLED end if r==N.DEFERRED then return N.PENDING end if R.confirm then return R.confirm()end end end function N.create(R,s)local v=s or{}local g=R.initial g=type(g)=="\115\116\114\105\110\103"and{state=g}or g local b=g and g.event or"\115\116\097\114\116\117\112"local c=R.terminal local P=R.events or{}local x=R.callbacks or{}local p={}local z={}local function r(R)local s=type(R.from)=="\116\097\098\108\101"and R.from or R.from and{R.from}or{N.WILDCARD}local v=R.to local g=R.name p[g]=p[g]or{}for N,R in ipairs(s)do z[R]=z[R]or{}table.insert(z[R],g)p[g][R]=v or R end end if g then r({name=b;from="\110\111\110\101",to=g.state})end for N,R in ipairs(P)do r(R)end for N,R in pairs(p)do v[N]=K(v,N,R)end for N,R in pairs(x)do v[N]=R end v.current="\110\111\110\101"function v.is(N)if type(N)=="\116\097\098\108\101"then for N,R in ipairs(N)do if v.current==R then return true end end return false end return v.current==N end function v.can(R)local s=p[R]local g=s[v.current]or s[N.WILDCARD]return g~=nil end function v.cannot(N)return not v.can(N)end function v.transitions()return z[v.current]end function v.is_pending()return v.confirm~=nil end function v.is_finished()return v.is(c)end if g and not g.defer then v[b]()end return v end return N
-- From https://github.com/unindented/lua-fsm/blob/master/src/fsm.lua
--[[
	Copyright (c) 2016 Daniel Perez Alvarez

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
]]
--
local Fsm = {}
Fsm.WILDCARD = "*"
Fsm.DEFERRED = 0
Fsm.SUCCEEDED = 1
Fsm.NO_TRANSITION = 2
Fsm.PENDING = 3
Fsm.CANCELLED = 4
local function do_callback(handler, args)
	if handler then
		return handler(unpack(args))
	end
end
local function before_event(self, event, _, _, args)
	local specific = do_callback(self["on_before_" .. event], args)
	local general = do_callback(self["on_before_event"], args)
	if specific == false or general == false then
		return false
	end
end
local function leave_state(self, _, from, _, args)
	local specific = do_callback(self["on_leave_" .. from], args)
	local general = do_callback(self["on_leave_state"], args)
	if specific == false or general == false then
		return false
	end
	if specific == Fsm.DEFERRED or general == Fsm.DEFERRED then
		return Fsm.DEFERRED
	end
end
local function enter_state(self, _, _, to, args)
	do_callback(self["on_enter_" .. to] or self["on_" .. to], args)
	do_callback(self["on_enter_state"] or self["on_state"], args)
end
local function after_event(self, event, _, _, args)
	do_callback(self["on_after_" .. event] or self["on_" .. event], args)
	do_callback(self["on_after_event"] or self["on_event"], args)
end
local function build_transition(self, event, states)
	return function(...)
		local from = self.current
		local to = states[from] or states[Fsm.WILDCARD] or from
		local args = { self, event, from, to, ... }
		assert(not self.is_pending(), "previous transition still pending")
		assert(self.can(event), "invalid transition from state '" .. from .. "' with event '" .. event .. "'")
		local before = before_event(self, event, from, to, args)
		if before == false then
			return Fsm.CANCELLED
		end
		if from == to then
			after_event(self, event, from, to, args)
			return Fsm.NO_TRANSITION
		end
		self.confirm = function()
			self.confirm = nil
			self.cancel = nil
			self.current = to
			enter_state(self, event, from, to, args)
			after_event(self, event, from, to, args)
			return Fsm.SUCCEEDED
		end
		self.cancel = function()
			self.confirm = nil
			self.cancel = nil
			after_event(self, event, from, to, args)
			return Fsm.CANCELLED
		end
		local leave = leave_state(self, event, from, to, args)
		if leave == false then
			return Fsm.CANCELLED
		end
		if leave == Fsm.DEFERRED then
			return Fsm.PENDING
		end
		if self.confirm then
			return self.confirm()
		end
	end
end
function Fsm.create(cfg, target)
	local self = target or {}
	-- Initial state.
	local initial = cfg.initial
	-- Allow for a string, or a map like `{state = "foo", event = "setup"}`.
	initial = type(initial) == "string" and { state = initial } or initial
	-- Initial event.
	local initial_event = initial and initial.event or "startup"
	-- Terminal state.
	local terminal = cfg.terminal
	-- Events.
	local events = cfg.events or {}
	-- Callbacks.
	local callbacks = cfg.callbacks or {}
	-- Track state transitions allowed for an event.
	local states_for_event = {}
	-- Track events allowed from a state.
	local events_for_state = {}
	local function add(e)
		-- Allow wildcard transition if `from` is not specified.
		local from = type(e.from) == "table" and e.from or (e.from and { e.from } or { Fsm.WILDCARD })
		local to = e.to
		local event = e.name
		states_for_event[event] = states_for_event[event] or {}
		for _, fr in ipairs(from) do
			events_for_state[fr] = events_for_state[fr] or {}
			table.insert(events_for_state[fr], event)
			-- Allow no-op transition if `to` is not specified.
			states_for_event[event][fr] = to or fr
		end
	end
	if initial then
		add({ name = initial_event, from = "none", to = initial.state })
	end
	for _, event in ipairs(events) do
		add(event)
	end
	for event, states in pairs(states_for_event) do
		self[event] = build_transition(self, event, states)
	end
	for name, callback in pairs(callbacks) do
		self[name] = callback
	end
	self.current = "none"
	function self.is(state)
		if type(state) == "table" then
			for _, s in ipairs(state) do
				if self.current == s then
					return true
				end
			end
			return false
		end
		return self.current == state
	end
	function self.can(event)
		local states = states_for_event[event]
		local to = states[self.current] or states[Fsm.WILDCARD]
		return to ~= nil
	end
	function self.cannot(event)
		return not self.can(event)
	end
	function self.transitions()
		return events_for_state[self.current]
	end
	function self.is_pending()
		return self.confirm ~= nil
	end
	function self.is_finished()
		return self.is(terminal)
	end
	if initial and not initial.defer then
		self[initial_event]()
	end
	return self
end
return Fsm
