Documentation for this module may be created at မဝ်ဂျူ:cy-mut/doc

local export = {}

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

local rfind = mw.ustring.find
local usub = mw.ustring.sub
local ulower = mw.ustring.lower

local PAGENAME = mw.title.getCurrentTitle().text
local IRREGMARKER = "<sup>△</sup>"

local mutation_rules = {
	['b'] = {"f", "m",},
	['c'] = {"g", "ngh", "ch",},
	['ch'] = {},
	['d'] = {"dd", "n",},
	['f'] = {},
	['g'] = {"", "ng"},
	['h'] = {},
	['j'] = {},
	['l'] = {},
	['ll'] = {"l",},
	['m'] = {"f",},
	['n'] = {},
	['p'] = {"b", "mh", "ph",},
	['r'] = {},
	['rh'] = {"r",},
	['s'] = {},
	['t'] = {"d", "nh", "th",},
}

function export.get_mutation_data(term)
	local data = {}
	data.radical = term
	data.initial = usub(term, 1, 1)
	data.is_uppercase = ulower(data.initial) ~= data.initial
	data.initial = ulower(data.initial)
	data.final = usub(term, 2, -1)
	
	data.first_two = data.initial .. usub(data.final, 1, 1)
	if data.first_two == 'll' or data.first_two == 'rh' or data.first_two == 'ch' then
		data.initial = data.first_two
		data.final = usub(data.final, 2, -1)
	end
	data.vowel = false
	
	data.mut1 = nil
	data.mut2 = nil
	data.mut3 = nil
	
	if rfind(data.initial, "[aâàeêèiîìoôòuûùwŵẁyŷỳ]") then
		data.vowel = true
		data.mut3 = "h" .. data.initial
	elseif mutation_rules[data.initial] then
		data.mut1 = mutation_rules[data.initial][1]
		data.mut2 = mutation_rules[data.initial][2]
		data.mut3 = mutation_rules[data.initial][3]
	else
		error("no mutation rule found for this term: " .. term)
	end

	return data
end

function export.show(frame)
	local params = {
		[1] = {},
		["soft"] = {},
		["nasal"] = {},
		["aspirate"] = {},
		["nocat"] = {type = "boolean"},
	}
	local parargs = frame:getParent().args
    local args = require("Module:parameters").process(parargs, params)
	
	local data = export.get_mutation_data(args[1] or PAGENAME)

	local function construct_mutation(accel_form, initial, override)
		local normal_mutation
		if not initial then
			normal_mutation = nil
		else
			normal_mutation = initial .. data.final
			if data.is_uppercase then
				normal_mutation = require("Module:string utilities").ucfirst(normal_mutation)
			end
		end
		local has_override = override
		if override then
			if override == "no" or override == "0" then
				override = nil
			end
		end
		local has_irreg_mutation = has_override and override ~= normal_mutation
		local mutation
		-- don't combine the following into A and B or C because `override` may be nil
		if has_override then
			mutation = override
		else
			mutation = normal_mutation
		end
		local irreg_marker = has_irreg_mutation and IRREGMARKER or ""
		if mutation then
			return require("Module:links").full_link({lang = lang, accel = {form = accel_form, lemma = data.radical}, term = mutation}) .. irreg_marker, true, has_irreg_mutation
		else
			return "''unchanged''" .. irreg_marker, false, has_irreg_mutation
		end
	end

	local soft, has_soft, has_irreg_soft = construct_mutation("soft", data.mut1, args.soft)
	local nasal, has_nasal, has_irreg_nasal  = construct_mutation("nasal", data.mut2, args.nasal)
	local aspirate, has_aspirate, has_irreg_aspirate = construct_mutation(data.vowel and "h-prothesis" or "aspirate", data.mut3, args.aspirate)

	result = '{| border="1" cellpadding="4" cellspacing="0" class="inflection-table" style="align: left; margin: 0.5em 0 0 0; border-style: solid; border: 1px solid #7f7f7f; border-right-width: 2px; border-bottom-width: 2px; border-collapse: collapse; background-color: #F8F8F8; font-size: 95%;"'
	result = result .. '\n|-'
	result = result .. '\n! colspan=4 | [[Appendix:Welsh mutations|Welsh mutation]]'
	result = result .. '\n|-'
	result = result .. '\n! [[radical]] !! [[soft mutation|soft]] !! [[nasal mutation|nasal]] !! ' .. (data.vowel and '[[h-prothesis]]' or '[[aspirate mutation|aspirate]]')
	result = result .. '\n|-'
	result = result .. '\n| ' .. require("Module:links").full_link({lang = lang, term = data.radical})
	result = result .. '\n| ' .. soft
	result = result .. '\n| ' .. nasal
	result = result .. '\n| ' .. aspirate
	if has_irreg_soft or has_irreg_nasal or has_irreg_aspirate then
		result = result .. '\n|-'
		result = result .. "\n| colspan=4 | <small style=\"font-size:85%;\">" .. IRREGMARKER .. "Irregular.</small>"
	end
	if has_soft or has_nasal or has_aspirate then
		result = result .. '\n|-'
		result = result .. "\n| colspan=4 | <small style=\"font-size:85%;\">''Note:'' Some of these forms may be hypothetical. Not every<br />possible mutated form of every word actually occurs.</small>"
	end
	result = result .. '\n|}'
	if not args.nocat and (has_irreg_soft or has_irreg_nasal or has_irreg_aspirate) then
		result = result .. require("Module:utilities").format_categories({"Welsh terms with irregular mutation"}, lang)
	end
	return result
end

return export