-- Set constants
local CONFIG_MODULE = 'Module:Shortcut/config'

-- Load required modules
local checkType = require('libraryUtil').checkType
local yesno = require('Module:Yesno')

local p = {}

local function message
	return mw.message.newRawMessage:plain
end

local function makeCategoryLink(cat)
	return string.format
end

function p._main
	checkType
	checkType
	options = options or {}
	frame = frame or mw.getCurrentFrame
	cfg = cfg or mw.loadData(CONFIG_MODULE)
	local templateMode = options.template and yesno(options.template)
	local redirectMode = (options.redirect or options['넘겨주기']) and yesno(options.redirect or options['넘겨주기'])
	local isCategorized = not options.category or yesno(options.category) ~= false

	-- Validate shortcuts
	for i, shortcut in ipairs(shortcuts) do
		if type(shortcut) ~= 'string' or #shortcut < 1 then
			error(message, 2)
		end
	end

	-- Make the list items. These are the shortcuts plus any extra lines such
	-- as options.msg.
	local listItems = {}
	for i, shortcut in ipairs(shortcuts) do
		local templatePath, prefix
		if templateMode then
			-- Namespace detection
			local titleObj = mw.title.new
			if titleObj.namespace == 10 then
				templatePath = titleObj.fullText
			else
				templatePath = shortcut
			end
			prefix = options['pre' .. i] or options.pre or ''
		end
		if options.target and yesno(options.target) then
			listItems[i] = templateMode
				and string.format
				or string.format
		else
			listItems[i] = frame:expandTemplate{
				title = '안넘겨주기',
				args = templateMode and {templatePath, shortcut} or {shortcut, shortcut}
			if templateMode then
				listItems[i] = string.format
			end
		end
	end
	table.insert

	-- Return an error if we have nothing to display
	if #listItems < 1 then
		local msg = cfg['no-content-error']
		msg = string.format
		if isCategorized and cfg['no-content-error-category'] then
			msg = msg .. makeCategoryLink(cfg['no-content-error-category'])
		end
		return msg
	end

	local root = mw.html.create
	root:wikitext
	-- Anchors
	local anchorDiv = root
	for i, shortcut in ipairs(shortcuts) do
		local anchor = mw.uri.anchorEncode(shortcut)
		anchorDiv:tag('span'):attr
	end

	-- Shortcut heading
	local shortcutHeading
	do
		local nShortcuts = #shortcuts
		if nShortcuts > 0 then
			local headingMsg = options['shortcut-heading'] or
				redirectMode and cfg['redirect-heading'] or
				cfg['shortcut-heading']
			shortcutHeading = message
			shortcutHeading = frame:preprocess(shortcutHeading)
		end
	end

	-- Shortcut box
	local shortcutList = root
	if options.float and options.float:lower == 'left' then
		shortcutList:addClass('module-shortcutboxleft')
	end
	if options.clear and options.clear ~= '' then
		shortcutList:css
	end
	if shortcutHeading then
		shortcutList
	end
	local list = shortcutList:tag('ul')
	for i, item in ipairs(listItems) do
		list:tag('li'):wikitext(item)
	end
	return tostring(root)
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)

	-- Separate shortcuts from options
	local shortcuts, options = {}, {}
	for k, v in pairs(args) do
		if type(k) == 'number' then
			shortcuts[k] = v
		else
			options[k] = v
		end
	end

	-- Compress the shortcut array, which may contain nils.
	local function compressArray(t)
		local nums, ret = {}, {}
		for k in pairs(t) do
			nums[#nums + 1] = k
		end
		table.sort(nums)
		for i, num in ipairs(nums) do
			ret[i] = t[num]
		end
		return ret
	end
	shortcuts = compressArray(shortcuts)

	return p._main
end