-- For functions in which the return value type depends on the input and a condition.

local function ProcessAndExecuteList(...)
	return Execute(ProcessList(...))
end

local function ProcessAndExecuteTable(intbl)
	return ProcessAndExecuteList(TableUnpack(intbl))
end

local function LookupItemInTemplate(intplt,...)
	local outval
	if IsTemplateToBool(intplt) then
		for i=1, TableLength(intplt) do
			-- Ignore items, focus on the locations
			for j=2, TableLength(intplt[i]) do
				if NumericTablePairMatchToBool({...},ValueToTable(intplt[i][j])) then
					outval = intplt[i][1]
				end
			end
		end
	end
	return outval
end

local function RetrieveTemplateLocationFromTemplate(intplt,intplt2)
	if IsTemplateToBool(intplt2) then
		local potentialmatches = RetrieveTemplatesInTemplate(intplt2)
		if TableLength(potentialmatches) > 0 then
			for template=1, TableLength(potentialmatches) do
				print(template,TableToString(nil,potentialmatches[template]),LookupItemInTemplate(intplt2,potentialmatches[template]))
				if DetermineTemplateEqualityToBool(intplt,potentialmatches[template]) then
					return LookupItemInTemplate(intplt2,TableUnpack(potentialmatches[template]))
				end
			end
		end
	end
end

local function Precompute(intplt,inpc,indb)
	local id = RetrieveTemplateLocationFromTemplate(intplt,indb)
	if NotToBool(NilToBool(id)) and NotNilToBool(LookupItemInTemplate(inpc,id)) then
		print("Yes!")
		return LookupItemInTemplate(inpc,id)
	else
		if NilToBool(id) then
			id = GenerateUniqueID()
			indb = IndexTemplateIntoDDB(intplt,id,indb)
		end
		local changetbl = ConcatenateTemplates(AddTemplateEntry({},ExecuteTable(ApplyTemplateToTable({},intplt)),id),inpc)
		local pcid = GenerateUniqueID(indb)
		return LookupItemInTemplate(LookupItemInTemplate(indb,pcid),id),changetbl,indb
	end
end