local C=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local g=C:WaitForChild("\077\111\100\117\108\101\115")local q=require(g.src.utils.Logger)local h=require(g.src.utils.PlayerUtils)local x=game:GetService("\067\111\108\108\101\099\116\105\111\110\083\101\114\118\105\099\101")local t={}local e={}local P={}local H={}function t.create(C,g,q,t,n)local l=x:GetInstanceAddedSignal(g)local X=x:GetInstanceRemovedSignal(g)local function B(C)local function g(g)if h:isHuman(g)then local x=h:getPlayer(g)if x then if not P[x]then P[x]=true q(x,g,C)wait(n and n or 1)P[x]=false end end end end local function x(g)if h:isHuman(g)then local q=h:getPlayer(g)if q then wait(.5)local g=false for C,q in pairs(C:GetTouchingParts())do if h:isHuman(q)then g=true break end end if not g and not H[q]then if t then H[q]=true t(q)wait(1)H[q]=false end end end end end local e={}e.touchedConn=C.Touched:Connect(g)e.untouchedConn=C.TouchEnded:Connect(x)return e end local function b(C)C.touchedConn:Disconnect()C.untouchedConn:Disconnect()end local function T(C)e[C]=B(C)end local function O(C)if e[C]then b(e[C])e[C]=nil end end for g,q in pairs(x:GetTagged(g))do if C then local g=require(q.Config)if g.roomId==C then T(q)end else T(q)end end l:Connect(T)X:Connect(O)end return t
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Modules = ReplicatedStorage:WaitForChild('Modules')
local logger = require(Modules.src.utils.Logger)
local PlayerUtils = require(Modules.src.utils.PlayerUtils)

local CollectionService = game:GetService('CollectionService')

local TagItem = {}

local bricks = {}
local isTouchingPlayer = {}
local isUntouchingPlayer = {}

function TagItem.create(roomId, tagName, touchedFn, untouchedFn, waitFor)
	local addedSignal = CollectionService:GetInstanceAddedSignal(tagName)
	local removedSignal = CollectionService:GetInstanceRemovedSignal(tagName)

	local function makeBrick(part)
		local function onTouchStart(hit)
			if PlayerUtils:isHuman(hit) then
				local player = PlayerUtils:getPlayer(hit)
				if player then
					if not isTouchingPlayer[player] then
						isTouchingPlayer[player] = true
						touchedFn(player, hit, part)

						wait(waitFor and waitFor or 1)

						isTouchingPlayer[player] = false
					end
				end
			end
		end

		local function onTouchEnd(hit)
			if PlayerUtils:isHuman(hit) then
				local player = PlayerUtils:getPlayer(hit)
				if player then
					wait(0.5)
					local isStillTouching = false
					for i, part in pairs(part:GetTouchingParts()) do
						if PlayerUtils:isHuman(part) then
							isStillTouching = true
							break
						end
					end

					if not isStillTouching and not isUntouchingPlayer[player] then
						if untouchedFn then
							isUntouchingPlayer[player] = true
							untouchedFn(player)
							wait(1)
							isUntouchingPlayer[player] = false
						end
					end
				end
			end
		end

		local data = {}
		data.touchedConn = part.Touched:Connect(onTouchStart)
		data.untouchedConn = part.TouchEnded:Connect(onTouchEnd)

		return data
	end

	local function undoBrick(data)
		data.touchedConn:Disconnect()
		data.untouchedConn:Disconnect()
	end

	local function onAdded(brick)
		bricks[brick] = makeBrick(brick)
	end

	local function onRemoved(brick)
		if bricks[brick] then
			undoBrick(bricks[brick])
			bricks[brick] = nil
		end
	end

	for _, brick in pairs(CollectionService:GetTagged(tagName)) do
		if roomId then
			local MapConfig = require(brick.Config)

			if MapConfig.roomId == roomId then
				onAdded(brick)
			end
		else
			onAdded(brick)
		end
	end

	addedSignal:Connect(onAdded)
	removedSignal:Connect(onRemoved)
end

return TagItem