This module is used by the templates {{Context}}

and {{Label}}
to show context labels for definitions.

local m_labels = require("Module:labels")

local export = {}

function export.show(frame)
	local args = frame:getParent().args
	local compat = (frame.args["compat"] or "") ~= ""
	local term_mode = (frame.args["term"] or "") ~= ""
	
	-- Gather parameters
	local lang = args[(compat and "lang" or 1)]; if lang == "" then lang = nil end
	local nocat = args["nocat"]; if nocat == "" then nocat = nil end
	local script = args["script"]; if script == ""  then script = nil end
	local script2 = args["script2"]; if script2 == ""  then script2 = nil end
	local sort_key = args["sort"]; if sort_key == ""  then sort_key = nil end
	local sort_key2 = args["sort2"]; if sort_key2 == ""  then sort_key2 = nil end
	
	if not lang then
		if mw.title.getCurrentTitle().nsText == "Teamplaid" then
			lang = "und"
		elseif compat then
			lang = "en"
		else
			error("Language code has not been specified. Please provide it to the template using the first parameter.")
		end
	end
	
	lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	
	-- Gather the labels
	local labels = {}
	
	local i = (compat and 1 or 2)
	local label = args[i]; if label == "" then label = nil end
	if not label then
		if mw.title.getCurrentTitle().nsText == "Teamplaid" then
			label = "eisimpleir"
		end
	end
	
	while label do
		table.insert(labels, label)
		i = i + 1
		label = args[i]; if label == "" then label = nil end
	end
	
	return m_labels.show_labels(labels, lang, script, script2, sort_key, sort_key2, nocat, term_mode)
end

-- temporary. intentionally undocumented.
-- this function is only to be used in {{alternative spelling of}}, {{eye dialect of}} and similar templates
function export.show_from(frame)
	local m_labeldata = require('Module:labels/data')
	
	local froms = {}
	local args = frame:getParent().args
	local nocat = args["nocat"]
	local lang = args["lang"] or "en"
	local limit = frame.args.limit and tonumber(frame.args.limit) or 99999
	
	lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")

	local key, i = 'from', 1
	while args[key] do
		local k = args[key]
		k = m_labeldata.aliases[k] or k
		local data = m_labeldata.labels[k]
		local label = data and data.display or k
		
		if not nocat and data then
			if data.regional_categories then
				for j, cat in ipairs(data.regional_categories) do
					label = label .. '[[Roinn-seòrsa:' .. cat .. ' ' .. lang:getCanonicalName() .. ']]'	
				end
			end
		
			if data.plain_categories then
				for j, cat in ipairs(data.plain_categories) do
					label = label .. '[[Roinn-seòrsa:' .. cat .. ']]'	
				end
			end
		end

		table.insert(froms, label)
		i = i + 1
		if i > limit then
			break	
		end
		key = 'from' .. i
	end
	
	if #froms == 0 then
		return frame.args.default	
	end
	
	if #froms == 2 then
		return froms[1] .. " and " .. froms[2]
	end
	
	local result = ""
	for i, item in ipairs(froms) do
		if i == 1 then
			-- nothing
		elseif i == #froms then
			result = result .. '<span class="serial-comma">,</span> <span class="serial-and"> and</span> '
		else
			result = result .. ', '
		end
		
		result = result .. item
	end
	return result
end

return export