---Lexicographic tools for Jarai language text.
local lang = require("Module:languages").getByCode("jra")

local p = {}

---Converts the given text to traditional tone marks.
function p.toTraditionalTones(text)
	if type(text) == "table" then
		text = text.args[1]
	end
	return (mw.ustring.gsub(text, "%a+", function (word)
		if mw.ustring.match(word, "^qu[yýỳỷỹỵ]$") then return word end
		return (mw.ustring.gsub(word, "%a%a$", {
			["oá"] = "óa", ["oà"] = "òa", ["oả"] = "ỏa", ["oã"] = "õa", ["oạ"] = "ọa",
			["oé"] = "óe", ["oè"] = "òe", ["oẻ"] = "ỏe", ["oẽ"] = "õe", ["oẹ"] = "ọe",
			["uý"] = "úy", ["uỳ"] = "ùy", ["uỷ"] = "ủy", ["uỹ"] = "ũy", ["uỵ"] = "ụy"
		}))
	end))
end

---Converts the given text to reformed tone marks.
function p.toReformedTones(text)
	if type(text) == "table" then
		text = text.args[1]
	end
	return (mw.ustring.gsub(text, "%a+", function (word)
		return (mw.ustring.gsub(word, "%a%a$", {
			["óa"] = "oá", ["òa"] = "oà", ["ỏa"] = "oả", ["õa"] = "oã", ["ọa"] = "oạ",
			["óe"] = "oé", ["òe"] = "oè", ["ỏe"] = "oẻ", ["õe"] = "oẽ", ["ọe"] = "oẹ",
			["úy"] = "uý", ["ùy"] = "uỳ", ["ủy"] = "uỷ", ["ũy"] = "uỹ", ["ụy"] = "uỵ"
		}))
	end))
end

function p.allSpellings(main_spelling, makeLinks)
	local frame = nil
	if type(main_spelling) == "table" then
		frame = main_spelling
		main_spelling, makeLinks = frame.args[1], frame.args.link
	end
	
	local xformers = {
		p.toTraditionalTones, p.toReformedTones,
	}
	
	local spellings = {}
	for i, xformer in ipairs(xformers) do
		local alt_spelling = xformer(main_spelling)
		if not spellings[alt_spelling] then
			table.insert(spellings, alt_spelling)
			spellings[alt_spelling] = true
		end
	end
	
	if makeLinks then
		local m_links = require("Module:links") -- [[Module:links]]
		for k, link in ipairs(spellings) do
			spellings[k] = m_links.full_link(link, nil, lang, nil, nil, nil, {}, false)
		end
	end
	return frame and table.concat(spellings, "/") or spellings
end

---Unicode codepoints for combining Jarai tone marks.
p.combiningToneMarks = mw.ustring.char(
	0x300,  -- à
	0x301,  -- á
	0x303,  -- ã
	0x309,  -- ả
	0x323   -- ạ
)

---Unicode codepoints for combining Jarai accent marks.
p.combiningAccentMarks = mw.ustring.char(
	0x302,  -- â
	0x306,  -- ă
	0x31b   -- ơ
)

---Strips Jarai diacritical marks from the given text.
-- @param tones     Set to “0” to leave tone marks intact.
-- @param accents   Set to “0” to leave accent marks intact.
-- @param đ         Set to “0” to leave “Đ” and “đ” intact.
function p.removeDiacritics(text, toneMarks, accentMarks, stroke)
	if type(text) == "table" then
		text, toneMarks, accentMarks, stroke = text.args[1],
			not text.args.tones or tonumber(text.args.tones) == 1,
			not text.args.accents or tonumber(text.args.accents) == 1,
			not text.args["đ"] or tonumber(text.args["đ"]) == 1
	end
	text = mw.ustring.toNFD(text)
	if toneMarks then
		text = mw.ustring.gsub(text, "[" .. p.combiningToneMarks .. "]", "")
	end
	if accentMarks then
		text = mw.ustring.gsub(text, "[" .. p.combiningAccentMarks .. "]", "")
	end
	if stroke then
		text = mw.ustring.gsub(text, "[Đđ]", {["Đ"] = "D", ["đ"] = "d"})
	end
	return mw.ustring.toNFC(text)
end

---Jarai letters for use in comp().
p.letters = "aAăĂâÂbBƀɃcCčČdDđĐeEĕĔêÊê̆Ê̆gGhHiIĭĬjJkKlLmMnNñÑŏŎôÔô̆Ô̆ơƠơ̆Ơ̆pPrRsStTuUŭŬưƯư̆Ư̆wWyY"

---Compare two syllables according to Jarai dictionary sorting order.
function p.compWord(word1, word2)
	if mw.ustring.find(word1, word2, 1, true) == 1 then return false end
	if mw.ustring.find(word2, word1, 1, true) == 1 then return true end
	do
		local func1, static1, var1 = mw.ustring.gmatch(word1, "[" .. p.letters .. "]")
		local func2, static2, var2 = mw.ustring.gmatch(word2, "[" .. p.letters .. "]")
		while true do
			local c1 = func1(static1, var1)
			local c2 = func2(static2, var2)
			if c1 == nil or c2 == nil then break end
			
			local idx1 = mw.ustring.find(p.letters, c1, 1, true)
			local idx2 = mw.ustring.find(p.letters, c2, 1, true)
			if idx1 and idx2 then
				if idx1 < idx2 then return true end
				if idx1 > idx2 then return false end
			end
		end
	end
	
	return word1 < word2
end

---Compare two strings according to Jarai dictionary sorting order.
function p.comp(text1, text2)
	if text1 == text2 then return false end
	
	do
		local func1, static1, var1 = mw.ustring.gmatch(text1, "%a+")
		local func2, static2, var2 = mw.ustring.gmatch(text2, "%a+")
		while true do
			local word1 = func1(static1, var1)
			local word2 = func2(static2, var2)
			if word1 == nil then return true end
			if word2 == nil then return false end
			
			if word1 ~= word2 then
				local lower1 = mw.ustring.lower(word1)
				local lower2 = mw.ustring.lower(word2)
				local noTones1 = p.removeDiacritics(lower1, true, false, false)
				local noTones2 = p.removeDiacritics(lower2, true, false, false)
				
				-- Compare base letters.
				if noTones1 ~= noTones2 then
					return p.compWord(noTones1, noTones2)
				end
				
				-- Compare letters case-insensitively.
				if lower1 ~= lower2 then
					return p.compWord(lower1, lower2)
				end
				
				-- Compare letters including tones.
				assert(word1 ~= word2)
				return p.compWord(word1, word2)
			end
		end
	end
	
	return text1 < text2
end

---Returns the categories indicated by the given wikitext.
function p.classifierCategories(frame)
	local src = frame.args[1]
	local classifiers = {}
	for classifier in mw.ustring.gmatch(mw.ustring.gsub(src, "<[^>]->", ""), "[" .. p.letters .. "]+") do
		if classifier ~= "l" and classifier ~= "jra" and classifier ~= "jra-l" and
				classifier ~= "Jarai" then
			local cat = mw.ustring.format("[[Thể loại:Danh từ tiếng Gia Rai có loại từ %s]]",
				classifier)
			table.insert(classifiers, cat)
		end
	end
	return table.concat(classifiers)
end

return p