local export = {}

local function make_anchor(text, anchor)
	return '<span id="' .. (anchor or text) .. '">' .. text .. '</span>'
end

function export.show()
	local data		= mw.loadData "Module:links/data"
	local output	= {}
	
	local insert		= table.insert
	local ugsub			= mw.ustring.gsub
	local get_codepoint = mw.ustring.codepoint
	local ulen			= mw.ustring.len
	local sorted_pairs	= require "Module:table".sortedPairs
	local is_whitespace = require "Module:Unicode data".is_whitespace
	
	local function process_link_text (link_text)
		-- The basic space character is stripped from link text if there are no
		-- characters surrounding it.
		if ulen(link_text) == 1 and is_whitespace(get_codepoint(link_text)) then
			return ("]%s["):format(link_text)
		else
			-- Convert all characters to hexadecimal character entities to
			-- prevent any transformations.
			-- This is pretty much only because HTML comment syntax would not be
			-- displayed otherwise. But who knows what else MediaWiki software
			-- might do.
			return ugsub(
				link_text,
				".",
				function (char)
					local codepoint = get_codepoint(char)
					if not is_whitespace(codepoint) then
						return ("&#x%X;"):format(codepoint)
					end
				end)
		end
	end
	
	local function link(term, title)
		local script = require "Module:scripts".findBestScriptWithoutLang(term):getCode()
		if script ~= "None" then
			return ('* <span class="%s">[[ชื่อไม่รองรับ/%s|%s]]</span>'):format(
				script,
				title,
				process_link_text(term))
		else
			return ("* [[ชื่อไม่รองรับ/%s|%s]]"):format(
				title,
				process_link_text(term))
		end
	end
	
	for term, title in sorted_pairs(data.unsupported_titles) do
		insert(output, link(term, title))
	end
	insert(output, 1, "; " .. make_anchor("ชื่อไม่รองรับ") .. ":")
	
	return table.concat(output, "\n")
end

return export