Documentation for this module may be created at વિભાગ:માથુંશબ્દ/doc

local export = {}

function export.full_headword(lang, sc, heads, tr, genders, inflections, categories, sort_key)
	local m_links = require("Module:કડીઓ")
	local m_utilities = require("Module:ઉપયોગીતા")
	
	local scFix = false
	
	-- Try to detect the script if it was not provided
	if not sc then
		sc, scFix = m_utilities.detect_script(heads[1] or SUBPAGENAME, lang)
	end
	
	-- Try to generate a transliteration if necessary
	-- Generate it if the script is not Gujr or similar, and if no transliteration was provided
	if tr == "-" then
		tr = nil
	elseif not tr and not ((sc:find("gu", nil, true))) then
		tr = lang:transliterate(heads[1] and m_links.remove_links(heads[1]) or SUBPAGENAME, sc)
	end
	
	-- Format and return all the gathered information
	return
		export.format_headword(heads, lang, sc) ..
		export.format_transliteration(tr, lang) ..
		export.format_genders(genders, lang) ..
		export.format_inflections(inflections, lang, sc) ..
		(scFix and "[[Category:Terms using script detection fallback]][[Category:Terms using script detection fallback/" .. lang:getCode() .. "]]" or "")
end

-- Format a headword
function export.format_headword(heads, lang, sc)
	local m_links = require("Module:કડીઓ")
	local m_scriptutils = require("Module:લિપિના ઉપયોગીતા")
	
	if type(heads) ~= "table" then
		heads = {heads}
	end
	
	if #heads == 0 then
	   	NAMESPACE = NAMESPACE or mw.title.getCurrentTitle().nsText
	   	SUBPAGENAME = SUBPAGENAME or mw.title.getCurrentTitle().subpageText
		
		if NAMESPACE == "પરિશિષ્ટ" and lang:getType() ~= "પરિશિષ્ટ-નિર્માણિત" then
			heads = {"*" .. SUBPAGENAME}
		else
			heads = {SUBPAGENAME}
		end
	end
	
	for i, head in ipairs(heads) do
		-- Apply processing to the headword, for formatting links and such
		if head:find("[[", nil, true) then
			head = m_links.language_link(head, nil, lang)
		end
		
		-- Add language and script wrapper
		head = m_scriptutils.tag_text(head, lang, sc, "માથું")
		heads[i] = head
	end
	
	return table.concat(heads, " ''કે'' ")
end

-- Format transliteration
function export.format_transliteration(tr, lang)
	if tr then
		local ret = " (<span lang=\"\">" .. tr .. "</span>)"
		
		if lang and mw.title.new(lang:getCanonicalName() .. " લિપ્યંતરણ", "વિક્શનરી").exists then
			ret = " [[વિક્શનરી:" .. lang:getCanonicalName() .. " લિપ્યંતરણ|•]]" .. ret
		end

		return ret
	else
		return ""
	end
end

function export.format_genders(genders, lang)
	if #genders > 0 then
		local gen = require("Module:લિંગ અને વચન")
		return "&nbsp;" .. gen.format_list(genders, lang:getCode())
	else
		return ""
	end
end

-- Format the inflections following the headword
function export.format_inflections(inflections, lang, sc)
	FULLPAGENAME = FULLPAGENAME or mw.title.getCurrentTitle().prefixedText
	
	if #inflections > 0 then
		-- Format each inflection individually
		for key, infl in ipairs(inflections) do
			-- If this inflection is a table, it contains parts
			-- consisting of alternating label-term pairs. Format them too.
			if type(infl) == "table" then
				inflections[key] = format_parts(infl, lang, sc)
			end
		end

		return " (" .. table.concat(inflections, ", ") .. ")"
	else
		return ""
	end
end

function format_parts(parts, lang, sc)
	local m_links = require("Module:કડીઓ")
	
	for key, part in ipairs(parts) do
		if type(part) ~= "table" then
			part = {term = part}
		end
		
		-- Convert the term into a full link
		-- Don't show a transliteration here, the consensus seems to be not to
		-- show them in headword lines to avoid clutter.
		part = m_links.full_link(not parts.nolink and part.term or nil, part.alt or (parts.nolink and part.term or nil), lang, part.sc, "ઘાટા", part.id, {genders = part.genders, tr = "-"}, FULLPAGENAME)
		
		if parts.accel then
			part = "<span class=\"ભાષાનું રૂપ-" .. lang:getCode() .. " " .. parts.accel .. "\">" .. part .. "</span>"
		end
		
		parts[key] = part
	end
	
	return "''" .. parts.label .. "''" .. (#parts > 0 and " " .. table.concat(parts, " ''કે'' ") or "")
end

return export