Documentation for this module may be created at Module:families/doc

local export = {}

local Family = {}


function Family:getCode()
	return self._code
end


function Family:getCanonicalName()
	return self._rawData.canonicalName
end


--function Family:getAllNames()
--	return self._rawData.names
--end


function Family:getOtherNames()
	return self._rawData.otherNames or {}
end


function Family:getType()
	return "family"
end


function Family:getFamily()
	if self._rawData.family and not self._familyObject then
		self._familyObject = export.getByCode(self._rawData.family)
	end
	
	return self._familyObject
end


function Family:getProtoLanguage()
	if not self._protoLanguage then
		self._protoLanguage = require("Module:languages").getByCode(self._rawData.protoLanguage or self._code .. "-pro")
	end
	
	return self._protoLanguage
end


function Family:getCategoryName()
	local name = self._rawData.canonicalName
	
	-- If the name already has "languages" in it, don't add it.
	if name:find("[Ll]anguages$") then
		return name
	else
		return name .. " languages"
	end
end

function Family:getWikidataItem()
	return self._rawData.wikidata_item
end

function Family:getWikipediaArticle()
	return (self:getWikidataItem() and mw.wikibase and mw.wikibase.sitelink(self:getWikidataItem(), 'enwiki')) or
		self:getCategoryName()
end
	
function Family:makeWikipediaLink()
	return "[[w:" .. self:getWikipediaArticle() .. "|" .. self:getCanonicalName() .. "]]"
end


function Family:toJSON()
	local ret = {
		canonicalName = self:getCanonicalName(),
		categoryName = self:getCategoryName(),
		code = self._code,
		family = self._rawData.family,
		otherNames = self:getOtherNames(),
		type = self:getType(),
		wikidataItem = self:getWikidataItem(),
		}
	
	return require("Module:JSON").toJSON(ret)
end


function Family:getRawData()
	return self._rawData
end


Family.__index = Family


function export.makeObject(code, data)
	return data and setmetatable({ _rawData = data, _code = code }, Family) or nil
end


function export.getByCode(code)
	if code == 'kdo' then
		require('Module:debug').track('Kordofanian')
	end
	
	return export.makeObject(code, mw.loadData("Module:families/data")[code])
end

function export.getByCanonicalName(name)
	local code = mw.loadData("Module:families/by name")[name]
	
	if not code then
		return nil
	end
	
	return export.makeObject(code, mw.loadData("Module:families/data")[code])
end

return export