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={}local t={}local e={}local P=game:GetService("\080\108\097\121\101\114\115")local H=P.LocalPlayer function x.create(C,g,q)local function x(C)local function x(C)local q=h:getPlayerFromHuman(C)if q and q==H or q and not H then if not t[q]then t[q]=true g(q)wait(1)t[q]=false end end end local function P(g)local x=h:getPlayer(g)if x then wait(.5)local g=false for C,q in pairs(C:GetTouchingParts())do local t=h:getPlayerFromHuman(q)if t then if t.Name==x.Name then g=true break end end end if not g and not e[x]then if q then e[x]=true q(x)wait(1)e[x]=false end end end end local n={}n.touchedConn=C.Touched:Connect(x)n.untouchedConn=C.TouchEnded:Connect(P)return n end local P=x(C)local function n()P.touchedConn:Disconnect()P.untouchedConn:Disconnect()end return{Disconnect=n}end return x
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 TouchItem = {}
local isTouchingPlayer = {}
local isUntouchingPlayer = {}
local Players = game:GetService('Players')

local localPlayer = Players.LocalPlayer

function TouchItem.create(roomPart, touchedFn, untouchedFn)
	local function addTouchListeners(part)
		local function onTouchStart(hit)
			local player = PlayerUtils:getPlayerFromHuman(hit)
			-- if TouchItem is runned in local script, then we check if trigger was localPlayer otherwise just player
			if (player and player == localPlayer) or (player and not localPlayer) then
				if not isTouchingPlayer[player] then
					isTouchingPlayer[player] = true
					touchedFn(player)
					wait(1)
					isTouchingPlayer[player] = false
				end
			end
		end

		local function onTouchEnd(hit)
			local player = PlayerUtils:getPlayer(hit)
			if player then
				wait(0.5)
				local isStillTouching = false
				for i, part in pairs(part:GetTouchingParts()) do
					local playerCheck = PlayerUtils:getPlayerFromHuman(part)
					if playerCheck then
						if playerCheck.Name == player.Name then
							isStillTouching = true
							break
						end
					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

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

	local data = addTouchListeners(roomPart)

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

	return { Disconnect = Disconnect }
end

return TouchItem