local D={boolean={springType=LinearSpring.new,toIntermediate=function(D)return{D and 1 or 0}end;fromIntermediate=function(D)return D[1]>=.5 end};number={springType=LinearSpring.new,toIntermediate=function(D)return{D}end;fromIntermediate=function(D)return D[1]end};NumberRange={springType=LinearSpring.new,toIntermediate=function(D)return{D.Min;D.Max}end,fromIntermediate=function(D)return NumberRange.new(D[1],D[2])end},UDim={springType=LinearSpring.new;toIntermediate=function(D)return{D.Scale;D.Offset}end;fromIntermediate=function(D)return UDim.new(D[1],round(D[2]))end},UDim2={springType=LinearSpring.new,toIntermediate=function(D)local d=D.X local c=D.Y return{d.Scale,d.Offset,c.Scale;c.Offset}end;fromIntermediate=function(D)return UDim2.new(D[1],round(D[2]),D[3],round(D[4]))end};Vector2={springType=LinearSpring.new;toIntermediate=function(D)return{D.X;D.Y}end,fromIntermediate=function(D)return Vector2.new(D[1],D[2])end};Vector3={springType=LinearSpring.new,toIntermediate=function(D)return{D.X;D.Y;D.Z}end,fromIntermediate=function(D)return Vector3.new(D[1],D[2],D[3])end},Color3={springType=LinearSpring.new,toIntermediate=rgbToLuv,fromIntermediate=luvToRgb};ColorSequence={springType=LinearSpring.new;toIntermediate=function(D)local d=D.Keypoints local c=rgbToLuv(d[1].Value)local J=rgbToLuv(d[#d].Value)return{c[1],c[2],c[3];J[1];J[2];J[3]}end,fromIntermediate=function(D)return ColorSequence.new(luvToRgb({D[1],D[2],D[3]}),luvToRgb({D[4];D[5],D[6]}))end}}local d={}RunService.Heartbeat:Connect(function(D)for c,J in d do for d,K in J do if K:canSleep()then J[d]=nil c[d]=K.rawTarget else c[d]=K:step(D)end end if not next(J)then d[c]=nil end end end)local function c(D,d,c,J)if not c:find(typeof(J))then error(("\098\097\100 \097\114\103\117\109\101\110\116 \035\037\100 \116\111 \037\115 \040\037\115 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041"):format(D,d,c,typeof(J)),3)end end local J={}function J.target(J,K,r,b)if STRICT_TYPES then c(1,"\115\112\114\046\116\097\114\103\101\116","\073\110\115\116\097\110\099\101",J)c(2,"\115\112\114\046\116\097\114\103\101\116","\110\117\109\098\101\114",K)c(3,"\115\112\114\046\116\097\114\103\101\116","\110\117\109\098\101\114",r)c(4,"\115\112\114\046\116\097\114\103\101\116","\116\097\098\108\101",b)end if K~=K or K<0 then error(("\101\120\112\101\099\116\101\100 \100\097\109\112\105\110\103 \114\097\116\105\111 \062\061 \048\059 \103\111\116 \037\046\050\102"):format(K),2)end if r~=r or r<0 then error(("\101\120\112\101\099\116\101\100 \117\110\100\097\109\112\101\100 \102\114\101\113\117\101\110\099\121 \062\061 \048\059 \103\111\116 \037\046\050\102"):format(r),2)end local f=d[J]if not f then f={}d[J]=f end for d,c in b do local b=J[d]if STRICT_TYPES and typeof(c)~=typeof(b)then error(("\098\097\100 \112\114\111\112\101\114\116\121 \037\115 \116\111 \115\112\114\046\116\097\114\103\101\116 \040\037\115 \101\120\112\101\099\116\101\100\044 \103\111\116 \037\115\041"):format(d,typeof(b),typeof(c)),2)end local u=f[d]if not u then local J=D[typeof(c)]if not J then error("\117\110\115\117\112\112\111\114\116\101\100 \116\121\112\101\058 "..typeof(c),2)end u=J.springType(K,r,b,J,c)f[d]=u end u.d=K u.f=r u:setGoal(c)end end function J.stop(D,J)if STRICT_TYPES then c(1,"\115\112\114\046\115\116\111\112","\073\110\115\116\097\110\099\101",D)c(2,"\115\112\114\046\115\116\111\112","\115\116\114\105\110\103\124\110\105\108",J)end if J then local c=d[D]if c then c[J]=nil end else d[D]=nil end end return table.freeze(J)

local typeMetadata = {
	boolean = {
		springType = LinearSpring.new,

		toIntermediate = function(value)
			return {value and 1 or 0}
		end,

		fromIntermediate = function(value)
			return value[1] >= 0.5
		end,
	},

	number = {
		springType = LinearSpring.new,

		toIntermediate = function(value)
			return {value}
		end,

		fromIntermediate = function(value)
			return value[1]
		end,
	},

	NumberRange = {
		springType = LinearSpring.new,

		toIntermediate = function(value)
			return {value.Min, value.Max}
		end,

		fromIntermediate = function(value)
			return NumberRange.new(value[1], value[2])
		end,
	},

	UDim = {
		springType = LinearSpring.new,

		toIntermediate = function(value)
			return {value.Scale, value.Offset}
		end,

		fromIntermediate = function(value)
			return UDim.new(value[1], round(value[2]))
		end,
	},

	UDim2 = {
		springType = LinearSpring.new,

		toIntermediate = function(value)
			local x = value.X
			local y = value.Y
			return {x.Scale, x.Offset, y.Scale, y.Offset}
		end,

		fromIntermediate = function(value)
			return UDim2.new(value[1], round(value[2]), value[3], round(value[4]))
		end,
	},

	Vector2 = {
		springType = LinearSpring.new,

		toIntermediate = function(value)
			return {value.X, value.Y}
		end,

		fromIntermediate = function(value)
			return Vector2.new(value[1], value[2])
		end,
	},

	Vector3 = {
		springType = LinearSpring.new,

		toIntermediate = function(value)
			return {value.X, value.Y, value.Z}
		end,

		fromIntermediate = function(value)
			return Vector3.new(value[1], value[2], value[3])
		end,
	},

	Color3 = {
		springType = LinearSpring.new,
		toIntermediate = rgbToLuv,
		fromIntermediate = luvToRgb,
	},
	
	-- Only takes start and end keypoints
	ColorSequence = {
		springType = LinearSpring.new,
		toIntermediate = function(value)
			local keypoints = value.Keypoints
			
			local luv0 = rgbToLuv(keypoints[1].Value)
			local luv1 = rgbToLuv(keypoints[#keypoints].Value)
			
			return {
				luv0[1], luv0[2], luv0[3],
				luv1[1], luv1[2], luv1[3],
			}
		end,

		fromIntermediate = function(value)
			return ColorSequence.new(
				luvToRgb{value[1], value[2], value[3]},
				luvToRgb{value[4], value[5], value[6]}
			)
		end,
	},
}

local springStates = {} -- {[instance] = {[property] = spring}

RunService.Heartbeat:Connect(function(dt)
	for instance, state in springStates do
		for propName, spring in state do
			if spring:canSleep() then
				state[propName] = nil
				instance[propName] = spring.rawTarget
			else
				instance[propName] = spring:step(dt)
			end
		end

		if not next(state) then
			springStates[instance] = nil
		end
	end
end)

local function assertType(argNum, fnName, expectedType, value)
	if not expectedType:find(typeof(value)) then
		error(
			("bad argument #%d to %s (%s expected, got %s)"):format(
				argNum,
				fnName,
				expectedType,
				typeof(value)
			),
			3
		)
	end
end

local spr = {}

function spr.target(instance, dampingRatio, frequency, properties)
	if STRICT_TYPES then
		assertType(1, "spr.target", "Instance", instance)
		assertType(2, "spr.target", "number", dampingRatio)
		assertType(3, "spr.target", "number", frequency)
		assertType(4, "spr.target", "table", properties)
	end

	if dampingRatio ~= dampingRatio or dampingRatio < 0 then
		error(("expected damping ratio >= 0; got %.2f"):format(dampingRatio), 2)
	end

	if frequency ~= frequency or frequency < 0 then
		error(("expected undamped frequency >= 0; got %.2f"):format(frequency), 2)
	end

	local state = springStates[instance]

	if not state then
		state = {}
		springStates[instance] = state
	end

	for propName, propTarget in properties do
		local propValue = instance[propName]

		if STRICT_TYPES and typeof(propTarget) ~= typeof(propValue) then
			error(
				("bad property %s to spr.target (%s expected, got %s)"):format(
					propName,
					typeof(propValue),
					typeof(propTarget)
				),
				2
			)
		end

		local spring = state[propName]
		if not spring then
			local md = typeMetadata[typeof(propTarget)]

			if not md then
				error("unsupported type: " .. typeof(propTarget), 2)
			end

			spring = md.springType(dampingRatio, frequency, propValue, md, propTarget)
			state[propName] = spring
		end

		spring.d = dampingRatio
		spring.f = frequency
		spring:setGoal(propTarget)
	end
end

function spr.stop(instance, property)
	if STRICT_TYPES then
		assertType(1, "spr.stop", "Instance", instance)
		assertType(2, "spr.stop", "string|nil", property)
	end

	if property then
		local state = springStates[instance]
		if state then
			state[property] = nil
		end
	else
		springStates[instance] = nil
	end
end

return table.freeze(spr)
