require('strict')

local p = {}

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

function p._collective_work_header(args)
	local current_title = mw.title.getCurrentTitle()
	local basename = args.basename or current_title.rootText
	
	args.title = '[[' .. basename .. '|' .. (args.displayname or basename) .. ']]'
	
	if not args.section and current_title.isSubpage then
		args.section = current_title.subpageText
	end
	
	if args.previous then
		args.previous = '[[' .. basename.. '/' .. args.previous .. '|' .. args.previous .. ']]'
	end
	if args['next'] then
		args['next'] = '[[' .. basename.. '/' .. args['next'] .. '|' .. args['next'] .. ']]'
	end
	
	local notes_table = {}
	local volume = args.volume or args.vol
	if volume then
		table.insert(notes_table, '<p>From volume ' .. volume .. ' of the work.</p>')
	end
	if args.disclaimer then
		table.insert(notes_table, "<p>\'\'\'See also\'\'\' the [[" .. basename.. '/Project Disclaimer|' .. args.disclaimer .. ']].</p>')
	end
	if args.notes then
		table.insert(notes_table, args.notes)
	end
	args.notes = table.concat(notes_table, '\n')
	if args.notes == '' then
		args.notes = nil
	end
	
	local cat = ''
	if not yesno(args.nocat) and current_title:inNamespace(0) then
		if yesno(args.noarticle) then
			cat = '[[Category:' .. basename.. ' articles without text|' .. current_title.subpageText .. ']]'
		else
			cat = '[[Category:' .. basename.. ' articles|' .. current_title.subpageText .. ']]'
		end
	end
	
	return header(args) .. cat
end

function p.collective_work_header(frame)
	return p._collective_work_header(getArgs(frame))
end

return p