local yesno = require('Module:Yesno')
local mList = require('Module:List')
local mTableTools = require('Module:TableTools')
local mMessageBox = require('Module:Message box')

local p = {}

function p.main(frame)
	local origArgs = frame:getParent.args
	local args = {}
	for k, v in pairs(origArgs) do
		v = v:match('^%s*(.-)%s*$')
		if v ~= '' then
			args[k] = v
		end
	end
	return p._main(args)
end

function p._main(args)
	local modules = mTableTools.compressSparseArray(args)
	local box = p.renderBox(modules)
	local trackingCategories = p.renderTrackingCategories
	return box .. trackingCategories
end

function p.renderBox(modules)
	local boxArgs = {}
	if #modules < 1 then
		boxArgs.text = ''
	else
		local moduleLinks = {}
		for i, module in ipairs(modules) do
			module = module:gsub
			moduleLinks[i] = string.format
			local maybeSandbox = mw.title.new(module .. '/연습장')
			if maybeSandbox and maybeSandbox.exists then
				moduleLinks[i] = moduleLinks[i] .. string.format(' (연습장)', maybeSandbox.fullText)
			end
		end
		local moduleList = mList.makeList
		local title = mw.title.getCurrentTitle
		if title.subpageText == "설명문서" then
			title = title.basePageTitle
		end
		if title.contentModel == "Scribunto" then
			boxArgs.text = '이 모듈은 다음의 다른 모듈에 의존합니다:' .. moduleList
		else
			boxArgs.text = '이 틀은 루아를 사용합니다:\n' .. moduleList
		end
	end
	boxArgs.type = 'notice'
	boxArgs.small = true
	boxArgs.image = '30px|alt=|link='
	return mMessageBox.main
end

function p.renderTrackingCategories
	if yesno(args.nocat) then
		return ''
	end

	local cats = {}

	-- Error category
	if #modules < 1 then
		cats[#cats + 1] = '오류가 있는 루아 틀'
	end

	-- Lua templates category
	titleObj = titleObj or mw.title.getCurrentTitle
	local subpageBlacklist = {
		doc = true,
		sandbox = true,
		sandbox2 = true,
		testcases = true,
		['설명문서'] = true,
		['연습장']= true,
		['시험장'] = true
	if not subpageBlacklist[titleObj.subpageText] then
		local protCatName
		if titleObj.namespace == 10 then
			local category = args.category
			if not category then
				local categories = {
					['모듈:String'] = '루아 String 틀',
					['모듈:Math'] = '루아 Math 틀',
					['모듈:Hangul'] = '루아 Hangul 틀',
					['모듈:BaseConvert'] = '루아 BaseConvert 틀',
					['모듈:Citation'] = '루아 Citation 틀',
					['모듈:Citation/CS1'] = '루아 Citation/CS1 틀'
				category = modules[1] and categories[modules[1]]
				category = category or '루아 틀'
			end	
			cats[#cats + 1] = category
			--protCatName = "Templates using under-protected Lua modules"
		elseif titleObj.namespace == 828 then
			--protCatName = "Modules depending on under-protected modules"
		end
		if not args.noprotcat and protCatName then
			local protLevels = {
				autoconfirmed = 1,
				extendedconfirmed = 2,
				templateeditor = 3,
				sysop = 4
			local currentProt
			if titleObj.id ~= 0 then
				-- id is 0 (page does not exist) if am previewing before creating a template.
				currentProt = titleObj.protectionLevels["edit"][1]
			end
			if currentProt == nil then currentProt = 0 else currentProt = protLevels[currentProt] end
			for i, module in ipairs(modules) do
				if module ~= "WP:libraryUtil" then
					local moduleTitle = mw.title.new(module)
					local moduleProt = moduleTitle and moduleTitle.protectionLevels["edit"][1]
					if moduleProt == nil then moduleProt = 0 else moduleProt = protLevels[moduleProt] end
					if moduleProt < currentProt then
						cats[#cats + 1] = protCatName
						break
					end
				end
			end
		end
	end
	for i, cat in ipairs(cats) do
		cats[i] = string.format
	end
	return table.concat(cats)
end