--[[
	Returns a character model wearing an outfit that corresponds to what the
	"player" is already wearing, with the addition of one item described by
	"itemInfo".

	If the player is already wearing something of the same category, that
	existing item is removed first.

	If no player is provided, create instead a dummy with a default look.
]]


local Players = game:GetService("Players")

local MerchBooth = script:FindFirstAncestor("MerchBooth")

local types = require(MerchBooth.types)
local generateDescriptionWithItem = require(script.Parent.generateDescriptionWithItem)

local idleAnimation = Instance.new("Animation")
idleAnimation.Name = "IdleAnimation"
idleAnimation.AnimationId = "rbxassetid://616136790"

local function generateDummyWithItem(itemInfo: types.ItemInfo, player: Player?)
	local description = generateDescriptionWithItem(player, itemInfo.assetId, itemInfo.assetType)
	local previewAvatar = Players:CreateHumanoidModelFromDescription(description, Enum.HumanoidRigType.R15)

	-- Destroys scripts to silence a client-side warning
	for _, descendant in ipairs(previewAvatar:GetDescendants()) do
		if descendant:IsA("LuaSourceContainer") then
			descendant:Destroy()
		end
	end

	local humanoid = previewAvatar:FindFirstChildOfClass("Humanoid")

	humanoid.RootPart.CFrame = CFrame.new(0, 1e5, 0)
	humanoid.RootPart.Anchored = true

	-- Not parenting the character in workspace will cause animations and layered clothing to not render properly
	previewAvatar.Parent = workspace

	humanoid:LoadAnimation(idleAnimation):Play()

	return previewAvatar
end

return generateDummyWithItem
