-- Replica creation:

rev_ReplicaCreate.OnClientEvent:Connect(function(param1, param2) -- (top_replica_id, {replica_data}) OR (top_replica_id, {creation_data}) or ({replica_package})
	--[[
	param1 description:
		top_replica_id = replica_id
		OR
		replica_package = {{replica_id, creation_data}, ...}
	param2 description:
		replica_data = {replica_class, replica_tags, data_table, parent_id / 0, write_lib_module / nil}
		OR
		creation_data = {["replica_id"] = replica_data, ...}
	--]]
	local created_replicas = {}
	-- Unpacking replica data:
	if type(param1) == "table" then -- Replica package
		table.sort(param1, function(a, b) -- Sorting top level replicas by their id
			return a[1] < b[1]
		end)
		for _, replica_branch_entry in ipairs(param1) do
			CreateReplicaBranch(replica_branch_entry[2], created_replicas)
		end
	elseif param2[1] ~= nil then -- One replica data
		CreateReplicaBranch({[tostring(param1)] = param2}, created_replicas)
	else -- Creation data table
		CreateReplicaBranch(param2, created_replicas)
	end
	-- Broadcasting replica creation:
	table.sort(created_replicas, function(a, b)
		return a.Id < b.Id
	end)
	-- 1) Child added:
	for _, replica in ipairs(created_replicas) do
		local parent_replica = replica.Parent
		if parent_replica ~= nil then
			local child_listener_table = ChildListeners[parent_replica.Id]
			if child_listener_table ~= nil then
				for i = 1, #child_listener_table do
					child_listener_table[i](replica)
				end
			end
		end
	end
	-- 2) New Replica and Replica of class created:
	for _, replica in ipairs(created_replicas) do
		NewReplicaSignal:Fire(replica)
		local class_listener_signal = ClassListeners[replica.Class]
		if class_listener_signal ~= nil then
			class_listener_signal:Fire(replica)
		end
	end
end)
