local p=game:GetService("\084\119\101\101\110\083\101\114\118\105\099\101")local m=require(script.Parent.AnimatedValue)local O=require(script.Parent.Parent.Roact)local j="\084\104\101 \099\111\109\112\111\110\101\110\116 \037\113 \099\097\110\110\111\116 \098\101 \097\110\105\109\097\116\101\100 \098\101\099\097\117\115\101 \105\116 \105\115 \110\111\116 \097 \112\114\105\109\105\116\105\118\101 \099\111\109\112\111\110\101\110\116\046"local function q(q)if type(q)~="\115\116\114\105\110\103"then error(string.format(j,tostring(q)),2)end local f=O.Component:extend("\095\097\110\105\109\097\116\101\100\067\111\109\112\111\110\101\110\116\058"..q)function f.didMount(O)O:_disconnectListeners()O._listeners={}for j,q in next,O.props do if type(q)=="\116\097\098\108\101"and q._class==m then local m=nil table.insert(O._listeners,q.AnimationStarted:Connect(function(f,Y)if O._rbx then if m then m:Cancel()m=m:Destroy()end local S=p:Create(O._rbx,Y,{[j]=f})S:Play()m=S m.Completed:Connect(function(p)if p==Enum.PlaybackState.Completed then q:FinishAnimation()S=nil m=m:Destroy()end end)end end))table.insert(O._listeners,q.Changed:Connect(function(p)if O._rbx then O._rbx[j]=p end end))end end end function f.willUnmount(p)p:_disconnectListeners()end function f.render(p)local j=p.props[O.Ref]local f={}for p,O in next,p.props do if type(O)=="\116\097\098\108\101"and O._class==m then f[p]=O.Value else f[p]=O end end f[O.Ref]=function(m)p._rbx=m if j then if type(j)=="\102\117\110\099\116\105\111\110"then j(m)elseif type(j)=="\116\097\098\108\101"then j.current=m end end end return O.createElement(q,f)end function f._disconnectListeners(p)if not p._listeners then return end for p,m in ipairs(p._listeners)do m:Disconnect()end end return f end return q
-- Wraps a component to produce a new component that responds to animated values.

local TweenService = game:GetService("TweenService")
local AnimatedValue = require(script.Parent.AnimatedValue)
local Roact = require(script.Parent.Parent.Roact)

local NON_PRIMITIVE_ERROR = "The component %q cannot be animated because it is not a primitive component."

local function makeAnimatedComponent(toWrap)
	if type(toWrap) ~= "string" then
		error(string.format(NON_PRIMITIVE_ERROR, tostring(toWrap)), 2)
	end

	local wrappedComponent = Roact.Component:extend("_animatedComponent:" .. toWrap)

	function wrappedComponent:didMount()
		self:_disconnectListeners()
		self._listeners = {}

		for key, value in next, self.props do
			if type(value) == "table" and value._class == AnimatedValue then
				local _currentTween = nil

				table.insert(self._listeners, value.AnimationStarted:Connect(function(to, tweenInfo)
					if self._rbx then
						if _currentTween then
							_currentTween:Cancel()
							_currentTween = _currentTween:Destroy()
						end

						local tween = TweenService:Create(self._rbx, tweenInfo, {[key] = to})
						tween:Play()
						_currentTween = tween

						_currentTween.Completed:Connect(function(status)
							if status == Enum.PlaybackState.Completed then
								value:FinishAnimation()
								tween = nil
								_currentTween = _currentTween:Destroy()
							end
						end)
					end
				end))

				table.insert(self._listeners, value.Changed:Connect(function(new)
					if self._rbx then
						self._rbx[key] = new
					end
				end))
			end
		end
	end

	function wrappedComponent:willUnmount()
		self:_disconnectListeners()
	end

	function wrappedComponent:render()
		local ref = self.props[Roact.Ref]
		local props = {}

		for key, value in next, self.props do
			if type(value) == "table" and value._class == AnimatedValue then
				props[key] = value.Value
			else
				props[key] = value
			end
		end

		props[Roact.Ref] = function(rbx)
			self._rbx = rbx
			if ref then
				if type(ref) == "function" then
					ref(rbx)
				elseif type(ref) == "table" then
					ref.current = rbx
				end
			end
		end

		return Roact.createElement(toWrap, props)
	end

	function wrappedComponent:_disconnectListeners()
		if not self._listeners then
			return
		end

		for _, listener in ipairs(self._listeners) do
			listener:Disconnect()
		end
	end

	return wrappedComponent
end

return makeAnimatedComponent
