Documentation for this module may be created at વિભાગ:સંયોજન/doc

local m_links = require("Module:કડીઓ")
local m_utilities = require("Module:ઉપયોગીતા")

local export = {}

-- TODO: If we can assume that all terms will have the same script,
-- then we can do script detection before calling full_link,
-- rather than letting it try to detect the script for each term individually.

function export.template_compound(frame)
	local args = frame:getParent().args
	NAMESPACE = mw.title.getCurrentTitle().nsText
	local categories = {}
	
	local lang = args["ભાષા"]; if lang == "" then lang = nil end
	local sc = args["લિપિ"] or ""; if sc == "" then sc = nil end
	local sort_key = args["ક્રમ"]; if sort_key == "" then sort_key = nil end
	local nocat = args["નાશ્રેણી"]; if not nocat or nocat == "" then nocat = false else nocat = true end
	
	if not lang then
		lang = "gu"
		table.insert(categories, "Language code missing/compound")
	end
	
	lang = require("Module:ભાષાઓ").getLanguageByCode(lang) or error("ભાષા સંકેત \"" .. lang .. "\" યોગ્ય નથી।")
	
	local parts = get_parts(args)
	
	-- Just to make sure someone didn't use the template in a silly way
	if #parts < 2 then
		if NAMESPACE == "ઢાંચો" then
			parts = { {term = "પહેલું", annotations = {}}, {term = "બીજું", annotations = {}} }
		else
			error("ઓછામાં ઓછું સંયોજનના બે ભાગ આપવા જોઈએ।")
		end
	end
	
	-- Add categories, if any
	if not nocat then
		table.insert(categories, lang:getCanonicalName() .. "નો સંયોજન શબ્દો")
	end
	
	-- Make links out of all the parts
	for i, part in ipairs(parts) do
		parts[i] = m_links.full_link(part.term, part.alt, lang, sc, "શબ્દ", part.id, part.annotations)
	end
	
	categories = m_utilities.format_categories(categories, lang, sort_key)
	
	return table.concat(parts, " +&lrm; ") .. categories
end

function export.template_prefix(frame)
	local args = frame:getParent().args
	NAMESPACE = mw.title.getCurrentTitle().nsText
	local categories = {}
	
	local lang = args["ભાષા"]; if lang == "" then lang = nil end
	local sc = args["લિપિ"] or ""; if sc == "" then sc = nil end
	local sort_key = args["ક્રમ"]; if sort_key == "" then sort_key = nil end
	local nocat = args["નાશ્રેણી"]; if not nocat or nocat == "" then nocat = false else nocat = true end
	local pos = args["pos"]; if pos == "" then pos = nil end
	
	if not lang then
		lang = "gu"
		table.insert(categories, "Language code missing/prefix")
	end
	
	lang = require("Module:ભાષાઓ").getLanguageByCode(lang) or error("ભાષા સંકેત \"" .. lang .. "\" યોગ્ય નથી।")
	
	local affixes = get_parts(args)
	local base = nil
	
	if #affixes > 1 then
		base = affixes[#affixes]
		affixes[#affixes] = nil
	end
	
	-- Hyphenate the affixes
	for i, aff in ipairs(affixes) do
		if aff.term then
			affixes[i].term = export.make_affix(aff.term, lang, sc, false)
		end
		if aff.alt then
			affixes[i].alt = export.make_affix(aff.alt, lang, sc, false)
		end
		if aff.annotations.tr then
			affixes[i].annotations.tr = export.make_affix(aff.annotations.tr, lang, "gu", false)
		end
	end
	
	-- Just to make sure someone didn't use the template in a silly way
	if #affixes < 1 then
		if NAMESPACE == "ઢાંચો" then
			base = {term = "આધાર", annotations = {}}
			affixes = { {term = "ઉપસર્ગ", annotations = {}} }
		else
			error("ઓછામાં ઓછું એક ઉપસર્ગ આપવું જોઈએ।")
		end
	end
	
	-- Add categories, if any
	if not nocat then
		-- Pluralize the part of speech name, if given
		if pos then
			pos = frame:expandTemplate{title = "બહુરૂપી", args = {pos}}
		else
			pos = "શબ્દો"
		end
		
		-- Insert the categories
		for i, aff in ipairs(affixes) do
			table.insert(categories, lang:getCanonicalName() .. " " .. pos .. " " .. lang:makeEntryName(aff.term, lang) .. " ઉપસર્ગ જોડે")
		end
	end
	
	-- Make links out of all the parts
	local sort_base = nil
	
	if base then
		sort_base = lang:makeEntryName(base.term)
		base = m_links.full_link(base.term, base.alt, lang, sc, "શબ્દ", base.id, base.annotations)
	end
	
	for i, aff in ipairs(affixes) do
		affixes[i] = m_links.full_link(aff.term, aff.alt, lang, sc, "શબ્દ", aff.id, aff.annotations)
	end
	
	-- The fourth parameter sets the base term (without the prefixes)
	-- as the basic sort key to which further processing is applied.
	categories = m_utilities.format_categories(categories, lang, sort_key, sort_base)
	
	return table.concat(affixes, " +&lrm; ") .. " +&lrm; " .. (base or "") .. categories
end

function export.template_suffix(frame)
	local args = frame:getParent().args
	NAMESPACE = mw.title.getCurrentTitle().nsText
	local categories = {}
	
	local lang = args["ભાષા"]; if lang == "" then lang = nil end
	local sc = args["લિપિ"] or ""; if sc == "" then sc = nil end
	local sort_key = args["ક્રમ"]; if sort_key == "" then sort_key = nil end
	local nocat = args["નાશ્રેણી"]; if not nocat or nocat == "" then nocat = false else nocat = true end
	local pos = args["pos"]; if pos == "" then pos = nil end
	
	if not lang then
		lang = "gu"
		table.insert(categories, "Language code missing/suffix")
	end
	
	lang = require("Module:ભાષાઓ").getLanguageByCode(lang) or error("ભાષા સંકેત \"" .. lang .. "\" યોગ્ય નથી।")
	
	local base = get_part(args, 1)
	local affixes = get_parts(args, 2)
	
	-- Hyphenate the affixes
	for i, aff in ipairs(affixes) do
		if aff.term then
			affixes[i].term = export.make_affix(aff.term, lang, sc, true)
		end
		if aff.alt then
			affixes[i].alt = export.make_affix(aff.alt, lang, sc, true)
		end
		if aff.annotations.tr then
			affixes[i].annotations.tr = export.make_affix(aff.annotations.tr, lang, "gu", true)
		end
	end
	
	-- Just to make sure someone didn't use the template in a silly way
	if #affixes < 1 then
		if NAMESPACE == "ઢાંચો" then
			base = {term = "આધાર", annotations = {}}
			affixes = { {term = "પ્રત્યય", annotations = {}} }
		else
			error("ઓછામાં ઓછું એક પ્રત્યય આપવું જોઈએ।")
		end
	end
	
	-- Add categories, if any
	if not nocat then
		-- Pluralize the part of speech name, if given
		if pos then
			pos = frame:expandTemplate{title = "બહુરૂપી", args = {pos}}
		else
			pos = "શબ્દો"
		end
		
		-- Insert the categories
		for i, aff in ipairs(affixes) do
			table.insert(categories, lang:getCanonicalName() .. " " .. pos .. " " .. lang:makeEntryName(aff.term) .. " પ્રત્યય જોડે")
		end
	end
	
	-- Make links out of all the parts
	if base then
		base = m_links.full_link(base.term, base.alt, lang, sc, "શબ્દ", base.id, base.annotations)
	end
	
	for i, aff in ipairs(affixes) do
		affixes[i] = m_links.full_link(aff.term, aff.alt, lang, sc, "શબ્દ", aff.id, aff.annotations)
	end
	
	categories = m_utilities.format_categories(categories, lang, sort_key)
	
	return (base or "") .. " +&lrm; " .. table.concat(affixes, " +&lrm; ") .. categories
end

function get_part(args, i)
	local term = args[i]; if term == "" then term = nil end
	local alt = args["બીજું" .. i]; if alt == "" then alt = nil end
	local id = args["ઓળખ" .. i]; if id == "" then id = nil end
	
	local tr = args["લિપ્ય" .. i]; if tr == "" then tr = nil end
	local gloss = args["અનુ" .. i] or args["પરિભાષા" .. i]; if gloss == "" then gloss = nil end
	local pos = args["pos" .. i]; if pos == "" then pos = nil end
	local lit = args["અક્ષરશ:" .. i]; if lit == "" then lit = nil end
	
	if not term and not alt and not tr then
		return nil
	-- Embedded wikilinks should not be allowed in parts of a compound;
		-- this would just confuse things a lot.
	elseif term and term:find("[[", nil, true) then
		error("Embedded wikilinks should not be used in the parts of a compound.")
	else
		return {term = term, alt = alt, id = id, annotations = {tr = tr, gloss = gloss, pos = pos, lit = lit}}
	end
end

function get_parts(args, start)
	local parts = {}
	
	local i = start or 1
	
	while true do
		local part = get_part(args, i)
		
		if not part then
			break
		end
		
		table.insert(parts, part)
		
		i = i + 1
	end
	
	return parts
end

-- Adds a hyphen to a word in the appropriate place
-- This is exported for the benefit of {{suffixcat}}
function export.make_affix(word, lang, sc, isSuffix)
	if type(word) == "કોષ્ટક" then
		local args = word.args
		
		word = args[1] or error("પારમિતિ ૧ (શબ્દ) આપેલું નથી।")
		lang = args["ભાષા"] or error("પારમિતિ ભાષા= આપેલું નથી।")
		lang = require("Module:ભાષાઓ").getLanguageByCode(lang)
		sc = args["લિપિ"]; if sc == "" then sc = nil end
		isSuffix = args["પ્રત્યય"]; if not isSuffix or isSuffix == "" then isSuffix = false else isSuffix = true end
	end
	
	local hyphen = "-"
	if sc ~= "ગુજ" then
		-- FIXME: should be script-based
		if lang:getCode() == "he" or lang:getCode() == "yi" then
			hyphen = "־"
		elseif lang:getCode() == "ja" or lang:getCode() == "ko" then
			hyphen = ""
		elseif lang:getCode() == "fa" then
			hyphen = "ـ"
		end
	end
	
	if isSuffix then
		if word:find("^*") then
			return (word:gsub("^*", "*" .. hyphen))
		else
			return hyphen .. word
		end
	else
		return word .. hyphen
	end
end

return export