--[=[
	Promises a user thumbnail with retry enabled.

	```lua
	PlayerThumbnailUtils.promiseUserThumbnail(4397833):Then(function(image)
		imageLabel.Image = image
	end)
	```
	@param userId number
	@param thumbnailType ThumbnailType?
	@param thumbnailSize ThumbnailSize?
	@return Promise<string>
]=]

function PlayerThumbnailUtils.promiseUserThumbnail(userId, thumbnailType, thumbnailSize)
	assert(type(userId) == "number", "Bad userId")
	thumbnailType = thumbnailType or Enum.ThumbnailType.HeadShot
	thumbnailSize = thumbnailSize or Enum.ThumbnailSize.Size100x100

	local promise
	promise = Promise.spawn(function(resolve, reject)
		local tries = 0
		repeat
			tries = tries + 1
			local content, isReady
			local ok, err = pcall(function()
				content, isReady = Players:GetUserThumbnailAsync(userId, thumbnailType, thumbnailSize)
			end)

			-- Don't retry if we immediately error (timeout exceptions!)
			if not ok then
				return reject(err)
			end

			if isReady then
				return resolve(content)
			else
				task.wait(0.05)
			end
		until tries >= MAX_TRIES or (not promise:IsPending())
		reject()
	end)

	return promise
end
