Documentation for this module may be created at Modul:dara kategoriyan/jikatbike/belge

local export = {}
local b = require("Module:bingeh")
-- Category object

local Category = {}
Category.__index = Category


function Category.new_main(frame)
	local params = {
		[1] = {},
		[2] = {required = true},
	}
	
	args = require("Module:parameters").process(frame:getParent().args, params)
	
	return Category.new({code = args[1], label = args[2]})
end


function Category.new(info)
	for key, val in pairs(info) do
		if not (key == "code" or key == "label") then
			error("The parameter \"" .. key .. "\" was not recognized.")
		end
	end
	
	local self = setmetatable({}, Category)
	self._info = info
	
	if not self._info.label then
		error("No label was specified.")
	end
	
	self._lang = self._info.code and (require("Module:ziman").getByCode(self._info.code) or error("The language code \"" .. self._info.code .. "\" is not valid.")) or nil
	self._source = 
		require("Module:families").getByCode(self._info.label) or
		require("Module:etymology languages").getByCode(self._info.label) or
		require("Module:languages").getByCode(self._info.label) or 
		error("\"" .. self._info.label .. "\" is not a valid language, family or etymology-only language code.")
	
	return self
end

export.new = Category.new
export.new_main = Category.new_main


function Category:getInfo()
	return self._info
end


function Category:getBreadcrumbName()
	local ret = nil
	
	if self._source:getType() == "family" then
		ret = self._source:getCategoryName()
	else
		ret = self._source:getCanonicalName()
	end
	
	return (not self._lang and "peyvên ji " or "") .. ret
end


function Category:getDataModule()
	return "Modul:dara kategoriyan/jikatbike"
end


function Category:canBeEmpty()
	return false
end


function Category:isHidden()
	return false
end


function Category:getCategoryName()
	local ret = nil

	if self._source:getType() == "family" then
		ret = self._source:getCategoryName()
	else
		if self._source:getCode() == "mul-tax" then
			ret = "taxonomic names"
		else
			ret = self._source:getCanonicalName()
		end
	end
	
	if self._lang then
		return "Peyvên " .. self._lang:getCanonicalName() .. " ji " .. ret
	else
		return "Peyvên ji " .. ret
	end
end


function Category:getDescription()
	local ret = nil
	
	if not self._lang then
		if self._source:getType() == "family" then
			return "Zimanên ku ji zimanên têvel yên " .. self._source:getCategoryName() .. " peyv wergirtine."
		else
			return "Zimanên ku ji zimanê " .. self._source:getCanonicalName() .. " peyv wergirtine."
		end
	else
		if self._source:getType() == "family" then
			return
				"Peyvên " .. self._lang:getCanonicalName() .. " yên ku ji " ..
				"[[:Kategorî:" .. mw.getContentLanguage():ucfirst(self._source:getCategoryName()) .. "|" .. self._source:getCategoryName() .. "]] hatine wergirtin.\n\n" ..
				"Divê di vê kategoriyê de tenê binkategorî hebin. Dema jêdera peyvekê baş nayê zanîn, dibe ku li vir jî gotar hebin.\n\n" ..
				"Heke zimanê jêder yê vê peyvê tê zanîn ji kerema xwe vê gotarê ji vê rûpelê rakin."
		elseif self._source:getType() == "etymology language" then
			return "Peyvên " .. self._lang:getCanonicalName() .. " yên ji [[:Kategorî:" .. mw.getContentLanguage():ucfirst(self._source:getCategoryName()) .. "|" .. b.lcfirst(self._source:getCategoryName()) .. "]] hatine wergirtin."
		else
			return "Peyvên " .. self._lang:getCanonicalName() .. " yên ji [[:Kategorî:" .. mw.getContentLanguage():ucfirst(self._source:getCategoryName()) .. "|" .. self._source:getCategoryName() .. "]] hatine wergirtin."
		end
	end
end


function Category:getParents()
	local derived_from_subvariety_of_self = false
	if self._lang then
		local parent = {}
		
		local pinfo = {
			code = self._lang:getCode()
		}
		local sortkey = nil
		
		if self._source:getType() == "family" then
			local fam = self._source:getFamily()
			
			if not fam or fam:getCode() == "qfa-iso" or fam:getCode() == "qfa-not" then
				pinfo.label = nil
			else
				pinfo.label = fam:getCode()
			end
			
			sortkey = self._source:getCanonicalName()
		elseif self._source:getType() == "etymology language" then
			if self._source:getParentCode() == "qfa-iso" or self._source:getParentCode() == "qfa-not" or self._source:getParentCode() == "qfa-und" then
				pinfo.label = nil
			elseif self._source:getParentCode() == self._lang:getCode() then
				-- error("A language can't derive terms from a subvariety of itself.")
				derived_from_subvariety_of_self = true
				pinfo.label = self._lang:getFamily():getCode()
			else
				pinfo.label = self._source:getParentCode()
			end
			
			sortkey = self._source:getCanonicalName()
		else
			local fam = self._source:getFamily()
			
			if fam and not (fam:getCode() == "qfa-iso" or fam:getCode() == "qfa-not") then
				pinfo.label = fam:getCode()
			end -- else label is nil
			
			sortkey = self._source:getCanonicalName()
		end
		
		local additional_category = derived_from_subvariety_of_self and {
				name = "Category:Categories for terms in a language derived from a term in a subvariety of that language",
				sort = self._lang:getCanonicalName()
			} or nil
		
		if not pinfo.label then
			return {
				{
					name = require("Modul:dara kategoriyan/gelemperkatbike ")
						.new {
							code = self._info.code,
							label = "ji zimanên din"
						},
					sort = sortkey
				},
				additional_category,
			}
		else
			return {{name = Category.new(pinfo), sort = sortkey}, additional_category}
		end
	else
		if self._source:getType() == "family" then
			return {{name = require("Modul:dara kategoriyan/malbatkatbike").new({code = self._info.label}), sort = " "}}
		elseif self._source:getType() == "etymology language" then
			return {{name = "Kategorî:" .. mw.getContentLanguage():ucfirst(self._source:getCategoryName()), sort = "peyvên ji"}}
		else
			return {{name = require("Modul:dara kategoriyan/zimankatbike").new({code = self._info.label}), sort = "peyvên ji"}}
		end
	end
end

function Category:getChildren()
	return nil
end


function Category:getUmbrella()
	if not self._lang then
		return nil
	end
	
	return Category.new({label = self._source:getCode()})
end


return export