local export = {}

-- from [[WT:POS]]
local POS_headers = require "Module:table".listToSet {
	"形容詞", "副詞", "Ambiposition", "冠詞", "環綴",
	"Circumposition", "Classifier", "Combining form", "連詞",
	"縮約形", ";量詞", "限定詞", "Diacritical mark",
	"漢字", "Ideophone", "Infix", "中綴", "感嘆詞",
	"字母", "合字", "名詞", "數字", "數詞", "分詞", "助詞",
	"短語", "後置詞", "前綴", "介詞", "介詞短語",
	"代詞", "專有名詞", "諺語", "標點符號", "羅馬化",
	"詞根", "後綴", "音節", "符號", "動詞", 
}

-- This isn't a perfect pattern, but should work in entries that don't have
-- bad syntax.
local title_pattern = "%f[^\n%z]==+%s*(.-)%s*==+"
local function count_POS_headers(title)
	local POS_count = 0
	
	for header in title:getContent():gmatch(title_pattern) do
		if POS_headers[header] then
			POS_count = POS_count + 1
		end
	end
	
	return POS_count
end

local function has_header(title, header_to_find)
	for header in title:getContent():gmatch(title_pattern) do
		if header == header_to_find then
			return true
		end
	end
	
	return false
end

-- Track Proto-Indo-European entries with more than one part-of-speech header.
-- Invoked by {{reconstruction}}, requested by Victar.
function export.main(frame)
	local title = mw.title.getCurrentTitle()
	local cats = {}
	
	local language = title.text:match "^[^/]+"
	local langcode = require("Module:languages").getByCanonicalName(language)
	if not langcode then
		-- Can happen e.g. if used on a user page
		return
	end
	if language == "原始印歐語" and count_POS_headers(title) > 1 then
		table.insert(cats, "有多種詞性的原始印歐語詞條")
	end
	
	local has_references_header = has_header(title, "References")
	local has_further_reading_header = has_header(title, "Further reading")
	if not has_references_header then
--		table.insert(cats, language .. " entries without References header")
	end
	
	if not (has_references_header or has_further_reading_header) then
--		table.insert(cats, language .. " entries without References or Further reading header")
	end
	
	return require("Module:utilities").format_categories(cats, langcode)
end

return export