----- Private Functions -----


local debounce = {}



local function SellSpeed(player: Player, sellPart : Part) -- convert speed to coin
	if debounce[player] then return end -- the cooldown
	
	local profile = PlayerData.Profiles[player] -- convert the speed to coin
	if not profile then return "Profile not found" end
	
	local sellPartArea = sellPart:GetAttribute("Area")
	
	if not profile.Data.Area[sellPartArea] then return "Area not unlocked" end -- check if the player has unlocked the area
	
	local SpeedToSell = profile.Data.Speed
	PlayerData.ResetSpeed(player)
	
	local CoinToMake = SpeedToSell * Stat.CoinMultiplier(player, profile.Data, sellPart)
	
	PlayerData.AdjustCoin(player, CoinToMake)
	
	debounce[player] = true -- activate the cooldown
	task.delay(SELL_COOLDOWN, function()
		debounce[player] = nil
	end)
end


local function CreateSellListener()
	for _, descendant in Workspace.Game.Special:GetDescendants() do
		if descendant.Name ~= "Sell" then continue end
		
		local touchPart = descendant.Hit
		touchPart.Touched:Connect(function(otherPart)
			local player = Players:GetPlayerFromCharacter(otherPart.Parent)
			if player then
				SellSpeed(player, descendant)
			end
		end)
		
	end
end
