local w=require((game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")):WaitForChild("\078\101\118\101\114\109\111\114\101"))local z={}z.ClassName="\073\110\112\117\116\073\109\097\103\101\076\105\098\114\097\114\121"z.__index=z function z.new(w)local g=setmetatable({},z)g._spritesheets={}g:_loadSpriteSheets(w)return g end function z._loadSpriteSheets(g,z)for z,H in pairs(z:GetChildren())do g._spritesheets[H.Name]={}for z,C in pairs(H:GetChildren())do if C:IsA("\077\111\100\117\108\101\083\099\114\105\112\116")then g._spritesheets[H.Name][C.Name]=(w(C)).new()end end end end function z.GetSprite(H,w,z,g)local C=H:_pickSheet(w,z,g)if C then return C:GetSprite(w)end return nil end function z.GetScaledImageLabel(H,w,z,g)local C=H:_getImageInstance("\073\109\097\103\101\076\097\098\101\108",w,z or"\068\097\114\107",g)if not C then return nil end local x=C.Size local Q=x.Y.Offset/x.X.Offset local h=Instance.new("\085\073\065\115\112\101\099\116\082\097\116\105\111\067\111\110\115\116\114\097\105\110\116")h.DominantAxis=Enum.DominantAxis.Height h.AspectRatio=Q h.Parent=C C.Size=UDim2.new(1,0,1,0)return C end function z._pickSheet(H,w,z,g)local function C(g)local H=g[z]if H and H:HasSprite(w)then return H end for z,g in pairs(g)do if g:HasSprite(w)then return g end end end local x=H._spritesheets[g]local Q=x and C(x)if Q then return Q end for w,z in pairs(H._spritesheets)do local g=C(z)if g then return g end end warn("\091\073\110\112\117\116\073\109\097\103\101\076\105\098\114\097\114\121\093 - \085\110\097\098\108\101 \116\111 \102\105\110\100 \115\112\114\105\116\101 \102\111\114",tostring(w),"\116\121\112\101",typeof(w))return nil end function z._getImageInstance(C,w,z,g,H)local x=C:_pickSheet(z,g,H)if x then return(x:GetSprite(z)):Get(w)end return nil end return z.new(script:WaitForChild("\083\112\114\105\116\101\115\104\101\101\116\115"))
--- Input image library
-- @module InputImageLibrary

local require = require(game:GetService("ReplicatedStorage"):WaitForChild("Nevermore"))

local InputImageLibrary = {}
InputImageLibrary.ClassName = "InputImageLibrary"
InputImageLibrary.__index = InputImageLibrary

function InputImageLibrary.new(parentFolder)
	local self = setmetatable({}, InputImageLibrary)

	self._spritesheets = {}
	self:_loadSpriteSheets(parentFolder)

	return self
end

function InputImageLibrary:_loadSpriteSheets(parentFolder)
	for _, platform in pairs(parentFolder:GetChildren()) do
		self._spritesheets[platform.Name] = {}
		for _, style in pairs(platform:GetChildren()) do
			if style:IsA("ModuleScript") then
				self._spritesheets[platform.Name][style.Name] = require(style).new()
			end
		end
	end
end

---
-- @treturn Sprite
function InputImageLibrary:GetSprite(index, preferredStyle, preferredPlatform)
	local sheet = self:_pickSheet(index, preferredStyle, preferredPlatform)
	if sheet then
		return sheet:GetSprite(index)
	end

	return nil
end

function InputImageLibrary:GetScaledImageLabel(keyCode, preferredStyle, preferredPlatform)
	local image = self:_getImageInstance("ImageLabel", keyCode, preferredStyle or "Dark", preferredPlatform)
	if not image then
		return nil
	end

	local size = image.Size
	local ratio = size.Y.Offset/size.X.Offset

	local uiAspectRatio = Instance.new("UIAspectRatioConstraint")
	uiAspectRatio.DominantAxis = Enum.DominantAxis.Height
	uiAspectRatio.AspectRatio = ratio
	uiAspectRatio.Parent = image

	image.Size = UDim2.new(1, 0, 1, 0)

	return image
end

function InputImageLibrary:_pickSheet(index, preferredStyle, preferredPlatform)
	local function findSheet(platformSheets)
		local preferredSheet = platformSheets[preferredStyle]
		if preferredSheet and preferredSheet:HasSprite(index) then
			return preferredSheet
		end

		-- otherwise search (yes, we double hit a sheet)
		for _, sheet in pairs(platformSheets) do
			if sheet:HasSprite(index) then
				return sheet
			end
		end
	end

	local sheet = self._spritesheets[preferredPlatform]
	local preferredSheet = sheet and findSheet(sheet)
	if preferredSheet then
		return preferredSheet
	end

	-- otherwise search (repeats preferred :/ )
	for _, platformSheets in pairs(self._spritesheets) do
		local foundSheet = findSheet(platformSheets)
		if foundSheet then
			return foundSheet
		end
	end

	warn("[InputImageLibrary] - Unable to find sprite for", tostring(index), "type", typeof(index))

	return nil
end


function InputImageLibrary:_getImageInstance(instanceType, index, preferredStyle, preferredPlatform)
	local sheet = self:_pickSheet(index, preferredStyle, preferredPlatform)
	if sheet then
		return sheet:GetSprite(index):Get(instanceType)
	end

	return nil
end

return InputImageLibrary.new(script:WaitForChild("Spritesheets"))
