local export = {}

function export.show(frame)
	local output = {}
	-- Hacky way to allow function to be called from other modules.
	local args = (frame.getParent and frame:getParent().args) or frame
	local demo = false

	local FULLPAGENAME = mw.title.getCurrentTitle().fullText
	local BASEPAGENAME, SUBPAGENAME = FULLPAGENAME:match("^(.-)/([^/]-)$")
	-- BASEPAGENAME = BASEPAGENAME or FULLPAGENAME
	
	if frame.getParent and FULLPAGENAME == frame:getParent():getTitle() then
		demo = true
	end
	output[#output + 1] = '<div class="noprint plainlinks shortcut-box">'

	if args.temp then
		output[#output + 1] = "[[Wiktionary:Viết tắt|Viết tắt tạm thời" .. (args[2] and '' or '') .. "]]:<br/>"
	else
		output[#output + 1] = "[[Wiktionary:Viết tắt|Viết tắt" .. (args[2] and '' or '') .. "]]:<br/>"
	end
	
	local attn = false

	for i, shortcut in ipairs(args) do
		local title = mw.title.new(shortcut)
		
		if not title then
			output[#output + 1] = '<span class="attentionseeking">(<code>' .. mw.text.nowiki(shortcut) .. '</code>)</span><br/>'
			attn = true
		else
			local iattn = false

			local item

			if not title.exists then
				attn = true
				local query = 'action=edit&redlink=1&preloadtext=' .. mw.uri.encode('#REDIRECT [[' .. FULLPAGENAME .. ']]')
				if title.nsText == "Template" then
					item = '<code>{{[' .. tostring(mw.uri.fullUrl(shortcut, query)) .. ' <span style="color:#c20;">' .. title.text .. '</span>]}}</code>'
				else
					item = '[' .. tostring(mw.uri.fullUrl(shortcut, query)) .. ' <span style="color:#c20;">' .. shortcut .. '</span>]'
				end
			else
				if title.nsText == "Template" then
					item = '<code>{{[' .. tostring(mw.uri.fullUrl(shortcut, 'redirect=no')) .. ' ' .. title.text .. ']}}</code>'
				else
					item = '[' .. tostring(mw.uri.fullUrl(shortcut, 'redirect=no')) .. ' ' .. shortcut .. ']'
				end
				
				if not demo then
					local redirectTarget = title.redirectTarget
					if redirectTarget then
						redirectTarget.fragment = '' -- remove fragment, if present
						local targetText = redirectTarget.fullText
						if title.nsText == "Template" then
							if SUBPAGENAME == 'documentation' then
								iattn = (targetText ~= BASEPAGENAME)
							else
								iattn = (targetText ~= FULLPAGENAME)
							end
						else
							if SUBPAGENAME == 'documentation' then
								iattn = not (targetText == BASEPAGENAME
									or targetText == FULLPAGENAME)
							else
								iattn = not (targetText == FULLPAGENAME
									or targetText == FULLPAGENAME .. '/documentation')
							end
						end
					end
					
					if not (redirectTarget and redirectTarget.exists) then
						iattn = true
					end
				end
			end

			if iattn then
				item = '<span class="attentionseeking">' .. item .. '</span>'
			end

			output[#output + 1] = item .. '<br/>'
			
			attn = attn or iattn
		end
	end

	if not args.nocat then
		if attn then
			output[#output + 1] = '[[Category:Shortcut boxes needing attention]]'	
		end
		if SUBPAGENAME ~= 'documentation' then
			output[#output + 1] = '[[Category:Wiktionary pages with shortcuts]]'
		end
	end

	output[#output + 1] = '</div>'
	
	return table.concat(output)
end

return export