---------------------------------------------------------

function conf.waitForOverride() -- waits for server to override conf with gamemode values
	while not conf.override do
		wait()
	end
end

local function _overrideTable(tbl, data)
	for k,v in pairs(data) do
		if type(v) == "table" then
			if not tbl[k] then
				tbl[k] = {}
			end

			_overrideTable(tbl[k], v)

		else
			tbl[k] = v
		end
	end
end

function conf.createOverride(name, data)
	if _overrides[name] then
		print(string.format("Configuration override %s already exists", name))
		return
	end

	_overrides[name] = data
end

function conf.applyOverride(data)
	local override

	if type(data) == "table" then
		override = data
	else
		override = _overrides[data]
	end

	if override then
		_overrideTable(conf, override)

		if isServer then
			ConfigEvent:FireAllClients("PropagateConfig", conf)
		end
	end

	return conf
end

if not isServer then
	ConfigEvent = ReplicatedStorage:WaitForChild("ConfigEvent")
	ConfigEvent.OnClientEvent:Connect(function(cmd, newConf)
		if cmd == "PropagateConfig" then
			_overrideTable(conf, newConf)
		end
	end)
else
	ConfigEvent = ReplicatedStorage:FindFirstChild("ConfigEvent")
	if ConfigEvent == nil then
		ConfigEvent = Instance.new("RemoteEvent", ReplicatedStorage)
		ConfigEvent.Name = "ConfigEvent"
	end

	Players.PlayerAdded:Connect(function(player)
		ConfigEvent:FireClient(player, "PropagateConfig", conf)
	end)
end
