-- @brief
--  Generic wrapper for source templates.
-- 
-- @author
--  [[meta:User:Danny B.]]
local Source = {}
----------------------------------------


local Error = require( "Module:Error" )


-- @brief
--  Write the citation template for given source.
-- 
-- @param
--  frame The current frame object
-- 
-- @return
--  Preprocessed wikitext
function Source.print( frame )
	
	local source = frame.args[1]
	
	if not source then
		error( "Missing first argument – name of the source module" )
	end
	
	source = mw.text.trim( source )
	
	local sourceObjectExist, sourceObject = pcall( require, frame:getTitle() .. "/" .. source )
	
	if not sourceObjectExist then
		error( "Missing \"" .. frame:getTitle() .. "/" .. source .. "\" module" )
	end
	
	if sourceObject.editions and not sourceObject.editions.switch then
		error( "\"switch\" field is missing in " .. frame:getTitle() .. "/" .. source .. ".editions" )
	end
	
	local output
	local templateArgs = frame:getParent().args
	local errorData = { template = source }
	local args = {}
	local errors = {}
	local data = {}
	
	for param, value in pairs( templateArgs ) do
		value = mw.text.trim( value )
		if value ~= "" then
			args[param] = value
		end
	end
	
	for param, value in pairs( sourceObject.arguments or {} ) do
		if value.required and not args[param] then
			table.insert( errors, { missingValue = { paramName = param, paramDesc = value.description } } )
		end
	end

	if sourceObject.editions and args[sourceObject.editions.switch] and not sourceObject.editions[args[sourceObject.editions.switch]] then
		table.insert( errors, { unknownValue = { paramName = param, paramDesc = value.description } } )
	end
	
	if sourceObject.fill then
		sourceObject.data = {}
		sourceObject.errors = {}
		sourceObject.fill( args )
		data = sourceObject.data
		errors = sourceObject.errors
	end
	
	if next( errors ) then
		local errorTexts = {}
		local method = mw.title.getCurrentTitle().namespace == 0 and "getText" or "getMessage"
		for index, err in ipairs( errors ) do
			for key, value in pairs( err ) do
				errorData[key] = value
			end
			table.insert( errorTexts, Error[method]( errorData ) )
		end
		output = table.concat( errorTexts, "<br />" )
		output = frame:preprocess( output )
	else
		local citeArgs = {
			["zdroj"] = source
		}
		
		if not sourceObject.fill then
			if sourceObject.common then
				for param, value in pairs( sourceObject.common ) do
					data[param] = ( type( value ) == "function" and value( args ) or value )
				end
			end
			if sourceObject.editions then
				for param, value in pairs( sourceObject.editions[args[sourceObject.editions.switch]] ) do
					data[param] = ( type( value ) == "function" and value( args ) or value )
				end
			end
			if sourceObject.arguments then
				for param, value in pairs( sourceObject.arguments ) do
					data[( value.fillIn or param )] = ( value.fill and value.fill( args ) or args[param] )
				end
			end
		end
		
		for param, value in pairs( data ) do
			citeArgs[param] = value
		end
		
		output = frame:expandTemplate({
			title = "Citácia " .. sourceObject.cite,
			args = citeArgs
		})
	end
	
	return output
	
end


----------------------------------------
return Source