Module:Fro-headword

local m_fro_utilities = require("Module:fro-utilities")

local export = {}

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

function export.noun(frame)
    local args = frame:getParent().args
    PAGENAME = mw.title.getCurrentTitle().text
    
    local head = args["head"]; if head == "" then head = nil end
    
    local op = args["op"] or args[2]; if op == "" then op = nil end
    local ns = args["ns"] or args[3]; if ns == "" then ns = nil end
    local np = args["np"] or args[4]; if np == "" then np = nil end
    
    local data = {lang = lang, pos_category = "nouns", categories = {}, heads = {head}, genders = {}, inflections = {}}
	
    -- Process the genders
    local valid_genders = {
        ["m"] = true,
        ["f"] = true,
        ["m-p"] = true,
        ["f-p"] = true}
    
    local g = args[1]
    
    if valid_genders[g] then
        data.genders = {g}
        if g == "m" or g == "m-p" then
            table.insert(data.categories, "Auld French masculine nouns")
        else
            table.insert(data.categories, "Auld French feminine nouns")
        end
    elseif g == "mf" then
        data.genders = {"m", "f"}
        table.insert(data.categories, "Auld French masculine nouns")
        table.insert(data.categories, "Auld French feminine nouns")
    else
        data.genders = {"?"}
        table.insert(data.categories, "Auld French terms wi incomplete gender")
    end
    
    -- Generate default plural forms by adding -s.
    -- Note that these are also used for the nominative singular of masculine nouns.
    local plurals = m_fro_utilities.pluralize(PAGENAME)
    
    -- Insert PART, either a string or array, into TAB. If PART is a string,
    -- split on commas.
    local function insert_part(tab, part)
        if type(part) == "table" then
            for _, form in ipairs(part) do
                table.insert(tab, form)
            end
        else
            local forms = mw.text.split(part, ",")
            for _, form in ipairs(forms) do
                table.insert(tab, form)
            end
        end
    end

    -- Oblique plurals are the same for both genders
    local op_parts = {label = "oblique plural"}

    insert_part(op_parts, op or plurals)    

    -- Nominative forms differ between the genders
    -- If masculine, the singular gets the -s
    -- If feminine, the plural gets the -s
    local ns_parts = {label = "nominative singular"}
    local np_parts = {label = "nominative plural"}
    
    if data.genders[1] == "m" or data.genders[1] == "m-p" then
        insert_part(np_parts, np or PAGENAME)
        insert_part(ns_parts, ns or plurals)
    else
        insert_part(ns_parts, ns or PAGENAME)
        insert_part(np_parts, np or plurals)
    end
    
    -- Add the inflections
    if op == "-" or data.genders[1] == "m-p" or data.genders[1] == "f-p" then
        table.insert(data.inflections, ns_parts)
        table.insert(data.categories, "Old French uncountable nouns")
    else
        table.insert(data.inflections, op_parts)
        table.insert(data.inflections, ns_parts)
        table.insert(data.inflections, np_parts)
    end
    
    return require("Module:headword").full_headword(data)
end
 
return export