function Icon.bindEvent(w,s,Q)local t=w[s]assert(t and(typeof(t)=="\116\097\098\108\101"and t.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(Q)=="\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")w._bindedEvents[s]=t:Connect(function(...)Q(w,...)end)return w end function Icon.unbindEvent(Q,s)local w=Q._bindedEvents[s]if w then w:Disconnect()Q._bindedEvents[s]=nil end return Q end function Icon.bindToggleKey(Q,s)assert(typeof(s)=="\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")Q._bindedToggleKeys[s]=true return Q end function Icon.unbindToggleKey(Q,s)assert(typeof(s)=="\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")Q._bindedToggleKeys[s]=nil return Q end function Icon.lock(s)s.instances.iconButton.Active=false s.locked=true task.defer(function()if s.locked then s.overlayLocked=true end end)return s end function Icon.unlock(s)s.instances.iconButton.Active=true s.locked=false s.overlayLocked=false return s end function Icon.debounce(Q,s)Q:lock()task.wait(s)Q:unlock()return Q end function Icon.autoDeselect(Q,s)if s==nil then s=true end Q.deselectWhenOtherIconSelected=s return Q end function Icon.setTopPadding(w,s,Q)local t=s or 4 local A=Q or 0 w.topPadding=UDim.new(A,t)w.updated:Fire()return w end function Icon.bindToggleItem(Q,s)if not s:IsA("\071\117\105\079\098\106\101\099\116")and not s: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 Q.toggleItems[s]=true Q:updateSelectionInstances()return Q end function Icon.updateSelectionInstances(s)for Q,w in pairs(s.toggleItems)do local t={}for s,Q in pairs(Q:GetDescendants())do if(Q:IsA("\084\101\120\116\066\117\116\116\111\110")or Q:IsA("\073\109\097\103\101\066\117\116\116\111\110"))and Q.Active then table.insert(t,Q)end end s.toggleItems[Q]=t end end function Icon.addBackBlocker(Q,s)table.insert(Q.blockBackBehaviourChecks,s)return Q end function Icon.unbindToggleItem(Q,s)Q.toggleItems[s]=nil return Q end function Icon._setToggleItemsVisible(w,s,Q)for w,t in pairs(w.toggleItems)do if not Q or Q.toggleItems[w]==nil then local Q="\086\105\115\105\098\108\101"if w:IsA("\076\097\121\101\114\067\111\108\108\101\099\116\111\114")then Q="\069\110\097\098\108\101\100"end w[Q]=s end end end function Icon.call(Q,s)task.spawn(s,Q)return Q end function Icon.give(Q,s)local w=s if typeof(s)=="\102\117\110\099\116\105\111\110"then local t=s(Q)if typeof(s)~="\102\117\110\099\116\105\111\110"then w=t else w=nil end end if w~=nil then Q._maid:give(w)end return Q 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.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
