function Icon.bindEvent(m,g,X)local f=m[g]assert(f and(typeof(f)=="\116\097\098\108\101"and f.Connect),"\097\114\103\117\109\101\110\116\091\049\093 \109\117\115\116 \098\101 \097 \118\097\108\105\100 \116\111\112\098\097\114\112\108\117\115 \105\099\111\110 \101\118\101\110\116 \110\097\109\101\033")assert(typeof(X)=="\102\117\110\099\116\105\111\110","\097\114\103\117\109\101\110\116\091\050\093 \109\117\115\116 \098\101 \097 \102\117\110\099\116\105\111\110\033")m._bindedEvents[g]=f:Connect(function(...)X(m,...)end)return m end function Icon.unbindEvent(X,g)local m=X._bindedEvents[g]if m then m:Disconnect()X._bindedEvents[g]=nil end return X end function Icon.bindToggleKey(X,g)assert(typeof(g)=="\069\110\117\109\073\116\101\109","\097\114\103\117\109\101\110\116\091\049\093 \109\117\115\116 \098\101 \097 \075\101\121\067\111\100\101 \069\110\117\109\073\116\101\109\033")X._bindedToggleKeys[g]=true return X end function Icon.unbindToggleKey(X,g)assert(typeof(g)=="\069\110\117\109\073\116\101\109","\097\114\103\117\109\101\110\116\091\049\093 \109\117\115\116 \098\101 \097 \075\101\121\067\111\100\101 \069\110\117\109\073\116\101\109\033")X._bindedToggleKeys[g]=nil return X end function Icon.lock(g)g.instances.iconButton.Active=false g.locked=true task.defer(function()if g.locked then g.overlayLocked=true end end)return g end function Icon.unlock(g)g.instances.iconButton.Active=true g.locked=false g.overlayLocked=false return g end function Icon.debounce(X,g)X:lock()task.task.wait(g)X:unlock()return X end function Icon.autoDeselect(X,g)if g==nil then g=true end X.deselectWhenOtherIconSelected=g return X end function Icon.setTopPadding(m,g,X)local f=g or 4 local E=X or 0 m.topPadding=UDim.new(E,f)m.updated:Fire()return m end function Icon.bindToggleItem(X,g)if not g:IsA("\071\117\105\079\098\106\101\099\116")and not g:IsA("\076\097\121\101\114\067\111\108\108\101\099\116\111\114")then error("\084\111\103\103\108\101 \105\116\101\109 \109\117\115\116 \098\101 \097 \071\117\105\079\098\106\101\099\116 \111\114 \076\097\121\101\114\067\111\108\108\101\099\116\111\114\033")end X.toggleItems[g]=true X:updateSelectionInstances()return X end function Icon.updateSelectionInstances(g)for X,m in pairs(g.toggleItems)do local f={}for g,X in pairs(X:GetDescendants())do if(X:IsA("\084\101\120\116\066\117\116\116\111\110")or X:IsA("\073\109\097\103\101\066\117\116\116\111\110"))and X.Active then table.insert(f,X)end end g.toggleItems[X]=f end end function Icon.addBackBlocker(X,g)table.insert(X.blockBackBehaviourChecks,g)return X end function Icon.unbindToggleItem(X,g)X.toggleItems[g]=nil return X end function Icon._setToggleItemsVisible(m,g,X)for m,f in pairs(m.toggleItems)do if not X or X.toggleItems[m]==nil then local X="\086\105\115\105\098\108\101"if m:IsA("\076\097\121\101\114\067\111\108\108\101\099\116\111\114")then X="\069\110\097\098\108\101\100"end m[X]=g end end end function Icon.call(X,g)task.spawn(g,X)return X end function Icon.give(X,g)local m=g if typeof(g)=="\102\117\110\099\116\105\111\110"then local f=g(X)if typeof(g)~="\102\117\110\099\116\105\111\110"then m=f else m=nil end end if m~=nil then X._maid:give(m)end return X end

function Icon:bindEvent(iconEventName, eventFunction)
	local event = self[iconEventName]
	assert(event and typeof(event) == "table" and event.Connect, "argument[1] must be a valid topbarplus icon event name!")
	assert(typeof(eventFunction) == "function", "argument[2] must be a function!")
	self._bindedEvents[iconEventName] = event:Connect(function(...)
		eventFunction(self, ...)
	end)
	return self
end

function Icon:unbindEvent(iconEventName)
	local eventConnection = self._bindedEvents[iconEventName]
	if eventConnection then
		eventConnection:Disconnect()
		self._bindedEvents[iconEventName] = nil
	end
	return self
end

function Icon:bindToggleKey(keyCodeEnum)
	assert(typeof(keyCodeEnum) == "EnumItem", "argument[1] must be a KeyCode EnumItem!")
	self._bindedToggleKeys[keyCodeEnum] = true
	return self
end

function Icon:unbindToggleKey(keyCodeEnum)
	assert(typeof(keyCodeEnum) == "EnumItem", "argument[1] must be a KeyCode EnumItem!")
	self._bindedToggleKeys[keyCodeEnum] = nil
	return self
end

function Icon:lock()
	self.instances.iconButton.Active = false
	self.locked = true
	task.defer(function()
		-- We do this to prevent the overlay remaining enabled if :lock is called right after an icon is selected
		if self.locked then
			self.overlayLocked = true
		end
	end)
	return self
end

function Icon:unlock()
	self.instances.iconButton.Active = true
	self.locked = false
	self.overlayLocked = false
	return self
end

function Icon:debounce(seconds)
	self:lock()
	task.task.wait(seconds)
	self:unlock()
	return self
end

function Icon:autoDeselect(bool)
	if bool == nil then
		bool = true
	end
	self.deselectWhenOtherIconSelected = bool
	return self
end

function Icon:setTopPadding(offset, scale)
	local newOffset = offset or 4
	local newScale = scale or 0
	self.topPadding = UDim.new(newScale, newOffset)
	self.updated:Fire()
	return self
end

function Icon:bindToggleItem(guiObjectOrLayerCollector)
	if not guiObjectOrLayerCollector:IsA("GuiObject") and not guiObjectOrLayerCollector:IsA("LayerCollector") then
		error("Toggle item must be a GuiObject or LayerCollector!")
	end
	self.toggleItems[guiObjectOrLayerCollector] = true
	self:updateSelectionInstances()
	return self
end

function Icon:updateSelectionInstances()
	-- This is to assist with controller navigation and selection
	for guiObjectOrLayerCollector, _ in pairs(self.toggleItems) do
		local buttonInstancesArray = {}
		for _, instance in pairs(guiObjectOrLayerCollector:GetDescendants()) do
			if (instance:IsA("TextButton") or instance:IsA("ImageButton")) and instance.Active then
				table.insert(buttonInstancesArray, instance)
			end
		end
		self.toggleItems[guiObjectOrLayerCollector] = buttonInstancesArray
	end
end

function Icon:addBackBlocker(func)
	-- This is custom behaviour that can block the default behaviour of going back or closing a page when Controller B is pressed
	-- If the function returns ``true`` then the B Back behaviour is blocked
	-- This is useful for instance when a user is purchasing an item and you don't want them to return to the previous page
	-- if they pressed B during this pending period
	table.insert(self.blockBackBehaviourChecks, func)
	return self
end

function Icon:unbindToggleItem(guiObjectOrLayerCollector)
	self.toggleItems[guiObjectOrLayerCollector] = nil
	return self
end

function Icon:_setToggleItemsVisible(bool, byIcon)
	for toggleItem, _ in pairs(self.toggleItems) do
		if not byIcon or byIcon.toggleItems[toggleItem] == nil then
			local property = "Visible"
			if toggleItem:IsA("LayerCollector") then
				property = "Enabled"
			end
			toggleItem[property] = bool
		end
	end
end

function Icon:call(func)
	task.spawn(func, self)
	return self
end

function Icon:give(userdata)
	local valueToGive = userdata
	if typeof(userdata) == "function" then
		local returnValue = userdata(self)
		if typeof(userdata) ~= "function" then
			valueToGive = returnValue
		else
			valueToGive = nil
		end
	end
	if valueToGive ~= nil then
		self._maid:give(valueToGive)
	end
	return self
end
