local p = {}

function p.main(frame)
	local content = mw.title.getCurrentTitle():getContent()
	local quorum = 0
	local pages, users = {}, {}
	for page in string.gmatch(content, '%[%[(W%a+:Amministratori/Elezioni/.-)[|%]]') do
		table.insert(pages, page)
		table.insert(users, page:match('Elezioni/(.-)/'))
	end
	if #pages ~= 8 then
		return error('Non è stato trovato un elenco di 8 votazioni distinte nella pagina')
	end
	for _, page in ipairs(pages) do
		content = mw.title.new(page):getContent()
		if not content then
			return error('Non è stata trovata la pagina di una votazione')
		end
		content = content:gsub('\n(==+ *[^\n]- *==+)', '\r%1')
		for title, section in string.gmatch(content, '\n==+ *([^\n]-) *==+(.-)\r') do
			if title == 'Pro' or title == 'Contro' then
				local _, count = section:gsub('\n#[^#:*]')
				quorum = quorum + count
			end
		end
	end
	users = mw.text.listToText(users, ', ', ' e ')
	return string.format('Pertanto il quorum per la prossima votazione, calcolato in base al numero di favorevoli e contrari nelle votazioni relative a %s è pari a <b>%s</<b>.', users, quorum / 12)
end

return p