-- Existance in this list signifies that it is an emote, the value indicates if it is a looping emote

local emoteNames: { [string]: boolean } =
	{ }

function configureAnimationSet(name: string, fileList: { { id: string, weight: number } })
	if animTable[name] ~= nil then
		for _, connection in ipairs(animTable[name].connections) do
			connection:Disconnect()
		end
	end

	animTable[name] = {} :: any
	animTable[name].count = 0
	animTable[name].totalWeight = 0
	animTable[name].connections = {}

	-- check for config values
	local config = script:FindFirstChild(name)
	if config ~= nil then
		--		print("Loading anims " .. name)
		table.insert(
			animTable[name].connections,
			config.ChildAdded:Connect(function(child)
				configureAnimationSet(name, fileList)
			end)
		)

		table.insert(
			animTable[name].connections,
			config.ChildRemoved:Connect(function(child)
				configureAnimationSet(name, fileList)
			end)
		)

		local idx = 1
		for _, childPart in ipairs(config:GetChildren()) do
			if childPart:IsA("Animation") then
				table.insert(
					animTable[name].connections,
					childPart.Changed:Connect(function(property)
						configureAnimationSet(name, fileList)
					end)
				)

				animTable[name][idx] = {} :: any
				animTable[name][idx].anim = childPart

				local weightObject = childPart:FindFirstChild("Weight")
				if weightObject == nil then
					animTable[name][idx].weight = 1
				else
					animTable[name][idx].weight = weightObject.Value
				end

				animTable[name].count = animTable[name].count + 1
				animTable[name].totalWeight = animTable[name].totalWeight + animTable[name][idx].weight
				--			print(name .. " [" .. idx .. "] " .. animTable[name][idx].anim.AnimationId .. " (" .. animTable[name][idx].weight .. ")")
				idx = idx + 1
			end
		end
	end

	-- fallback to defaults
	if animTable[name].count <= 0 then
		for idx, anim in ipairs(fileList) do
			animTable[name][idx] = {} :: any
			animTable[name][idx].anim = Instance.new("Animation")
			animTable[name][idx].anim.Name = name
			animTable[name][idx].anim.AnimationId = anim.id
			animTable[name][idx].weight = anim.weight
			animTable[name].count = animTable[name].count + 1
			animTable[name].totalWeight = animTable[name].totalWeight + anim.weight
			--			print(name .. " [" .. idx .. "] " .. anim.id .. " (" .. anim.weight .. ")")
		end
	end
end
