Modul:etymology languages/templates

local export = {}

function export.exists(frame)
	local args = frame.args
	local etymlang = args[1] or error("Etymology language code has not been specified. Please pass parameter 1 to the module invocation.")
	
	etymlang = require("Module:etymology languages").getByCode(etymlang)
	
	if etymlang then
		return "1"
	else
		return ""
	end
end

function export.getByCode(frame)
	local args = frame.args
	local etymlang = args[1] or error("Etymology language code (parameter 1) has not been specified.")
	etymlang = require("Module:etymology languages").getByCode(etymlang) or error("The etymology language code '" .. etymlang .. "' is not valid.")

	return require("Module:language-like").templateGetByCode(etymlang, args)
end

function export.getByCanonicalName(frame)
	local args = frame.args
	local etymlang = args[1] or error("Etymology language name (parameter 1) has not been specified.")
	
	etymlang = require("Module:etymology languages").getByCanonicalName(etymlang)
	
	if etymlang then
		return etymlang:getCode()
	else
		return ""
	end
end

local function get_child_codes(parent)
	local children = require "Module:User:Erutuon/lang stuff/map":new(require "Module:etymology languages/data")
		:filter(
			function(data)
				return data.parent == parent
			end)
	
	local Array = require "Module:array"
	local data_to_codes = setmetatable({}, {
		__index = function (self, key)
			local val = Array()
			self[key] = val
			return val
		end,
	})
	for code, data in pairs(children) do
		data_to_codes[data]:insert(code)
	end
	
	return data_to_codes
end

function export.list_child_codes(frame)
	local lang = frame.args[1] or error("Language code (parameter 1) is required.")
	local children = get_child_codes(lang)
	
	local Array = require "Module:array"
	return require "Module:User:Erutuon/lang stuff/map":new(children)
		:map(
			function (codes, data)
				return ("* %s: %s")
					:format(
						require "Module:etymology languages".makeObject(codes[1], data):makeWikipediaLink(),
						Array(codes)
							:sort(
								function (a, b)
									return mw.ustring.lower(a) < mw.ustring.lower(b)
								end)
							:map(
								function (code)
									return "<code>" .. code .. "</code>"
								end)
							:concat ", ")
			end)
		:values()
		:sort()
		:concat "\n"
end

return export