-- TODO: retry multiple time if HTTP call fail

local function getTextureId(id: number): number?
	local assetInfo = MarketplaceService:GetProductInfo(id, Enum.InfoType.Asset)

	if assetInfo.AssetTypeId == Enum.AssetType.Image.Value then
		return id
	elseif assetInfo.AssetTypeId == Enum.AssetType.Decal.Value then
		return HttpService:GetAsync(`http://f3xteam.com/bt/getDecalImageID/{id}`)
	end

	return
end

Remote.OnServerEvent:Connect(function(plr, id: number | string)
	if Helper.Owner ~= plr then
		return
	end

	local assetId = tonumber(tostring(id):match("%d+") or 0) :: number

	if assetId == 0 then
		Decal.Texture = ""
		return
	end

	local textureId = getTextureId(assetId)

	if not textureId then
		Decal.Texture = ""
		return
	end

	if textureId == 0 then
		Decal.Texture = ""
	else
		Decal.Texture = `rbxassetid://{textureId}`
	end
end)
