local k=require(script.Parent.RetryLayer)local d=game:GetService("\072\116\116\112\083\101\114\118\105\099\101")local N=require(script.Schemes.raw)local y=require(script.Schemes.compressed)local u=1000 local h={schemes={["\114\097\119\047\049"]=N["\114\097\119\047\049"],["\099\111\109\112\114\101\115\115\101\100\047\049"]=y["\099\111\109\112\114\101\115\115\101\100\047\049"]}}function h._unpack(k)if k==nil then return nil end local N=k.scheme if not h.schemes[N]then error(("\085\110\107\110\111\119\110 \115\099\104\101\109\101\058 \037\113"):format(N))end return d:JSONDecode(h.schemes[N].unpack(k.data))end function h._pack(k)k=d:JSONEncode(k)local N if#k>u then N="\099\111\109\112\114\101\115\115\101\100\047\049"else N="\114\097\119\047\049"end return{scheme=N,data=h.schemes[N].pack(k)}end function h.update(d,N,y)local u k.update(d,N,function(k)u=y(h._unpack(k))if u~=nil then return h._pack(u)else return nil end end)return u end function h.read(d,N)return h._unpack(k.read(d,N))end return h
local RetryLayer = require(script.Parent.RetryLayer)

local HttpService = game:GetService("HttpService")

local RawSchemes = require(script.Schemes.raw)
local CompressedSchemes = require(script.Schemes.compressed)

local MINIMUM_LENGTH_TO_COMPRESS = 1000

local DataLayer = {
	schemes = {
		["raw/1"] = RawSchemes["raw/1"];
		["compressed/1"] = CompressedSchemes["compressed/1"];
	}
}

function DataLayer._unpack(value)
	if value == nil then
		return nil
	end

	local scheme = value.scheme

	if not DataLayer.schemes[scheme] then
		error(("Unknown scheme: %q"):format(scheme))
	end

	return HttpService:JSONDecode(DataLayer.schemes[scheme].unpack(value.data))
end

function DataLayer._pack(value)
	value = HttpService:JSONEncode(value)

	local scheme
	if #value > MINIMUM_LENGTH_TO_COMPRESS then
		scheme = "compressed/1"
	else
		scheme = "raw/1"
	end

	return {
		scheme = scheme;
		data = DataLayer.schemes[scheme].pack(value)
	}
end

function DataLayer.update(collection, key, callback)
	local decompressed

	RetryLayer.update(collection, key, function(value)
		decompressed = callback(DataLayer._unpack(value))

		if decompressed ~= nil then
			return DataLayer._pack(decompressed)
		else
			return nil
		end
	end)

	return decompressed
end

function DataLayer.read(collection, key)
	return DataLayer._unpack(RetryLayer.read(collection, key))
end

return DataLayer