local w=require(script.SettingsTemplate)local function z(w)if typeof(w)=="\116\097\098\108\101"then local c={}for w,o in w do if type(o)=="\116\097\098\108\101"then c[w]=z(o)else c[w]=o end end return c else return w end end return function(c)local o={}for w,l in w do if not c[w]then o[w]=z(l[2])else if type(c[w])~=l[1]then o[w]=z(l[2])else o[w]=z(c[w])end end end for w,c in c.Admins do if not tonumber(w)then warn("\083\101\116\116\105\110\103\115\046\065\100\109\105\110\115\091\""..(w.."\"\093 \058 \073\110\118\097\108\105\100 \116\121\112\101 \102\111\114 \076\101\118\101\108 \105\110 \065\100\109\105\110\115\059 \101\120\112\101\099\116\101\100 \110\117\109\098\101\114\044 \103\111\116 \115\116\114\105\110\103"))else o.Admins[w]=nil o.Admins[tonumber(w)]=z(c)for z,c in o.Admins[tonumber(w)]do if type(c)=="\116\097\098\108\101"then if c.Temporary then o.Admins[tonumber(w)][z]=nil end end end end end return o end
local Template = require(script.SettingsTemplate)

local function CloneTable(array)
	if typeof(array) == 'table' then
		local clone = {}
		for i,v in array do
			if type(v) == "table" then
				clone[i] = CloneTable(v)
			else
				clone[i] = v
			end
		end
		return clone
	else
		return array;
	end
end

return function(Settings)
	local ReturnedSettings = {};
	-- Check for missing keys, and add them with default values.
	-- For matching references;
	-- If a key does not exist, add the key from the template
	-- If a key does exist, verify the type; if type mismatches, set the type from template

	-- template; [Key]={type:string,defaultValue:[typeRelative]}
	-- settings; [Key]=Value

	for Key,Data in Template do
		if not Settings[Key] then
			ReturnedSettings[Key] = CloneTable(Data[2]);
		else
			if type(Settings[Key]) ~= Data[1] then
				ReturnedSettings[Key] = CloneTable(Data[2]);
			else
				ReturnedSettings[Key] = CloneTable(Settings[Key]);
			end
		end
	end
	
	-- sanitary thing for the admins thing because mixing strings and numbers does NOT go well for json.
	for Level, AdminData in Settings.Admins do
		if not tonumber(Level) then
			-- it can't be a string. warn the user and remove from the table.
			warn("Settings.Admins[\"" .. Level .. "\"] : Invalid type for Level in Admins; expected number, got string")
		else
			ReturnedSettings.Admins[Level] = nil -- clear the previous iteration
			ReturnedSettings.Admins[tonumber(Level)] = CloneTable(AdminData)

			-- CHECK IF THE ADMIN WAS TEMPORARY!!
			for k, v in ReturnedSettings.Admins[tonumber(Level)] do
				if type(v) == "table" then
					if v.Temporary then
						ReturnedSettings.Admins[tonumber(Level)][k] = nil;
					end
				end
			end
		end
	end
	
	return ReturnedSettings;
end;