Documentation for this module may be created at Մոդուլ:Քույրհղումներ/doc

local p = {};

-- Configuration data
local config = require( 'Մոդուլ:Քույրհղումներ/config' );

-- Modules 
local getArgs = require( 'Module:Arguments' ).getArgs;
local errorMsg = require( 'Module:Error' ).error;
local yesno = require( 'Module:Yesno' );

-- Variables
local modules = {};

-- Helpers
local function formatProject( frame, args, item )
	local out = '';
	local context = frame;

	local entityId
	if args.from and args.from ~= '' then
		entityId = args.from
	else
		entityId = mw.wikibase.getEntityIdForCurrentPage()
	end

	if entityId and item.sitelink then
		item.value = mw.wikibase.getSitelink( entityId, item.sitelink )
	elseif item[ 'module' ] and item[ 'function' ] then
		if modules[ item[ 'module' ] ] == nil then
			modules[ item[ 'module' ] ] = require( 'Module:' .. item[ 'module' ] );
		end

		context.args = item[ 'args' ] or {}
		context.args.from = entityId

		item.wikiValue = modules[ item[ 'module' ] ][ item[ 'function' ] ]( context );
		-- add wiki prefix
		if item.wikiValue ~= nil and item.wikiValue ~= '' and item.wikiPrefix ~= nil and item.wikiPrefix ~= '' then
			item.wikiValue = item.wikiPrefix.. ':' .. item.wikiValue;
		end
		-- override value if local value does not exist
		if item.value == nil or item.value == '' then
			item.value = item.wikiValue;
		end
	end
	
	if item.value ~= nil and item.value ~= '' then
		-- format prefix
		if string.find( item.value, '^' .. item.prefix .. ':' ) ~= nil then
			item.prefixFormatted = '';
		else
			item.prefixFormatted = item.prefix .. ':';
		end
		-- replace variables
		if item.text ~= nil and item.text ~= '' then
			item.text = string.gsub( item.text, '%$1', '[[' .. item.prefixFormatted .. item.value .. '|' .. mw.title.getCurrentTitle().text  .. ']]' );
		end
		-- format image
		if item.imageTemplate ~= nil and item.imageTemplate ~= '' then
			item.imageFormatted = frame:expandTemplate{ title = item.imageTemplate, args = { item.value, size = config.params.itemImageSize } };
		elseif item.image ~= nil and item.image ~= '' then
			item.imageFormatted = '[[File:' .. item.image .. '|link=|alt=|' .. config.params.itemImageSize .. 'px]]';
		end
		-- format wikilink
		if item.text ~= nil then
			out = out .. item.text;
		end
	end
	
	return out;
end

local function formatCategory( value )
	return '[[Category:' .. value .. ']]';
end

local function getParamsList( frame, args, list )
	local result = {};
	
	for i, item in ipairs( list ) do
		local value = args[item.id];
		if value == nil or value ~= '-' then
			item.localValue = value;
			item.value = value;
			item.formatted = formatProject( frame, args, item );
			if item.formatted ~= '' then
				table.insert( result, item );
			end
		end
	end
	
	return result;
end

-- Renders
local function renderProjects( frame, list )
	local res = mw.html.create();
	
	for i, item in ipairs( list ) do
		-- wrapper
		local tbl = res:tag( 'table' )
			:addClass( 'toccolours' )
			:css('width', '100%');

		local tr = tbl:tag('tr');
		
		local image = tr:tag('td'):css('width', 'auto'):wikitext(item.imageFormatted);
		local content = tr:tag('td'):css('width', '100%'):wikitext(item.formatted);
	end
	
	return res;
end

local function render( frame, interprojects )
	local interprojectsFormatted = renderProjects( frame, interprojects );
	return interprojectsFormatted;
end

-- Main
function p.main( frame )
	local args = getArgs( frame, { wrappers = config.params.wrappers } );
	-- get parameters list
	local interprojects = getParamsList( frame, args, config['interprojects'] );
	-- wrapper
	local res = mw.html.create();
	-- render body
	if next( interprojects ) ~= nil then
		local body = render( frame, interprojects );
		res:node( body );
	else
		res:wikitext( formatCategory( config.categories['empty'] ) )
	end

	return tostring( res );
end

return p;