local a=game:GetService("\068\097\116\097\083\116\111\114\101\083\101\114\118\105\099\101")local Z=require(script.Parent.Parent.Promise)local T={}T.__index=T function T.Get(a)return(Z.async(function(Z)Z(((a.orderedDataStore:GetSortedAsync(false,1)):GetCurrentPage())[1])end)):andThen(function(T)if T then local D=T.value a.dataStore2:Debug("\109\111\115\116 \114\101\099\101\110\116 \107\101\121",T)a.mostRecentKey=D return Z.async(function(Z)Z(a.dataStore:GetAsync(D))end)else a.dataStore2:Debug("\110\111 \114\101\099\101\110\116 \107\101\121")return nil end end)end function T.Set(T,a)local D=(T.mostRecentKey or 0)+1 return((Z.async(function(Z)T.dataStore:SetAsync(D,a)Z()end)):andThen(function()return(Z.promisify(function()T.orderedDataStore:SetAsync(D,D)end))()end)):andThen(function()T.mostRecentKey=D end)end function T.new(Z)local D=Z.Name..("\047"..Z.UserId)local U={dataStore2=Z;dataStore=a:GetDataStore(D),orderedDataStore=a:GetOrderedDataStore(D)}return setmetatable(U,T)end return T


local DataStoreService = game:GetService("DataStoreService")
local Promise = require(script.Parent.Parent.Promise)

local OrderedBackups = {}
OrderedBackups.__index = OrderedBackups

function OrderedBackups:Get()
	return Promise.async(function(resolve)
		resolve(self.orderedDataStore:GetSortedAsync(false, 1):GetCurrentPage()[1])
	end):andThen(function(mostRecentKeyPage)
		if mostRecentKeyPage then
			local recentKey = mostRecentKeyPage.value
			self.dataStore2:Debug("most recent key", mostRecentKeyPage)
			self.mostRecentKey = recentKey

			return Promise.async(function(resolve)
				resolve(self.dataStore:GetAsync(recentKey))
			end)
		else
			self.dataStore2:Debug("no recent key")
			return nil
		end
	end)
end

function OrderedBackups:Set(value)
	local key = (self.mostRecentKey or 0) + 1

	return Promise.async(function(resolve)
		self.dataStore:SetAsync(key, value)
		resolve()
	end):andThen(function()
		return Promise.promisify(function()
			self.orderedDataStore:SetAsync(key, key)
		end)()
	end):andThen(function()
		self.mostRecentKey = key
	end)
end

function OrderedBackups.new(dataStore2)
	local dataStoreKey = dataStore2.Name .. "/" .. dataStore2.UserId

	local info = {
		dataStore2 = dataStore2,
		dataStore = DataStoreService:GetDataStore(dataStoreKey),
		orderedDataStore = DataStoreService:GetOrderedDataStore(dataStoreKey),
	}

	return setmetatable(info, OrderedBackups)
end

return OrderedBackups
