-- Hook up ShopRegion parts to open and close the merch booth

local TAG_NAME = "ShopRegion"
local DEBOUNCE_TIME = 1
local lastClose = 0
function hookupRegionPart(regionPart)
	regionPart.Touched:Connect(function(otherPart)
		
		-- Debounce the shop from opening too soon after being closed
		if os.clock() < lastClose + DEBOUNCE_TIME then
			return
		end
		
		local character = Players.LocalPlayer.Character
		if character and character:FindFirstChild("HumanoidRootPart") == otherPart then
			MerchBooth.openMerchBooth()
		end
	end)

	regionPart.TouchEnded:Connect(function(otherPart)
		local character = Players.LocalPlayer.Character
		if character and character:FindFirstChild("HumanoidRootPart") == otherPart then
			lastClose = os.clock()
			MerchBooth.closeMerchBooth()
		end
	end)
end

for _, regionPart in ipairs(CollectionService:GetTagged(TAG_NAME)) do
	hookupRegionPart(regionPart)
end
CollectionService:GetInstanceAddedSignal(TAG_NAME):connect(hookupRegionPart)
