function Icon.set(p,A,W,X,Z)local u=p._settingsDictionary[A]assert(u~=nil,("\115\101\116\116\105\110\103 \'\037\115\' \100\111\101\115 \110\111\116 \101\120\105\115\116"):format(A))if type(X)=="\115\116\114\105\110\103"then X=X:lower()end local L=p:get(A,X)if X=="\104\111\118\101\114\105\110\103"then u.hoveringValue=W if Z~="\095\105\103\110\111\114\101\080\114\101\118\105\111\117\115"then u.additionalValues["\112\114\101\118\105\111\117\115\095"..X]=L end if type(Z)=="\115\116\114\105\110\103"then u.additionalValues[Z..("\095"..X)]=L end p:_update(A)else local x=X local R=u.type if R=="\116\111\103\103\108\101\097\098\108\101"then local A={}if x=="\100\101\115\101\108\101\099\116\101\100"or x=="\115\101\108\101\099\116\101\100"then table.insert(A,x)else table.insert(A,"\100\101\115\101\108\101\099\116\101\100")table.insert(A,"\115\101\108\101\099\116\101\100")x=nil end for A,X in pairs(A)do u.values[X]=W if Z~="\095\105\103\110\111\114\101\080\114\101\118\105\111\117\115"then u.additionalValues["\112\114\101\118\105\111\117\115\095"..X]=L end if type(Z)=="\115\116\114\105\110\103"then u.additionalValues[Z..("\095"..X)]=L end end else u.value=W if type(Z)=="\115\116\114\105\110\103"then if Z~="\095\105\103\110\111\114\101\080\114\101\118\105\111\117\115"then u.additionalValues.previous=L end u.additionalValues[Z]=L end end if L==W then return p,"\086\097\108\117\101 \119\097\115 \097\108\114\101\097\100\121 \115\101\116"end local e=p:getToggleState()if not p._updateAfterSettingAll and(u.instanceNames and(e==x or x==nil))then local W=A=="\105\099\111\110\083\105\122\101"and(L and L.X.Scale==1)local X=u.tweenAction and(not W and p:get(u.tweenAction))or TweenInfo.new(0)p:_update(A,e,X)end end if u.callMethods then for A,Z in pairs(u.callMethods)do Z(p,W,X)end end if u.callSignals then for A,W in pairs(u.callSignals)do W:Fire()end end return p end function Icon.get(Z,A,W,X)local p=Z._settingsDictionary[A]assert(p~=nil,("\115\101\116\116\105\110\103 \'\037\115\' \100\111\101\115 \110\111\116 \101\120\105\115\116"):format(A))local u,L if typeof(W)=="\115\116\114\105\110\103"then W=W:lower()end if W=="\104\111\118\101\114\105\110\103"and X==nil then u=p.hoveringValue L=type(X)=="\115\116\114\105\110\103"and p.additionalValues[X..("\095"..W)]end local x=p.type if x=="\116\111\103\103\108\101\097\098\108\101"then local A=(W=="\100\101\115\101\108\101\099\116\101\100"or W=="\115\101\108\101\099\116\101\100")and W or Z:getToggleState()if L==nil then L=type(X)=="\115\116\114\105\110\103"and p.additionalValues[X..("\095"..A)]end if u==nil then u=p.values[A]end else if L==nil then L=type(X)=="\115\116\114\105\110\103"and p.additionalValues[X]end if u==nil then u=p.value end end return u,L end function Icon.getHovering(W,A)local X=W._settingsDictionary[A]assert(X~=nil,("\115\101\116\116\105\110\103 \'\037\115\' \100\111\101\115 \110\111\116 \101\120\105\115\116"):format(A))return X.hoveringValue end function Icon.getToggleState(W,A)A=A or W.isSelected return A and"\115\101\108\101\099\116\101\100"or"\100\101\115\101\108\101\099\116\101\100"end function Icon.getIconState(A)if A.hovering then return"\104\111\118\101\114\105\110\103"else return A:getToggleState()end end function Icon._update(Z,A,W,X)local p=Z._settingsDictionary[A]assert(p~=nil,("\115\101\116\116\105\110\103 \'\037\115\' \100\111\101\115 \110\111\116 \101\120\105\115\116"):format(A))W=W or Z:getToggleState()local u=p.value or p.values and p.values[W]if Z.hovering and p.hoveringValue then u=p.hoveringValue end if u==nil then return end local L=X or p.tweenAction and Z:get(p.tweenAction)or Z:get("\116\111\103\103\108\101\084\114\097\110\115\105\116\105\111\110\073\110\102\111")or TweenInfo.new(.15)local x=p.propertyName local R={string=true;NumberSequence=true;Text=true,EnumItem=true;ColorSequence=true}local e=Z._uniqueSettingsDictionary[A]local r=u if p.useForcedGroupValue then r=p.forcedGroupValue end if p.instanceNames then for W,X in pairs(p.instanceNames)do local p=Z.instances[X]local c=typeof(p[x])local d=R[c]or typeof(p)=="\116\097\098\108\101"if e then e(A,p,x,r)elseif d then p[x]=u else(tweenService:Create(p,L,{[x]=r})):Play()end if A=="\105\099\111\110\083\105\122\101"and p[x]~=r then Z.updated:Fire()end end end end function Icon._updateAll(X,A,W)for Z,p in pairs(X._settingsDictionary)do if p.instanceNames then X:_update(Z,A,W)end end end function Icon._updateHovering(W,A)for X,Z in pairs(W._settingsDictionary)do if Z.instanceNames and Z.hoveringValue~=nil then W:_update(X,nil,A)end end end function Icon._updateStateOverlay(X,A,W)local Z=X.instances.iconOverlay Z.BackgroundTransparency=A or 1 Z.BackgroundColor3=W or Color3.new(1,1,1)end function Icon.setTheme(X,A,W)X._updateAfterSettingAll=W for A,W in pairs(A)do if A=="\116\111\103\103\108\101\097\098\108\101"then for A,W in pairs(W.deselected)do if not X.lockedSettings[A]then X:set(A,W,"\098\111\116\104")end end for A,W in pairs(W.selected)do if not X.lockedSettings[A]then X:set(A,W,"\115\101\108\101\099\116\101\100")end end else for A,W in pairs(W)do if not X.lockedSettings[A]then X:set(A,W)end end end end X._updateAfterSettingAll=nil if W then X:_updateAll()end return X end function Icon.getInstance(W,A)return W.instances[A]end function Icon.setInstance(X,A,W)local Z=X.instances[A]X.instances[A]=W if Z then Z:Destroy()end return X end function Icon.getSettingDetail(W,A)for W,X in pairs(W._settings)do for W,X in pairs(X)do if W==A then return X end end end return false end function Icon.modifySetting(X,A,W)local Z=X:getSettingDetail(A)for A,W in pairs(W)do Z[A]=W end return X end function Icon.convertLabelToNumberSpinner(W,A)W:set("\105\099\111\110\076\097\098\101\108\083\105\122\101",UDim2.new(1,0,1,0))A.Parent=W:getInstance("\105\099\111\110\066\117\116\116\111\110")local X={}setmetatable(X,{__newindex=function(W,X,Z)for A,W in pairs(A.Frame:GetDescendants())do if W:IsA("\084\101\120\116\076\097\098\101\108")then W[X]=Z end end end})local Z=W:getInstance("\105\099\111\110\066\117\116\116\111\110")Z.ZIndex=0 W:setInstance("\105\099\111\110\076\097\098\101\108",X)W:modifySetting("\105\099\111\110\084\101\120\116",{instanceNames={}})W:setInstance("\105\099\111\110\076\097\098\101\108\083\112\105\110\110\101\114",A.Frame)local p={"\105\099\111\110\076\097\098\101\108\086\105\115\105\098\108\101","\105\099\111\110\076\097\098\101\108\065\110\099\104\111\114\080\111\105\110\116";"\105\099\111\110\076\097\098\101\108\080\111\115\105\116\105\111\110","\105\099\111\110\076\097\098\101\108\083\105\122\101"}for A,X in pairs(p)do W:modifySetting(X,{instanceNames={"\105\099\111\110\076\097\098\101\108\083\112\105\110\110\101\114"}})end W:_updateAll()return W end function Icon.setEnabled(W,A)W.enabled=A W.instances.iconContainer.Visible=A W.updated:Fire()return W end function Icon.setName(W,A)W.name=A W.instances.iconContainer.Name=A return W end function Icon.setProperty(X,A,W)X[A]=W return X end function Icon._playClickSound(A)local W=A.instances.clickSound if W.SoundId~=nil and(#W.SoundId>0 and W.Volume>0)then local A=W:Clone()A.Parent=W.Parent A:Play()debris:AddItem(A,W.TimeLength)end end function Icon.select(W,A)if W.locked then return W end W.isSelected=true W:_setToggleItemsVisible(true,A)W:_updateNotice()W:_updateAll()W:_playClickSound()if#W.dropdownIcons>0 or#W.menuIcons>0 then IconController:_updateSelectionGroup()end W.selected:Fire()W.toggled:Fire(W.isSelected)return W end function Icon.deselect(W,A)if W.locked then return W end W.isSelected=false W:_setToggleItemsVisible(false,A)W:_updateNotice()W:_updateAll()W:_playClickSound()if#W.dropdownIcons>0 or#W.menuIcons>0 then IconController:_updateSelectionGroup()end W.deselected:Fire()W.toggled:Fire(W.isSelected)return W end function Icon.notify(X,A,W)(coroutine.wrap(function()if not A then A=X.deselected end if X._parentIcon then X._parentIcon:notify(A)end local Z=Signal.new()local p=X._endNotices:Connect(function()Z:Fire()end)local u=A:Connect(function()Z:Fire()end)W=W or httpService:GenerateGUID(true)X.notices[W]={completeSignal=Z;clearNoticeEvent=A}X.totalNotices+=1 X:_updateNotice()X.notified:Fire(W)Z:Wait()p:Disconnect()u:Disconnect()Z:Disconnect()X.totalNotices-=1 X.notices[W]=nil X:_updateNotice()end))()return X end function Icon._updateNotice(A)local W=true if A.totalNotices<1 then W=false end if not A.isSelected then if(#A.dropdownIcons>0 or#A.menuIcons>0)and A.totalNotices>0 then W=true end end if A.isSelected then if#A.dropdownIcons>0 or#A.menuIcons>0 then W=false end end local X=W and 0 or 1 A:set("\110\111\116\105\099\101\073\109\097\103\101\084\114\097\110\115\112\097\114\101\110\099\121",X)A:set("\110\111\116\105\099\101\084\101\120\116\084\114\097\110\115\112\097\114\101\110\099\121",X)A.instances.noticeLabel.Text=A.totalNotices<100 and A.totalNotices or"\057\057\043"end function Icon.clearNotices(A)A._endNotices:Fire()return A end function Icon.disableStateOverlay(W,A)if A==nil then A=true end local X=W.instances.iconOverlay X.Visible=not A return W end

function Icon:set(settingName, value, iconState, setAdditional)
	local settingDetail = self._settingsDictionary[settingName]
	assert(settingDetail ~= nil, ("setting '%s' does not exist"):format(settingName))
	if type(iconState) == "string" then
		iconState = iconState:lower()
	end
	local previousValue = self:get(settingName, iconState)

	if iconState == "hovering" then
		-- Apply hovering state if valid
		settingDetail.hoveringValue = value
		if setAdditional ~= "_ignorePrevious" then
			settingDetail.additionalValues["previous_"..iconState] = previousValue
		end
		if type(setAdditional) == "string" then
			settingDetail.additionalValues[setAdditional.."_"..iconState] = previousValue
		end
		self:_update(settingName)

	else
		-- Update the settings value
		local toggleState = iconState
		local settingType = settingDetail.type
		if settingType == "toggleable" then
			local valuesToSet = {}
			if toggleState == "deselected" or toggleState == "selected" then
				table.insert(valuesToSet, toggleState)
			else
				table.insert(valuesToSet, "deselected")
				table.insert(valuesToSet, "selected")
				toggleState = nil
			end
			for i, v in pairs(valuesToSet) do
				settingDetail.values[v] = value
				if setAdditional ~= "_ignorePrevious" then
					settingDetail.additionalValues["previous_"..v] = previousValue
				end
				if type(setAdditional) == "string" then
					settingDetail.additionalValues[setAdditional.."_"..v] = previousValue
				end
			end
		else
			settingDetail.value = value
			if type(setAdditional) == "string" then
				if setAdditional ~= "_ignorePrevious" then
					settingDetail.additionalValues["previous"] = previousValue
				end
				settingDetail.additionalValues[setAdditional] = previousValue
			end
		end

		-- Check previous and new are not the same
		if previousValue == value then
			return self, "Value was already set"
		end

		-- Update appearances of associated instances
		local currentToggleState = self:getToggleState()
		if not self._updateAfterSettingAll and settingDetail.instanceNames and (currentToggleState == toggleState or toggleState == nil) then
			local ignoreTweenAction = (settingName == "iconSize" and previousValue and previousValue.X.Scale == 1)
			local tweenInfo = (settingDetail.tweenAction and not ignoreTweenAction and self:get(settingDetail.tweenAction)) or TweenInfo.new(0)
			self:_update(settingName, currentToggleState, tweenInfo)
		end
	end

	-- Call any methods present
	if settingDetail.callMethods then
		for _, callMethod in pairs(settingDetail.callMethods) do
			callMethod(self, value, iconState)
		end
	end
	
	-- Call any signals present
	if settingDetail.callSignals then
		for _, callSignal in pairs(settingDetail.callSignals) do
			callSignal:Fire()
		end
	end
	return self
end

function Icon:get(settingName, iconState, getAdditional)
	local settingDetail = self._settingsDictionary[settingName]
	assert(settingDetail ~= nil, ("setting '%s' does not exist"):format(settingName))
	local valueToReturn, additionalValueToReturn
	if typeof(iconState) == "string" then
		iconState = iconState:lower()
	end

	--if ((self.hovering and settingDetail.hoveringValue) or iconState == "hovering") and getAdditional == nil then
	if (iconState == "hovering") and getAdditional == nil then
		valueToReturn = settingDetail.hoveringValue
		additionalValueToReturn = type(getAdditional) == "string" and settingDetail.additionalValues[getAdditional.."_"..iconState]
	end

	local settingType = settingDetail.type
	if settingType == "toggleable" then
		local toggleState = ((iconState == "deselected" or iconState == "selected") and iconState) or self:getToggleState()
		if additionalValueToReturn == nil then
			additionalValueToReturn = type(getAdditional) == "string" and settingDetail.additionalValues[getAdditional.."_"..toggleState]
		end
		if valueToReturn == nil then
			valueToReturn = settingDetail.values[toggleState]
		end
	
	else
		if additionalValueToReturn == nil then
			additionalValueToReturn = type(getAdditional) == "string" and settingDetail.additionalValues[getAdditional]
		end
		if valueToReturn == nil then
			valueToReturn = settingDetail.value
		end
	end

	return valueToReturn, additionalValueToReturn
end

function Icon:getHovering(settingName)
	local settingDetail = self._settingsDictionary[settingName]
	assert(settingDetail ~= nil, ("setting '%s' does not exist"):format(settingName))
	return settingDetail.hoveringValue
end

function Icon:getToggleState(isSelected)
	isSelected = isSelected or self.isSelected
	return (isSelected and "selected") or "deselected"
end

function Icon:getIconState()
	if self.hovering then
		return "hovering"
	else
		return self:getToggleState()
	end
end

function Icon:_update(settingName, toggleState, customTweenInfo)
	local settingDetail = self._settingsDictionary[settingName]
	assert(settingDetail ~= nil, ("setting '%s' does not exist"):format(settingName))
	toggleState = toggleState or self:getToggleState()
	local value = settingDetail.value or (settingDetail.values and settingDetail.values[toggleState])
	if self.hovering and settingDetail.hoveringValue then
		value = settingDetail.hoveringValue
	end
	if value == nil then return end
	local tweenInfo = customTweenInfo or (settingDetail.tweenAction and self:get(settingDetail.tweenAction)) or self:get("toggleTransitionInfo") or TweenInfo.new(0.15)
	local propertyName = settingDetail.propertyName
	local invalidPropertiesTypes = {
		["string"] = true,
		["NumberSequence"] = true,
		["Text"] = true,
		["EnumItem"] = true,
		["ColorSequence"] = true,
	}
	local uniqueSetting = self._uniqueSettingsDictionary[settingName]
	local newValue = value
	if settingDetail.useForcedGroupValue then
		newValue = settingDetail.forcedGroupValue
	end
	if settingDetail.instanceNames then
		for _, instanceName in pairs(settingDetail.instanceNames) do
			local instance = self.instances[instanceName]
			local propertyType = typeof(instance[propertyName])
			local cannotTweenProperty = invalidPropertiesTypes[propertyType] or typeof(instance) == "table"
			if uniqueSetting then
				uniqueSetting(settingName, instance, propertyName, newValue)
			elseif cannotTweenProperty then
				instance[propertyName] = value
			else
				tweenService:Create(instance, tweenInfo, {[propertyName] = newValue}):Play()
			end
			--
			if settingName == "iconSize" and instance[propertyName] ~= newValue then
				self.updated:Fire()
			end
			--
		end
	end
end

function Icon:_updateAll(iconState, customTweenInfo)
	for settingName, settingDetail in pairs(self._settingsDictionary) do
		if settingDetail.instanceNames then
			self:_update(settingName, iconState, customTweenInfo)
		end
	end
end

function Icon:_updateHovering(customTweenInfo)
	for settingName, settingDetail in pairs(self._settingsDictionary) do
		if settingDetail.instanceNames and settingDetail.hoveringValue ~= nil then
			self:_update(settingName, nil, customTweenInfo)
		end
	end
end

function Icon:_updateStateOverlay(transparency, color)
	local stateOverlay = self.instances.iconOverlay
	stateOverlay.BackgroundTransparency = transparency or 1
	stateOverlay.BackgroundColor3 = color or Color3.new(1, 1, 1)
end

function Icon:setTheme(theme, updateAfterSettingAll)
	self._updateAfterSettingAll = updateAfterSettingAll
	for settingsType, settingsDetails in pairs(theme) do
		if settingsType == "toggleable" then
			for settingName, settingValue in pairs(settingsDetails.deselected) do
				if not self.lockedSettings[settingName] then
					self:set(settingName, settingValue, "both")
				end
			end
			for settingName, settingValue in pairs(settingsDetails.selected) do
				if not self.lockedSettings[settingName] then
					self:set(settingName, settingValue, "selected")
				end
			end
		else
			for settingName, settingValue in pairs(settingsDetails) do
				if not self.lockedSettings[settingName] then
					self:set(settingName, settingValue)
				end
			end
		end
	end
	self._updateAfterSettingAll = nil
	if updateAfterSettingAll then
		self:_updateAll()
	end
	return self
end

function Icon:getInstance(instanceName)
	return self.instances[instanceName]
end

function Icon:setInstance(instanceName, instance)
	local originalInstance = self.instances[instanceName]
	self.instances[instanceName] = instance
	if originalInstance then
		originalInstance:Destroy()
	end
	return self
end

function Icon:getSettingDetail(targetSettingName)
	for _, settingsDetails in pairs(self._settings) do
		for settingName, settingDetail in pairs(settingsDetails) do
			if settingName == targetSettingName then
				return settingDetail
			end
		end
	end
	return false
end

function Icon:modifySetting(settingName, dictionary)
	local settingDetail = self:getSettingDetail(settingName)
	for key, value in pairs(dictionary) do
		settingDetail[key] = value
	end
	return self
end

function Icon:convertLabelToNumberSpinner(numberSpinner)
	-- This updates the number spinners appearance
	self:set("iconLabelSize", UDim2.new(1,0,1,0))
	numberSpinner.Parent = self:getInstance("iconButton")

	-- This creates a fake iconLabel which updates the property of all descendant spinner TextLabels when indexed
	local textLabel = {}
	setmetatable(textLabel, {__newindex = function(_, index, value)
		for _, label in pairs(numberSpinner.Frame:GetDescendants()) do
			if label:IsA("TextLabel") then
				label[index] = value
			end
		end
	end})

	-- This overrides existing instances and settings so that they update the spinners properties (instead of the old textlabel)
	local iconButton = self:getInstance("iconButton")
	iconButton.ZIndex = 0
	self:setInstance("iconLabel", textLabel)
	self:modifySetting("iconText", {instanceNames = {}}) -- We do this to prevent text being modified within the metatable above
	self:setInstance("iconLabelSpinner", numberSpinner.Frame)
	local settingsToConvert = {"iconLabelVisible", "iconLabelAnchorPoint", "iconLabelPosition", "iconLabelSize"}
	for _, settingName in pairs(settingsToConvert) do
		self:modifySetting(settingName, {instanceNames = {"iconLabelSpinner"}})
	end

	-- This applies all the values we just updated
	self:_updateAll()
	return self
end

function Icon:setEnabled(bool)
	self.enabled = bool
	self.instances.iconContainer.Visible = bool
	self.updated:Fire()
	return self
end

function Icon:setName(string)
	self.name = string
	self.instances.iconContainer.Name = string
	return self
end

function Icon:setProperty(propertyName, value)
	self[propertyName] = value
	return self
end

function Icon:_playClickSound()
	local clickSound = self.instances.clickSound
	if clickSound.SoundId ~= nil and #clickSound.SoundId > 0 and clickSound.Volume > 0 then
		local clickSoundCopy = clickSound:Clone()
		clickSoundCopy.Parent = clickSound.Parent
		clickSoundCopy:Play()
		debris:AddItem(clickSoundCopy, clickSound.TimeLength)
	end
end

function Icon:select(byIcon)
	if self.locked then return self end
	self.isSelected = true
	self:_setToggleItemsVisible(true, byIcon)
	self:_updateNotice()
	self:_updateAll()
	self:_playClickSound()
	if #self.dropdownIcons > 0 or #self.menuIcons > 0 then
		IconController:_updateSelectionGroup()
	end
    self.selected:Fire()
    self.toggled:Fire(self.isSelected)
	return self
end

function Icon:deselect(byIcon)
	if self.locked then return self end
	self.isSelected = false
	self:_setToggleItemsVisible(false, byIcon)
	self:_updateNotice()
	self:_updateAll()
	self:_playClickSound()
	if #self.dropdownIcons > 0 or #self.menuIcons > 0 then
		IconController:_updateSelectionGroup()
	end
    self.deselected:Fire()
    self.toggled:Fire(self.isSelected)
	return self
end

function Icon:notify(clearNoticeEvent, noticeId)
	coroutine.wrap(function()
		if not clearNoticeEvent then
			clearNoticeEvent = self.deselected
		end
		if self._parentIcon then
			self._parentIcon:notify(clearNoticeEvent)
		end
		
		local notifComplete = Signal.new()
		local endEvent = self._endNotices:Connect(function()
			notifComplete:Fire()
		end)
		local customEvent = clearNoticeEvent:Connect(function()
			notifComplete:Fire()
		end)
		
		noticeId = noticeId or httpService:GenerateGUID(true)
		self.notices[noticeId] = {
			completeSignal = notifComplete,
			clearNoticeEvent = clearNoticeEvent,
		}
		self.totalNotices += 1
		self:_updateNotice()

		self.notified:Fire(noticeId)
		notifComplete:Wait()
		
		endEvent:Disconnect()
		customEvent:Disconnect()
		notifComplete:Disconnect()
		
		self.totalNotices -= 1
		self.notices[noticeId] = nil
		self:_updateNotice()
	end)()
	return self
end

function Icon:_updateNotice()
	local enabled = true
	if self.totalNotices < 1 then
		enabled = false
	end
	-- Deselect
	if not self.isSelected then
		if (#self.dropdownIcons > 0 or #self.menuIcons > 0) and self.totalNotices > 0 then
			enabled = true
		end
	end
	-- Select
	if self.isSelected then
		if #self.dropdownIcons > 0 or #self.menuIcons > 0 then
			enabled = false
		end
	end
	local value = (enabled and 0) or 1
	self:set("noticeImageTransparency", value)
	self:set("noticeTextTransparency", value)
	self.instances.noticeLabel.Text = (self.totalNotices < 100 and self.totalNotices) or "99+"
end

function Icon:clearNotices()
	self._endNotices:Fire()
	return self
end

function Icon:disableStateOverlay(bool)
	if bool == nil then
		bool = true
	end
	local stateOverlay = self.instances.iconOverlay
	stateOverlay.Visible = not bool
	return self
end


