local export = {}

local function family_is_not_a_family(fam)
	local famcode = fam:getCode()
	if famcode == "paa" then
		return false -- Papuan languages are not a family but have a category
	elseif famcode == "qfa-iso" or famcode == "qfa-not" then
		return true
	else
		local parfam = fam:getFamily()
		if parfam and parfam:getCode() == "qfa-not" then
			-- Constructed languages, sign languages, etc.; no category for them
			return true
		end
	end
	return false
end

function export.show(text)

	if type(text) == "table" then -- called directly from a template
		text = text.args[1]
	end

	local family_name = mw.ustring.match(text, "^กลุ่มภาษา(.+)$")
	local family = require("Module:families").getByCanonicalName(family_name) or
		require("Module:families").getByCanonicalName(mw.ustring.lower(family_name))

	local parent_fam = family:getFamily()
	local first_parent
	
	if not parent_fam or family_is_not_a_family(parent_fam) then
		first_parent = "ภาษาแบ่งตามกลุ่มภาษา"
	else
		first_parent = parent_fam:getCategoryName()
	end

	return first_parent

end

return export