local y=game:GetService("\067\111\108\108\101\099\116\105\111\110\083\101\114\118\105\099\101")local g={}g.__index=g function g.new(y)local H=setmetatable({rocs=y,_tags={}},g)y:registerComponentRegistrationHook(function(y)if y.tag then H:listenForTag(y.tag,y)end end)return H end function g.listenForTag(k,g,H)assert(k._tags[g]==nil,("\084\097\103 \037\113 \105\115 \097\108\114\101\097\100\121 \105\110 \117\115\101\033"):format(g))k._tags[g]=true local function o(y)local g={}if y:FindFirstChild(H.name)and y[H.name].ClassName=="\077\111\100\117\108\101\083\099\114\105\112\116"then g=require(y[H.name])end;(k.rocs:getEntity(y,"\116\097\103\115")):addBaseComponent(H,g)end local function T(y)(k.rocs:getEntity(y,"\116\097\103\115")):removeBaseComponent(H)end;(y:GetInstanceRemovedSignal(g)):Connect(T);(y:GetInstanceAddedSignal(g)):Connect(o)for y,g in ipairs(y:GetTagged(g))do o(g)end end return g
local CollectionService = game:GetService("CollectionService")

local TagWatcher = {}
TagWatcher.__index = TagWatcher

function TagWatcher.new(rocs)
	local self = setmetatable({
		rocs = rocs;
		_tags = {};
	}, TagWatcher)

	rocs:registerComponentRegistrationHook(function(staticAggregate)
		if staticAggregate.tag then
			self:listenForTag(staticAggregate.tag, staticAggregate)
		end
	end)

	return self
end

function TagWatcher:listenForTag(tag, staticAggregate)
	assert(self._tags[tag] == nil, ("Tag %q is already in use!"):format(tag))
	self._tags[tag] = true

	local function addFromTag(instance)
		local data = {}
		if
			instance:FindFirstChild(staticAggregate.name)
			and instance[staticAggregate.name].ClassName == "ModuleScript"
		then
			data = require(instance[staticAggregate.name])
		end

		self.rocs:getEntity(instance, "tags"):addBaseComponent(staticAggregate, data)
	end

	local function removeFromTag(instance)
		self.rocs:getEntity(instance, "tags"):removeBaseComponent(staticAggregate)
	end

	CollectionService:GetInstanceRemovedSignal(tag):Connect(removeFromTag)
	CollectionService:GetInstanceAddedSignal(tag):Connect(addFromTag)
	for _, instance in ipairs(CollectionService:GetTagged(tag)) do
		addFromTag(instance)
	end
end

return TagWatcher
