This module is used for Swazi headword-line templates.


local export = {}

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


-- 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"] = {"umu([^aeiou])", "um(.)"},
	["1a"] = {"", "u[^aeiou]"},
	["2"] = {"ba([^aeiou])", "be([^aeiou])"},
	["2a"] = {"bo([^aeiou])"},
	["3"] = {"umu([^aeiou])", "um(.)"},
	["4"] = {"imi([^aeiou])", "im([aeiou])"},
	["5"] = {"li([^aeiou])"},
	["6"] = {"ema([^aeiou])", "em([aeiou])"},
	["7"] = {"si([^aeiou])", "s([aeiou])"},
	["8"] = {"ti([^aeiou])", "t([aeiou])"},
	["9"] = {"in([^aeiou])", "im([^aeiou])", "i([mn][aeiou])", "i[^aeiou]"},
	["10"] = {"tin([^aeiou])", "tim([^aeiou])", "ti([^aeiou])"},
	["11"] = {"lu([^aeiou])", "lw([aeiou])"},
	["14"] = {"bu([^aeiou])", "tjw(a)", "tj(a)"},
	["15"] = {"ku([^aeiou])", "kw([aeiou])"},
	["17"] = {"ku([^aeiou])", "kw([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 = "nouns", 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 = "plural", 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 = "adjectives", 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 = "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, "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 = "ideophones", 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 = "verbs", 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