local export = {}
local u = require("Module:string/char")

local a, b = u(0xF000), u(0xF001)

local diacritics = {
	--[===[--Order given by Stephen G. Brown
	[u(0x0301)] = c, -- acute
	[u(0x0300)] = d, -- grave
	[u(0x0309)] = e, -- hook
	[u(0x0303)] = f, -- tilde
	[u(0x0323)] = g -- dot above]===]
	-- Order given by Fumiko Take
	[u(0x0300)] = "!", -- grave
	[u(0x0309)] = "#", -- hook
	[u(0x0303)] = "$", -- tilde
	[u(0x0301)] = "%", -- acute
	[u(0x0323)] = "&" -- dot above
}

local oneChar = { -- separate letters
	["ă"] = "a" .. a, ["â"] = "a" .. b, ["đ"] = "d" .. a, ["ê"] = "e" .. a, ["ô"] = "o" .. a, ["ơ"] = "o" .. b, ["ư"] = "u" .. a
}

function export.makeSortKey(text, lang, sc)
	text = mw.ustring.gsub(mw.ustring.lower(mw.ustring.toNFD(text)), ".", diacritics)
	
	text = mw.ustring.gsub(text, "([!-&])([^%s]+)", "%2%1")
	
	return mw.ustring.upper(mw.ustring.gsub(mw.ustring.toNFC(text), ".", oneChar))
end

local vi = require("Module:languages").getByCode("vi")
local function tag(text)
	return require("Module:script utilities").tag_text(text, vi)
end

local showsubst = {
	[a] = "₂", [b] = "₃", ["!"] = "²", ["#"] = "³", ["$"] = "⁴", ["%"] = "⁵", ["&"] = "⁶"
}

function export.showSortkey(frame)
	local output = {}
	
	for _, word in ipairs(frame.args) do
		local sc = vi:findBestScript(word):getCode()
		local sortkey = mw.ustring.gsub(export.makeSortKey(word, "vi", sc), ".", showsubst)
		sortkey = mw.ustring.gsub(sortkey, "([^0-9²³⁴⁵⁶])$", "%1¹")
		sortkey = mw.ustring.gsub(sortkey, "([^0-9²³⁴⁵⁶])(%s)", "%1¹%2")
		local example = "\n* <code>" .. sortkey .. "</code>\n: " .. tag(word)
		table.insert(output, example)
	end
	
	return table.concat(output)
end

function export.showSorting(frame)
	local terms = {}
	
	for _, term in ipairs(frame.args) do
		table.insert(terms, term)
	end
	
	local makeSortKey = require("Module:fun").memoize(export.makeSortKey)
	local function comp(term1, term2)
		return makeSortKey(term1) < makeSortKey(term2)
	end
	
	table.sort(terms, comp)
	
	for i, term in pairs(terms) do
		local sc = vi:findBestScript(term):getCode()
		local sortkey = mw.ustring.gsub(export.makeSortKey(term, "vi", sc), ".", showsubst)
		sortkey = mw.ustring.gsub(sortkey, "([^0-9²³⁴⁵⁶])$", "%1¹")
		sortkey = mw.ustring.gsub(sortkey, "([^0-9²³⁴⁵⁶])(%s)", "%1¹%2")
		terms[i] = "\n* " .. tag(term) .. " (<code>" .. sortkey .. "</code>)"
	end
	
	return table.concat(terms)
end

return export