local export = {}

local lang = require("Module:languages").getByCode("xh")


-- A list of valid noun classes
-- The value is the corresponding plural class.
-- True means the class is uncountable.
local singular_classes = {
	["1"] = "2",
	["1a"] = "2a",
	["3"] = "4",
	["5"] = "6",
	["7"] = "8",
	["9"] = "10",
	["11"] = "10",
	["14"] = true,
	["15"] = true,
	["17"] = true,
}

local plural_classes = {
	["2"] = true,
	["2a"] = true,
	["4"] = true,
	["6"] = true,
	["8"] = true,
	["10"] = true,
}


local prefixes = {
	["1"] = {"um(.)"},
	["1a"] = {"u([^aeiou])"},
	["2"] = {"aba([^aeiou])", "abe([^aeiou])"},
	["2a"] = {"oo([^aeiou])"},
	["3"] = {"um(.)"},
	["4"] = {"imi([^aeiou])", "im([aeiou])"},
	["5"] = {"ili([^aeiou])", "i([^aeiou])"},
	["6"] = {"ama([^aeiou])", "am([aeiou])"},
	["7"] = {"isi([^aeiou])", "is([aeiou])"},
	["8"] = {"izi([^aeiou])", "iz([aeiou])"},
	["9"] = {"in([^aeiou])", "im([^aeiou])", "i([mn][aeiou])", "i[^aeiou]"},
	["10"] = {"izin([^aeiou])", "izim([^aeiou])", "iim([^aeiou])", "iin([^aeiou])", "ii([^aeiou])"},
	["11"] = {"ulu([^aeiou])", "u([^aeiou])"},
	["14"] = {"ubu([^aeiou])", "utyw(a)", "uty(a)"},
	["15"] = {"uku([^aeiou])", "ukw([aeiou])"},
	["17"] = {"uku([^aeiou])", "ukw([aeiou])"},
}

local function remove_prefix(word, class)
	if not class or not prefixes[class] then
		return nil
	end
	
	-- If there is a capital letter in the word,
	-- assume that it's the first letter of the stem and remove everything before it
	if word:find("[A-Z]") then
		return word:gsub("^[a-z]+", "")
	else
		for key, prefix in ipairs(prefixes[class]) do
			word = word:gsub("^" .. prefix, "%1")
		end
		
		return word:gsub("^-", "")
	end
end


function export.noun(frame)
	local params = {
		[1] = {},
		[2] = {},
		[3] = {},
		[4] = {list = true},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {lang = lang, pos_category = "名詞", categories = {}, heads = {args[1]}, genders = {}, inflections = {}}
	
	if not args[1] then
		data.heads[1] = mw.title.getCurrentTitle().subpageText:gsub("^-", "") .. "<sup title=\"tones missing\">?</sup>"
		table.insert(data.categories, "Requests for tone in " .. lang:getCanonicalName() .. " noun entries")
	end
	
	-- Singular
	if not singular_classes[args[2]] and not plural_classes[args[2]] then
		args[2] = nil
	end
	
	table.insert(data.genders, "c" .. (args[2] or "?"))
	
	-- Plural
	if args[3] ~= "-" and (#args[4] > 0 or type(singular_classes[args[2]] ~= true) == "string") then
		if not plural_classes[args[3]] then
			args[3] = nil
		end
		
		local plural = {label = "複數", request = true}
		
		for _, form in ipairs(args[4]) do
			table.insert(plural, {term = form, genders = {"c" .. (args[3] or "?")}})
		end
		
		table.insert(data.inflections, plural)
	end
	
	data.sort_key = remove_prefix(mw.title.getCurrentTitle().subpageText, args[2])
	
	return require("Module:headword").full_headword(data)
end


function export.adjective(frame)
	local params = {
		[1] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {lang = lang, pos_category = "形容詞", categories = {}, heads = {args[1]}, inflections = {}}
	
	if not args[1] then
		data.heads[1] = mw.title.getCurrentTitle().subpageText:gsub("^-", "") .. "<sup title=\"tones missing\">?</sup>"
		table.insert(data.categories, "Requests for tone in " .. lang:getCanonicalName() .. " adjective entries")
	end
	
	data.heads[1] = "-" .. data.heads[1]
	
	return require("Module:headword").full_headword(data)
end


function export.relative(frame)
	local params = {
		[1] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {lang = lang, pos_category = "關係詞", categories = {}, heads = {args[1]}, inflections = {}}
	
	if not args[1] then
		data.heads[1] = mw.title.getCurrentTitle().subpageText:gsub("^-", "") .. "<sup title=\"tones missing\">?</sup>"
		table.insert(data.categories, "Requests for tone in " .. lang:getCanonicalName() .. " relative entries")
	end
	
	data.heads[1] = "-" .. data.heads[1]
	
	return require("Module:headword").full_headword(data)
end

function export.ideophone(frame)
	local params = {
		[1] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {lang = lang, pos_category = "擬態詞", categories = {}, heads = {args[1]}, inflections = {}}
	
	if not args[1] then
		data.heads[1] = mw.title.getCurrentTitle().subpageText:gsub("^-", "") .. "<sup title=\"tones missing\">?</sup>"
		table.insert(data.categories, "Requests for tone in " .. lang:getCanonicalName() .. " ideophone entries")
	end
	
	return require("Module:headword").full_headword(data)
end


function export.verb(frame)
	local params = {
		[1] = {},
		}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {lang = lang, pos_category = "動詞", categories = {}, heads = {args[1]}, inflections = {}}
	
	if not args[1] then
		data.heads[1] = mw.title.getCurrentTitle().subpageText:gsub("^-", "") .. "<sup title=\"tones missing\">?</sup>"
		table.insert(data.categories, "Requests for tone in " .. lang:getCanonicalName() .. " verb entries")
	end
	
	data.heads[1] = "-" .. data.heads[1]
	
	return require("Module:headword").full_headword(data)
end


return export