function renderSource( src )
	src.title = src.title or '\'\'(unspecified title)\'\''

	local result = '';
	if ( src.authors ) then
		local first = true;
		for _, author in pairs( src.authors ) do
			if ( not first ) then
				result = result .. ', ';
			else
				first = false;
			end 
			result = result .. '\'\'' .. author .. '\'\'';
		end
	end
	if ( string.len( result ) ~= 0 ) then
		result = result .. ' ';
	end
	result = result .. src.title;
	if ( src.originaltitle ) then
		result = result .. ' = ' .. src.originaltitle;
	end

	if ( src.publication ) then
		result = result .. ' // \'\'' .. src.publication .. '\'\'';
	end

	if ( src.place or src.publisher or src.year ) then
		result = result .. ' — ';
		if ( src.place ) then
			result = result .. src.place;
			if ( src.publisher or src.year ) then
				result = result .. ': ';
			end
		end
		if ( src.publisher ) then
			result = result .. src.publisher;
			if ( src.year ) then
				result = result .. ', ';
			end
		end
		if ( src.year ) then
			result = result .. src.year;
		end
		result = result .. '.';
	end

	if ( src.issue ) then
		result = result .. ' — № ' .. src.issue .. '.';
	end

	if ( src.page ) then
		local letter;
		if ( src.lang == 'de' ) then letter = 'S.'
		elseif ( src.lang == 'en' ) then letter = 'P.'
		elseif ( src.lang == 'es' ) then letter = 'P.'
		else letter = 'С.' end
		result = result .. ' — ' .. letter .. ' ' .. src.page  .. '.';
	end

	if ( src.isbn ) then
		result = result .. ' — ISBN ' .. src.isbn;
	end

	return {text = result, code = src.code};
end

function renderLink( entityId, text )
	local link = mw.wikibase.sitelink( entityId ) or ( ':d:' .. entityId )
	return '[[' .. link .. '|' .. text .. ']]';
end

function updateWithRef( reference, src )
	if ( reference.snaks.p304 ) then
		src.page = reference.snaks.p304[0].datavalue.value;
	end
	return src;
end

local p = {};

function p.srcQ10695694( ref )
	return renderSource( updateWithRef( ref, {
		code = 'Q10695694',
		lang = 'en',
		authors = { renderLink( 'Q1176320', 'Hunt, D.R.' ) },
		title = 'Новый словарь кактусов',
		originaltitle = 'The New Cactus Lexicon',
		place = 'Milborne Port',
		publisher = 'dh books',
		year = '2006',
		isbn = '978-0-9538134-4-5'
	} ) );
end

function p.srcQ13520496( ref )
	return renderSource( updateWithRef( ref, {
		code = 'Q13520496',
		lang = 'de',
		authors = { renderLink( 'Q1176320', 'Андерсон, Э. Ф.' ) },
		title = 'Большой словарь кактусов',
		originaltitle = 'Das große Kakteen-Lexikon',
		place = renderLink( 'Q1022', 'Stuttgart' ),
		publisher = renderLink( 'Q1372939', 'Eugen Ulmer Verlag' ),
		year = '2005',
		isbn = '978-3-8001-4573-7'
	} ) );
end

function p.srcQ14755870( ref )
	return renderSource( updateWithRef( ref, {
		code = 'Q14755870',
		lang = 'es',
		authors = { renderLink( 'Q614235', 'Glass, C. E.' ),  renderLink( 'Q13218779', 'Maurice, W. A. F.' ) },
		title = 'Nuevos taxa de cactaceas de Nuevo Leon, Mexico',
		publication = renderLink( 'Q5738832', 'Cact. Suc. Mex.'),
		publisher = 'Sociedad Mexicana de Cactología',
		page = '11-21',
		issue = '37',
		year = '1992',
	} ) );
end

function p.srcDefault( ref )
	local entityId = "Q" .. ref.snaks.p248[0].datavalue.value["numeric-id"];
    local label = mw.wikibase.label( entityId ) or '(untranslated title)'
	return renderSource( updateWithRef( ref, {
		code = entityId,
		title = renderLink( entityId, label)
	} ) );
end

return p;