--!native
--!nonstrict
--!optimize 2
-- Compiled with roblox-ts v2.3.0
local TS = _G[script]
local t = TS.import(script, TS.getModule(script, "@rbxts", "t").lib.ts).t
local isValidCapacity = t.intersection(t.integer, t.numberMin(0))
local LFUCache
do
	LFUCache = setmetatable({}, {
		__tostring = function()
			return "LFUCache"
		end,
	})
	LFUCache.__index = LFUCache
	function LFUCache.new(...)
		local self = setmetatable({}, LFUCache)
		return self:constructor(...) or self
	end
	function LFUCache:constructor(capacity)
		self.cache = {}
		self.frequencyHash = {
			[0] = {},
		}
		self.minimumFrequency = 0
		local _arg0 = isValidCapacity(capacity)
		assert(_arg0, "Capacity must be a number greater than 0")
		self.capacity = capacity
	end
	function LFUCache:get(key)
		local cache = self.cache
		local _key = key
		local item = cache[_key]
		if not item then
			return nil
		end
		local frequencyHash = self.frequencyHash
		local frequency = item.frequency + 1
		local _arg0 = frequency - 1
		local _result = frequencyHash[_arg0]
		if _result ~= nil then
			local _key_1 = key
			_result[_key_1] = nil
		end
		item.frequency = frequency
		local currentSet = frequencyHash[frequency]
		if currentSet then
			local _key_1 = key
			currentSet[_key_1] = true
		else
			local _arg1 = {
				[key] = true,
			}
			frequencyHash[frequency] = _arg1
		end
		local _key_1 = key
		cache[_key_1] = item
		local minimumFrequency = self.minimumFrequency
		if next(frequencyHash[minimumFrequency]) == nil then
			self.minimumFrequency = minimumFrequency + 1
		end
		return item.value
	end
	function LFUCache:set(key, value)
		local capacity = self.capacity
		if capacity < 1 then
			return nil
		end
		local _binding = self
		local cache = _binding.cache
		local frequencyHash = _binding.frequencyHash
		local minimumFrequency = _binding.minimumFrequency
		local _key = key
		local cached = cache[_key]
		if cached then
			local frequency = cached.frequency + 1
			local _arg0 = frequency - 1
			local _result = frequencyHash[_arg0]
			if _result ~= nil then
				local _key_1 = key
				_result[_key_1] = nil
			end
			cached.frequency = frequency
			cached.value = value
			local _key_1 = key
			cache[_key_1] = cached
			local currentSet = frequencyHash[frequency]
			if currentSet then
				local _key_2 = key
				currentSet[_key_2] = true
			else
				local _arg1 = {
					[key] = true,
				}
				frequencyHash[frequency] = _arg1
			end
			if next(frequencyHash[minimumFrequency]) == nil then
				self.minimumFrequency = minimumFrequency + 1
			end
			return nil
		end
		-- ▼ ReadonlyMap.size ▼
		local _size = 0
		for _ in cache do
			_size += 1
		end
		-- ▲ ReadonlyMap.size ▲
		if _size == capacity then
			local hash = frequencyHash[minimumFrequency]
			local keyToEvict = next(hash)
			local _arg0 = keyToEvict ~= nil
			assert(_arg0, "keyToEvict should be defined")
			hash[keyToEvict] = nil
			cache[keyToEvict] = nil
			if next(hash) == nil then
				frequencyHash[minimumFrequency] = nil
			end
		end
		local item = {
			frequency = 1,
			value = value,
		}
		local _key_1 = key
		cache[_key_1] = item
		local currentSet = frequencyHash[1]
		if currentSet then
			local _key_2 = key
			currentSet[_key_2] = true
		else
			local _arg1 = {
				[key] = true,
			}
			frequencyHash[1] = _arg1
		end
		self.minimumFrequency = 1
	end
	LFUCache.instanceof = function(value)
		local _value = value
		local _condition = type(_value) == "table"
		if _condition then
			_condition = getmetatable(value) == LFUCache
		end
		return _condition
	end
end
local metatable = LFUCache
metatable.__tostring = function()
	return "LFUCache"
end
return {
	LFUCache = LFUCache,
}
