local export = {}

function export.dercat(frame)
	local parameters = {
		[1] = { required = true },
		[2] = { list = true, required = true },
		["inh"] = { type = "number" },
		
	}
	local args = require("Module:parameters").process(frame:getParent().args, parameters)
	
	local target_lang = require("Module:languages").getByCode(args[1], 1)
	--local target_lang_name = target_lang:getCanonicalName()
	local target_lang_name = target_lang:getCategoryName()
	local categories = {}
	local function add_category(adjective, source_lang_name)
		if target_lang_name == source_lang_name and adjective == "รับมา" then
			table.insert(categories, "ศัพท์" .. target_lang_name .. "ที่ยืมสองครั้ง")
		else
			table.insert(categories, "ศัพท์" .. target_lang_name .. "ที่" .. adjective
				.. "จาก" .. source_lang_name)
		end
	end
	local has_cutoff = false
	for _, lang_code in ipairs(args[2]) do
		if lang_code == "<" then
			has_cutoff = true
			break
		end
	end
	for i, source_lang_code in ipairs(args[2]) do
		-- Accept family and etymology language codes.
		if source_lang_code == "<" then
			has_cutoff = false
		else
			local source_lang = require("Module:languages").getByCode(source_lang_code, i, true, true)
			--local source_lang_name = source_lang:getCanonicalName()
			local source_lang_name = source_lang:getCategoryName()
			--if source_lang:getType() == "family" then
			--	source_lang_name = source_lang_name .. " languages"
			--end
			add_category("รับมา", source_lang_name)
			if has_cutoff or (args.inh and i <= args.inh) then
				add_category("สืบทอด", source_lang_name)
			end
		end
	end
	return require("Module:utilities").format_categories(categories, target_lang)
end

return export