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

local export = {}
local sc = require("Module:scripts").getByCode("Arab")
local lang = require("Module:languages").getByCode("acw")
local m_utilities = require("Module:utilities")
local m_links = require("Module:links")
local m_headword = require("Module:headword")
local rsplit = mw.text.split

function export.catfix() return m_utilities.catfix(lang, sc) end

-- If Not Empty
local function ine(x)
    if x == "" then
        return nil
    else
        return x
    end
end

local function link(term, tr, gloss, face, alt)
    if word == "" or word == "—" then
        return word
    else
        return m_links.full_link({
            term = term,
            alt = alt,
            lang = lang,
            tr = tr,
            sc = sc,
            gloss = gloss
        }, face)
    end
end

local function validateRoot(rootTable, joined_root)
    if type(rootTable) ~= "table" then error("rootTable is not a table", 2) end

    for i, letter in ipairs(rootTable) do
        if mw.ustring.len(letter) > 1 then
            error("'" .. letter .. "', the " .. ordinal[i] ..
                      " letter in the root '" .. joined_root ..
                      "' should be a single letter.")
        end
    end
end

function export.acw_root(frame)
    local output = {}
    local categories = {}
    local title = mw.title.getCurrentTitle()
    local fulltitle = title.fullText
    local pagename = title.text
    local namespace = title.nsText

    local params = {
        [1] = {},
        [2] = {},
        [3] = {},
        [4] = {},
        ["nocat"] = {type = "boolean", default = false},
        ["plain"] = {type = "boolean", default = false},
        ["t"] = {},
        ["gloss"] = {alias_of = "t"},
        ["notext"] = {type = "boolean", default = false},
        ["nolink"] = {type = "boolean", default = false}
    }

    local args = require("Module:parameters").process(frame:getParent().args,
                                                      params)
    local rootLetters = {}

    if not args[1] and namespace == "Template" then
        rootLetters = {"ك", "ت", "ب"}
    elseif args[1] then
        rootLetters = rsplit(args[1], " ")
    else
        rootLetters = rsplit(fulltitle, " ")
    end

    local joined_root = table.concat(rootLetters, " ")
    validateRoot(rootLetters, joined_root)

    if fulltitle == joined_root then
        table.insert(output, m_headword.full_headword(
                         {
                lang = lang,
                sc = sc,
                pos_category = "roots",
                categories = {},
                heads = {joined_root}
            }))

 

        if args[1] then
            require("Module:debug").track("acw-root") -- [[Special:WhatLinksHere/Template:tracking/acw-root]]
        end
    else
        if sc:countCharacters(pagename) < mw.ustring.len(pagename) - 2 then
            require("Module:debug").track("acw-root/title-not-acw") -- [[Special:WhatLinksHere/Template:tracking/acw-root/title-not-acw]]

            if not args["nocat"] then
                require("Module:debug").track("acw-root/title-not-acw/cat") -- [[Special:WhatLinksHere/Template:tracking/acw-root/title-not-acw/cat]]
            end
        end

        local link_text

        if args["nolink"] then
            link_text = link(nil, ine(args["gloss"]), args["face"], joined_root)
        else
            link_text = link(joined_root, ine(args["gloss"]), args["face"])
        end

        table.insert(output, link_text)

    
    end

    if args["plain"] then
        return joined_root
    elseif args["nocat"] then
        return table.concat(output)
    elseif args["notext"] then
        return table.concat(categories)
    else
        return table.concat(output) .. table.concat(categories)
    end
end

return export