local function I(I)return debug.traceback(I)end local function G(G)local function j(j,x)return function(h)if typeof(h)=="\102\117\110\099\116\105\111\110"then local j,s=xpcall(function()return h(x,G)end,I)if not j then x._errorReporter.reportReducerError(x:getState(),h,{message="\067\097\117\103\104\116 \101\114\114\111\114 \105\110 \116\104\117\110\107";thrownValue=s})return nil end return s end return j(h)end end return j end return G
--!strict
--[[
	A middleware that allows for functions to be dispatched with an extra
	argument for convenience. Functions will receive two arguments:
	the store itself and the extra argument provided initially to makeThunkMiddleware.

	This middleware consumes the function; middleware further down the chain
	will not receive it.
]]
local function tracebackReporter(message)
	return debug.traceback(message)
end

local function makeThunkMiddleware(extraArgument)
	local function thunkMiddleware(nextDispatch, store)
		return function(action)
			if typeof(action) == "function" then
				local ok, result = xpcall(function()
					return action(store, extraArgument)
				end, tracebackReporter)

				if not ok then
					-- report the error and move on so it's non-fatal app
					store._errorReporter.reportReducerError(store:getState(), action, {
						message = "Caught error in thunk",
						thrownValue = result,
					})
					return nil
				end

				return result
			end

			return nextDispatch(action)
		end
	end

	return thunkMiddleware
end

return makeThunkMiddleware
