local Y=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 w=Y("\084\097\098\108\101")local T=Y("\068\097\116\097\083\116\111\114\101\068\101\108\101\116\101\084\111\107\101\110")local t={}t.ClassName="\068\097\116\097\083\116\111\114\101\087\114\105\116\101\114"t.__index=t function t.new()local Y=setmetatable({},t)Y._rawSetData={}Y._writers={}return Y end function t.SetRawData(T,Y)T._rawSetData=w.DeepCopy(Y)end function t.AddWriter(T,Y,w)assert(type(Y)=="\115\116\114\105\110\103")assert(not T._writers[Y])assert(w)T._writers[Y]=w end function t.WriteMerge(w,Y)Y=Y or{}for w,t in pairs(w._rawSetData)do if t==T then Y[w]=nil else Y[w]=t end end for t,M in pairs(w._writers)do if w._rawSetData[t]~=nil then warn(("\091\068\097\116\097\083\116\111\114\101\087\114\105\116\101\114\046\087\114\105\116\101\077\101\114\103\101\093 - \079\118\101\114\119\114\105\116\116\105\110\103 \107\101\121 \037\113 \097\108\114\101\097\100\121 \115\097\118\101\100 \097\115 \114\097\119\068\097\116\097 \119\105\116\104 \097 \119\114\105\116\101\114"):format(tostring(t)))end local p=M:WriteMerge(Y[t])if p==T then Y[t]=nil else Y[t]=p end end return Y end return t
--- Captures a snapshot of data to write and then merges it with the original
-- @classmod DataStoreWriter

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

local Table = require("Table")
local DataStoreDeleteToken = require("DataStoreDeleteToken")

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

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

	self._rawSetData = {}
	self._writers = {}

	return self
end

function DataStoreWriter:SetRawData(data)
	self._rawSetData = Table.DeepCopy(data)
end

function DataStoreWriter:AddWriter(name, value)
	assert(type(name) == "string")
	assert(not self._writers[name])
	assert(value)

	self._writers[name] = value
end

-- Do merge here
function DataStoreWriter:WriteMerge(original)
	original = original or {}

	for key, value in pairs(self._rawSetData) do
		if value == DataStoreDeleteToken then
			original[key] = nil
		else
			original[key] = value
		end
	end

	for key, writer in pairs(self._writers) do
		if self._rawSetData[key] ~= nil then
			warn(("[DataStoreWriter.WriteMerge] - Overwritting key %q already saved as rawData with a writer")
				:format(tostring(key)))
		end

		local result = writer:WriteMerge(original[key])
		if result == DataStoreDeleteToken then
			original[key] = nil
		else
			original[key] = result
		end
	end

	return original
end

return DataStoreWriter