local export = {}

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


-- 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",
	["3a"] = "4a",
	["5"] = "6",
	["7"] = "8",
	["9"] = "10",
	["11"] = "10",
	["14"] = true,
	["15"] = true,
	["17"] = true,
}

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


local prefixes = {
	["1"] = {"umu([^aeiou])", "um(.)", "u(n)"},
	["1a"] = {"u([^aeiou])"},
	["2"] = {"aba([^aeiou])", "abe([^aeiou])"},
	["2a"] = {"o([^aeiou])"},
	["3"] = {"umu([^aeiou])", "um(.)", "u(n)"},
	["3a"] = {"u([^aeiou])"},
	["4"] = {"imi([^aeiou])", "im([aeiou])"},
	["4a"] = {"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])"},
	["10"] = {"izin([^aeiou])", "izim([^aeiou])", "izi([^aeiou])", "iz([aeiou])"},
	["11"] = {"ulu([^aeiou])", "u([^aeiou])"},
	["14"] = {"ubu([^aeiou])", "utshw(a)", "utsh(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, lang:getCanonicalName() .. " nouns needing tone")
	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, lang:getCanonicalName() .. " adjectives needing tone")
	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 = "relatives", 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, lang:getCanonicalName() .. " relatives needing tone")
	end
	
	data.heads[1] = "-" .. data.heads[1]
	
	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, lang:getCanonicalName() .. " verbs needing tone")
	end
	
	data.heads[1] = "-" .. data.heads[1]
	
	return require("Module:headword").full_headword(data)
end


return export