require('strict')

local p = {}

local getArgs = require('Module:Arguments').getArgs
local header = require('Module:Header')._header

function p._EB1911(args, argsWithBlanks)
	argsWithBlanks = argsWithBlanks or {}
	
	local current_title = mw.title.getCurrentTitle()
	
	local tracking_cats = {}
	
	args.title = '[[1911 Encyclopædia Britannica|1911 \'\'Encyclopædia Britannica\'\']]'
	if args.volume then
		args.title = args.title .. ', [[1911 Encyclopædia Britannica/Volume ' .. args.volume .. '|Volume ' .. args.volume .. ']]'
	elseif current_title:inNamespaces(0) then
		table.insert(tracking_cats, '[[Category:' .. 'EB 1911 no volume' .. ']]')
	end
	
	local newArgs = {}
	local nilArgs = {}
	for k, v in pairs(args) do
		local newkey = string.gsub(string.gsub(k, '^author', 'section-author'), '_', '-')
		if newkey ~= tostring(k) then
			if argsWithBlanks[newkey] then
				table.insert(tracking_cats, '[[Category:' .. 'Pages using duplicate arguments in template calls' .. ']]')
			end
			if not args[newkey] then
				newArgs[newkey] = v
			end
			table.insert(nilArgs, k)
		end
	end
	for k, v in pairs(nilArgs) do
		args[v] = nil
	end
	for k, v in pairs(newArgs) do
		args[k] = v
	end
	
	if not args.section then
		if current_title.baseText == '1911 Encyclopædia Britannica' then
			args.section = current_title.subpageText
		else
			local parent = mw.title.new(current_title.baseText, current_title.nsText)
			args.section = ' - [[' .. parent.prefixedText .. '|' .. parent.subpageText .. ']] ' .. current_title.subpageText
		end
	end
	
	if args.previous then
		args.previous = '[[' .. current_title.nsText .. ':' .. current_title.rootText .. '/' .. args.previous .. '|' .. args.previous .. ']]'
	end
	if args['next'] then
		args['next'] = '[[' .. current_title.nsText .. ':' .. current_title.rootText .. '/' .. args['next'] .. '|' .. args['next'] .. ']]'
	end
	
	if current_title:inNamespaces(0) then
		args.sortkey = args.sortkey or args.defaultsort or current_title.subpageText
	end
	
	args['allow-explicit-formatting'] = true
	
	if not args.notes then
		args.wikipedia = args.wikipedia or argsWithBlanks.wikipedia
		args.notes = '\'\'\'See also\'\'\' '
		if args.wikipedia == '' then
		elseif args.wikipedia then
			args.notes = args.notes .. '[[w:' .. args.wikipedia .. '|' .. args.wikipedia .. ']]'
			if args.wikipedia2 then
				args.notes = args.notes .. ' and [[w:' .. args.wikipedia2 .. '|' .. args.wikipedia2 .. ']]'
			end
			args.notes = args.notes .. ' on [[w:Main_Page|Wikipedia]]; '
		else
			args.notes = args.notes .. '[[w:' .. current_title.subpageText .. '|' .. current_title.subpageText .. ']] on [[w:Main_Page|Wikipedia]]; '
		end
		if args['other-projects'] then
			args.notes = args.notes .. args['other-projects'] .. '; and '
		elseif args.wikipedia ~= '' then
			args.notes = args.notes .. 'and '
		end
		args.notes = args.notes .. 'our [[Wikisource:WikiProject 1911 Encyclopædia Britannica/Project Disclaimer|1911 Encyclopædia Britannica disclaimer]].'
	end
	
	if args['extra-notes'] then
		args.notes = args.notes .. ' ' .. args['extra-notes']
	end
	
	if args.wiktionary then
		table.insert(tracking_cats, '[[Category:' .. 'EB 1911 wiktionary links' .. ']]')
	end
	
	return header(args) .. table.concat(tracking_cats)
end

function p.EB1911(frame)
	return p._EB1911(getArgs(frame), getArgs(frame, {removeBlanks = false}))
end

return p