--[[
	Updates the emote configuration to add default and paid emotes that
	the user owns to it. Also adds these emotes to the user's
	HumanoidDescription so that they can be used by the function
	Humanoid:PlayEmote

	Parameters:
	- player - The Player whose HumanoidDescription is being updated
	- humanoidDescription - The humanoid description to used for getting and
	setting emotes
]]

function EmoteManager:updateEmotes(player, humanoidDescription)
	local ownedEmotes = self.playerOwnedEmotes[player.UserId] or {}

	-- Find emotes set by setEmotes
	local configuredEmotes = emotes.getValues()
	local externalEmotes = {}
	for emoteName, emoteInfo in pairs(configuredEmotes) do
		-- Filter out default and paid emotes from the configuredEmotes
		if defaultEmotes[emoteName] and defaultEmotes[emoteName].emote == emoteInfo.emote then
			continue
		elseif paidEmotes[emoteName] and paidEmotes[emoteName].emote == emoteInfo.emote then
			continue
		end

		externalEmotes[emoteName] = emoteInfo
	end

	local newEmoteConfig
	if serverConfig.getValues().useDefaultEmotes then
		-- Combine default emotes, paid emotes, and any emotes set via setEmotes
		newEmoteConfig = Cryo.Dictionary.join(defaultEmotes, ownedEmotes, externalEmotes)
	else
		-- Only send emotes configured by setEmotes
		newEmoteConfig = externalEmotes
	end

	-- Add default and owned emotes to the player's humanoid
	-- description so that they can be played using the PlayEmote function
	local humanoidEmotes = {}
	for emoteName, emote in pairs(newEmoteConfig) do
		-- Only add Emotes that use emote asset ids. Ignore Emotes that use
		-- animation asset ids.
		if emote.emote then
			humanoidEmotes[emoteName] = { emote.emote }
		end
	end
	humanoidDescription:SetEmotes(humanoidEmotes)

	self.playerEmoteConfig[player.UserId] = newEmoteConfig
	self.remotes.sendUpdatedEmotes:FireClient(player, newEmoteConfig)
end
