Документация
local wikidata = require "Module:Wikidata"
local general = require "Модуль:Карточка/Fonctions"
local sports = require "Модуль:Карточка/Fonctions/Compétition sportive"
local linktopic = '-'

local function getFormerNames( item )
	local claims =  wikidata.getClaims{
		property = vals,
		entity = item,
		property = 'P1448',
		rank = 'valid',
		sorttype = 'chronological'
	}
	if not claims then
		return nil
	end
	local names, periods = {}, {}
	for i, val in pairs(claims) do
		table.insert(names, wikidata.formatStatement(val))
		table.insert(periods, wikidata.getFormattedDate(val, {linktopic = '-', precision = 'day'}))
	end
	return names, periods
end

local function formerNamesTable( localdata ) -- retourne une table de lignes contenant les anciens noms de l'équipe
	local names, periods = localdata['ancien_nom'], localdata['saison']
	if names then
		names =  mw.text.split(names, '<br />')
		periods = mw.text.split(periods or '', '<br />')
	else
		names, periods = getFormerNames( localdata.item )
	end
	if not names then
		return nil
	end
	local rows = {}
	for i, period in pairs(periods) do
		table.insert( rows, {type = 'row', label = period, value = function () return names[i] end})	
	end
	local title = 'Dénominations'
	if #names == 1 then
		title = 'Dénomination'
	end
	return {type = 'table', title = title, rows = rows}
end

return {
	maincolor = '#FFDF80',
	parts = {
		general.title('entete cyclisme'),
		general.mainimage('Article à illustrer Équipe cycliste'),
		{type = 'table', title = 'Informations', rows = {
			sports.typeofteam,
			{
				type = 'row',
				label = function ( localdata )
					local claims = wikidata.getClaims{property = 'P1998', entity = localdata.item}
					if (not claims) or #claims == 1 then
						return "Code UCI"
					end
					return "Codes UCI"
				end,
				value = 'code',
				wikidata = function ( item )
					local claims = wikidata.getClaims{
						entity = item,
						property = 'P1998',
						conjtype = '<br />',
						sorttype = 'chronological',
						rank = 'valid'
					}
					if not claims then
						return nil
					end
					local conjtype,textformat = '<br />', 'long'
					if #claims > 4 then
						conjtype, textformat = 'comma', 'long'
					end
					return wikidata.formatAndCat{
						entity = item,
						property = 'P1998',
						showdate = true,
						textformat = textformat,
						linktopic = '-',
						rank = 'valid'
					}
				end
			},
			{type = 'row', label = 'Discipline', value = 'discipline', property = 'P641'},
			function ( localdata ) -- affichage du pays, avec les dates seulement s'il y en a plus qu'1
				local c = wikidata.getClaims{entity = localdata.item, property = 'P17', rank = 'valid'}
				if not c then
					return nil
				elseif c == 1 then
					return general.country{rank = 'valid', sorttype = 'chronological'}
				else
					return general.country{rank = 'valid', sorttype = 'chronological', showdate = true}
				end
			end,
			{type = 'row', label = 'Création', value = 'creation', property = 'P571'},
			{type = 'row', label = 'Disparition', value = 'disparition', property = 'P576'},
			{
				type = 'row',
				label = 'Saisons',
				value = 'saisons',
				wikidata = function ( item ) -- suppose que P527 ne contienne que des saisons, et contienne toutes les saisons
					local seasons = wikidata.getClaims{entity = item, property = 'P527'}
					if not seasons then
						return nil
					end
					local str = tostring(#seasons)
					str = wikidata.addLinkback(str, item, 'P527')
					str = str .. wikidata.addtrackingcat('P527')
					return str
				end,
			},
			{type = 'row', label = 'Budget', value = 'budget', wikidata = {
				property = 'P2130',
				separator = '<br />',
				rank = 'valid',
				showdate = true
			}},
			{type = 'row', label = 'Sponsor', singularlabel = 'Sponsor', plurallabel = 'Sponsors', property = 'P859'},
			{type = 'row', label = 'Marque de cycles', value = 'velo'},
		}},
		{type = 'table', title = 'Encadrement', rows = {
			{type = 'row', label = 'Directeur général', value = 'manager_gén', wikidata = {
				property = 'P505',
				conjtype = '<br />',
				showdate = true,
				linktopic = linktopic
			}},
			{type = 'row', label = 'Directeur technique', value = 'dir_tech'},
			{
				type = 'row',
				label = 'Directeur(s) sportif(s)',
				singularlabel = 'Directeur sportif',
				plurallabel = 'Directeurs sportifs',
				value = function ( localdata )
					local vals = {}
					for i = 1, 10 do
						local dir = localdata['ds' .. tostring(i)]
						if dir then
							table.insert(vals, dir)
						end
					end
					if #vals > 0 then
						return table.concat(vals, '<br />')
					end
				end,
				wikidata = {
					property = 'P286',
					conjtype = '<br />',
					showdate = true,
					linktopic = linktopic
				}
			},
			{type = 'row', label = 'Soigneur', value = 'sg1'},
			{type = 'row', label = 'Médecin', value = 'medecin'},
		}},
		formerNamesTable,
	}
}