local p = {}

p['_teamBracket'] = function (frame, template, subs, args)
	-- figure out parameters from template name
	args.rounds = args.rounds or mw.ustring.match(template or '', "^(%d*)Round")
	if not args.rounds then
		local teams = tonumber(mw.ustring.match(template or '', "^(%d*)Team"))
		if teams and teams > 1 then -- log2(teams) is the number of rounds
			args.rounds = math.ceil( math.log(teams)/math.log(2) )
		end
	end
	local tennis = mw.ustring.match(template or '', "-Tennis(%d*)")
	local squash = mw.ustring.match(template or '', "-Squash(%d*)") 
	if tennis or squash then
		args.sets = args.sets or tennis or squash
		args.seeds = args.seeds or 'yes'
		args.nowrap = args.nowrap or 'yes'
	end
	if string.find(template, "-Compact") then
		args.compact = args.compact or 'yes'
	end
	-- overwrite defaults with frame args and alternative args
	for k, v in pairs(frame.args) do
		if subs[k] then
			args[subs[k]] = v
		else
			args[k] = v
		end
	end
	--return template
	return require('Module:Team bracket').main(frame:newChild{title = ("Template:" .. template), args = args})
end

p['2TeamBracket'] = function (frame)
	return p._teamBracket(frame, '2TeamBracket', {}, {autoseeds = "y"})
end

p['4TeamBracket'] = function (frame)
	return p._teamBracket(frame, '4TeamBracket', {
		group1 = "RD1-group1"
	}, {
		autoseeds = "y",
		['RD1-group1'] = ""
	})
end

p['4TeamBracket-Tennis3'] = function (frame)
	return p._teamBracket(frame, '4TeamBracket-Tennis3', {}, {
		seeds = '',
		RD2 = "Final",
		['team-width'] = 125
	})
end

p['4TeamBracket-Tennis5'] = function (frame)
	return p._teamBracket(frame, '4TeamBracket-Tennis5', {}, {
		seeds = '',
		RD1 = "Quarterfinals",
		RD2 = "Semifinals",
		['team-width'] = 125,
		['RD1-seed1'] = '',
		['RD1-seed2'] = '',
		['RD1-seed3'] = '',
		['RD1-seed4'] = '',
	})
end

p['4TeamBracket-Tennis35'] = function (frame)
	return p._teamBracket(frame, '4TeamBracket-Tennis35', {}, {
		sets = "3/5",
		RD1 = "Semifinals",
		RD2 = "Final",
		['RD1-seed1'] = '',
		['RD1-seed2'] = '',
		['RD1-seed3'] = '',
		['RD1-seed4'] = '',
	})
end

p['6TeamBracket'] = function (frame)
	stepdown = require('Module:yesno')(frame.args.stepdown)
	return p._teamBracket(frame, '6TeamBracket', {
		group1 = "RD1-group1",
		group2 = "RD1-group2",
		group3 = "RD2-group1",
	}, {
		compact = "y",
		seeds = "y",
		byes = '',
		['RD1-omit'] = stepdown and "3/4/7/8" or "1/2/5/6",
		['RD1-group1'] = "",
		['RD1-group2'] = "",
		['RD2-group1'] = "",
		['RD1-seed1'] = 4,
		['RD1-seed2'] = 5,
		['RD1-seed3'] = 3,
		['RD1-seed4'] = 6,
		['RD2-seed1'] = stepdown and '' or 1,
		['RD2-seed2'] = stepdown and 1 or '',
		['RD2-seed3'] = stepdown and '' or 2,
		['RD2-seed4'] = stepdown and 2 or '',
	})
end

p['7TeamBracket'] = function (frame)
	return p._teamBracket(frame, '7TeamBracket', {
		group1 = "RD1-group1",
		group2 = "RD1-group2",
		group3 = "RD2-group1",
		['RD1-seed01'] = 'RD1-seed1',
		['RD1-seed02'] = 'RD1-seed2',
		['RD1-seed03'] = 'RD1-seed3',
		['RD1-seed04'] = 'RD1-seed4',
		['RD1-seed05'] = 'RD1-seed5',
		['RD1-seed06'] = 'RD1-seed6',
		['RD2-seed01'] = 'RD2-seed1',
		['RD1-team01'] = 'RD1-team1',
		['RD1-team02'] = 'RD1-team2',
		['RD1-team03'] = 'RD1-team3',
		['RD1-team04'] = 'RD1-team4',
		['RD1-team05'] = 'RD1-team5',
		['RD1-team06'] = 'RD1-team6',
	}, {
		['RD1-omit'] = "1/2",
		byes = 1,
		['RD1-group1'] = "",
		['RD1-group2'] = "",
		['RD2-group1'] = "",
		['RD1-seed1'] = 4,
		['RD1-seed2'] = 5,
		['RD1-seed3'] = 2,
		['RD1-seed4'] = 7,
		['RD1-seed5'] = 3,
		['RD1-seed6'] = 6,
		['RD2-seed1'] = 1,
		['RD1-team1'] = ' ',
		['RD1-team2'] = ' ',
		['RD1-team3'] = ' ',
		['RD1-team4'] = ' ',
		['RD1-team5'] = ' ',
		['RD1-team6'] = ' ',

	})
end

p['8TeamBracket'] = function (frame)
	return p._teamBracket(frame, '8TeamBracket', {
		group1 = 'RD1-group1',
		group2 = 'RD1-group2',
		group3 = 'RD2-group1',
		['RD1-seed01'] = 'RD1-seed1',
		['RD1-seed02'] = 'RD1-seed2',
		['RD1-seed03'] = 'RD1-seed3',
		['RD1-seed04'] = 'RD1-seed4',
		['RD1-seed05'] = 'RD1-seed5',
		['RD1-seed06'] = 'RD1-seed6',
		['RD1-seed07'] = 'RD1-seed7',
		['RD1-seed08'] = 'RD1-seed8',
	}, {
		['RD1-seed1'] = 1,
		['RD1-seed2'] = 8,
		['RD1-seed3'] = 4,
		['RD1-seed4'] = 5,
		['RD1-seed5'] = 2,
		['RD1-seed6'] = 7,
		['RD1-seed7'] = 3,
		['RD1-seed8'] = 6,
	})
end

p['8TeamBracket-Byes'] = function (frame)
	return p._teamBracket(frame, '8TeamBracket-Byes', {
		group1 = "RD1-group1",
		group2 = "RD1-group2",
		group3 = "RD2-group1",
		Consol = "RD3b"
	}, {
		seeds = 'yes',
		byes = 1,
		['team-width'] = '11em',
		['score-width'] = '2em',
		['RD1-group1'] = '',
		['RD1-group2'] = '',
		['RD2-group1'] = '',
		RD3b = frame.args['RD3-team3'] and "Third place" or ""
	})
end
p['3RoundBracket-Byes'] = p['8TeamBracket-Byes']


p['8TeamBracket-Compact-NoSeeds-Byes'] = function (frame)
	return p._teamBracket(frame, '8TeamBracket-Compact-NoSeeds-Byes', {
		['RD1-seed01'] = 'RD1-seed1',
		['RD1-seed02'] = 'RD1-seed2',
		['RD1-seed03'] = 'RD1-seed3',
		['RD1-seed04'] = 'RD1-seed4',
		['RD1-seed05'] = 'RD1-seed5',
		['RD1-seed06'] = 'RD1-seed6',
		['RD1-seed07'] = 'RD1-seed7',
		['RD1-seed08'] = 'RD1-seed8',
	}, {
		byes = 2,
		['team-width'] = '125',
		['score-width'] = '25',
		RD1 = 'Quarterfinals',
		RD2 = 'Semifinals',
		RD3 = 'Final',
	})
end

p['8TeamBracket-Tennis3'] = function (frame)
	return p._teamBracket(frame, '8TeamBracket-Tennis3', {}, {
		RD1 = 'Quarterfinals',
		RD2 = 'Semifinals',
		RD3 = 'Finals',
		['team-width'] = 140,
		['RD1-seed1'] = 1,
		['RD1-seed2'] = 8,
		['RD1-seed3'] = 4,
		['RD1-seed4'] = 5,
		['RD1-seed5'] = 3,
		['RD1-seed6'] = 6,
		['RD1-seed7'] = 2,
		['RD1-seed8'] = 7,
	})
end

p['8TeamBracket-Tennis5'] = function (frame)
	return p._teamBracket(frame, '8TeamBracket-Tennis5', {}, {
		RD1 = 'Quarterfinals',
		RD2 = 'Semifinals',
		RD3 = 'Final',
	})
end

p['16TeamBracket'] = function (frame)
	return p._teamBracket(frame, '16TeamBracket', {
		subgroup1 = 'RD1-group1',
		subgroup2 = 'RD1-group2',
		subgroup3 = 'RD1-group3',
		subgroup4 = 'RD1-group4',
		group1 = 'RD2-group1',
		group2 = 'RD2-group2',
		group3 = 'RD3-group1',
		['RD1-seed01'] = 'RD1-seed1',
		['RD1-seed02'] = 'RD1-seed2',
		['RD1-seed03'] = 'RD1-seed3',
		['RD1-seed04'] = 'RD1-seed4',
		['RD1-seed05'] = 'RD1-seed5',
		['RD1-seed06'] = 'RD1-seed6',
		['RD1-seed07'] = 'RD1-seed7',
		['RD1-seed08'] = 'RD1-seed8',
		['RD1-seed09'] = 'RD1-seed9',
	}, {
		['RD1-seed1'] = 1,
		['RD1-seed2'] = 16,
		['RD1-seed3'] = 8,
		['RD1-seed4'] = 9,
		['RD1-seed5'] = 5,
		['RD1-seed6'] = 12,
		['RD1-seed7'] = 4,
		['RD1-seed8'] = 13,
		['RD1-seed9'] = 6,
		['RD1-seed10'] = 11,
		['RD1-seed11'] = 3,
		['RD1-seed12'] = 14,
		['RD1-seed13'] = 7,
		['RD1-seed14'] = 10,
		['RD1-seed15'] = 2,
		['RD1-seed16'] = 15,
		RD1 = 'First round',
		RD2 = 'Quarter-finals',
		RD3 = 'Semi-finals',
		RD4 = 'Final',
	})
end

p['16TeamBracket-Byes'] = function (frame)
	return p._teamBracket(frame, '16TeamBracket-Byes', {
		group1 = "RD1-group1",
		group2 = "RD2-group2",
	}, {
		seeds = 'yes',
		byes = 1,
		RD1 = 'First round',
		RD2 = 'Second round',
		RD3 = 'Semifinals',
		RD4 = 'Finals',
		['team-width'] = '11em',
		['score-width'] = '2em',
		['RD1-group1'] = '',
		['RD2-group2'] = '',
	})
end
p['4RoundBracket-Byes'] = p['16TeamBracket-Byes']

p['16TeamBracket-Tennis3'] = function (frame)
	return p._teamBracket(frame, '16TeamBracket-Tennis3', {}, {
		['team-width'] = 140,
		['score-width'] = 25,
		RD1 = 'First round',
		RD2 = 'Quarterfinals',
		RD3 = 'Semifinals',
		RD4 = 'Final',
	})
end

p['16TeamBracket-Compact-NoSeeds-Byes'] = function (frame)
	return p._teamBracket(frame, '16TeamBracket-Compact-NoSeeds-Byes', {
		['RD1-seed01'] = 'RD1-seed1',
		['RD1-seed02'] = 'RD1-seed2',
		['RD1-seed03'] = 'RD1-seed3',
		['RD1-seed04'] = 'RD1-seed4',
		['RD1-seed05'] = 'RD1-seed5',
		['RD1-seed06'] = 'RD1-seed6',
		['RD1-seed07'] = 'RD1-seed7',
		['RD1-seed08'] = 'RD1-seed8',
		['RD1-seed09'] = 'RD1-seed9',
	}, {
		byes = 3,
		RD1 = 'Round of 16',
		RD2 = 'Quarterfinals',
		RD3 = 'Semifinals',
		RD4 = 'Final',
		['team-width'] = 125,
		['score-width'] = 25
	})
end

p['16TeamBracket-Compact-Tennis3'] = function (frame)
	return p._teamBracket(frame, '16TeamBracket-Compact-Tennis3', {}, {
		RD1 = '1st round',
		RD2 = '2nd round',
	})
end

p['16TeamBracket-Compact-Tennis3-Byes'] = function (frame)
	return p._teamBracket(frame, '16TeamBracket-Compact-Tennis3-Byes', {}, {
		byes = 2,
		['seed-width'] = 25,
		['team-width'] = 150,
		RD1 = '1st round',
		RD2 = '2nd round',
		RD3 = 'Semifinals',
		RD4 = 'Final',
	})
end
p['4RoundBracket-Compact-Tennis3-Byes'] = p['16TeamBracket-Compact-Tennis3-Byes']

p['16TeamBracket-Compact-Archery'] = function (frame)
	return p._teamBracket(frame, '16TeamBracket-Compact-Archery', {}, {
		nowrap = 'yes',
		sets = 6,
		seeds = 'yes',
		['team-width'] = 150,
		['score-width'] = 12,
		RD1 = '1st round',
		RD2 = '2nd round',
		RD3 = 'Semifinals',
		RD4 = 'Finals',
	})
end

p['16TeamBracket-Tennis5'] = function (frame)
	return p._teamBracket(frame, '16TeamBracket-Tennis5', {}, {
		RD1 = 'First round',
		RD2 = 'Quarterfinals',
		RD3 = 'Semifinals',
		RD4 = 'Final',
		['seed-width'] = 25,
		['team-width'] = 150,
		['score-width'] = 12,
	})
end

p['16TeamBracket-Compact-Tennis5'] = function (frame)
	return p._teamBracket(frame, '16TeamBracket-Compact-Tennis5', {}, {
		RD1 = 'First round',
		RD2 = 'Second round',
		RD3 = 'Third round',
		RD4 = 'Fourth round',
	})
end

p['16TeamBracket-Compact-Tennis5-Byes'] = function (frame)
	return p._teamBracket(frame, '16TeamBracket-Compact-Tennis5-Byes', {}, {
		byes = 1,
		RD1 = 'First round',
		RD2 = 'Second round',
		RD3 = 'Semifinals',
		RD4 = 'Final',
	})
end
p['4RoundBracket-Compact-Tennis5-Byes'] = p['16TeamBracket-Compact-Tennis5-Byes']

p['32TeamBracket'] = function (frame)
	return p._teamBracket(frame, '32TeamBracket', {
		subgroup1 = 'RD1-group1',
		subgroup2 = 'RD1-group2',
		subgroup3 = 'RD1-group3',
		subgroup4 = 'RD1-group4',
		subgroup5 = 'RD1-group5',
		subgroup6 = 'RD1-group6',
		subgroup7 = 'RD1-group7',
		subgroup8 = 'RD1-group8',
		group1 = 'RD2-group1',
		group2 = 'RD2-group2',
		group3 = 'RD3-group3',
		group4 = 'RD2-group4',
		half1 = 'RD3-group1',
		half2 = 'RD3-group2',
		group5 = 'RD4-group1',
	}, {
		autoseeds = 'y',
		RD1 = "1st Round",
		RD2 = "2nd Round",
	})
end

p['32TeamBracket-Byes'] = function (frame)
	return p._teamBracket(frame, '32TeamBracket-Byes', {}, {
		byes = 1,
		seeds = 'y',
		RD1 = "First Round",
		RD2 = "Second Round",
		RD3 = "Quarter Finals",
		RD4 = "Semi Finals",
		RD5 = "Final",
		['seed-width'] = '2em',
		['team-width'] = '11em',
		['score-width'] = '2em',
	})
end

p['32TeamBracket-Compact-NoSeeds-Byes'] = function (frame)
	return p._teamBracket(frame, '32TeamBracket-Compact-NoSeeds-Byes', {}, {
		byes = 4,
		RD1 = "Round of 32",
		RD2 = "Round of 16",
		RD3 = "Quarterfinals",
		RD4 = "Semifinals",
		RD5 = "Final",
		['team-width'] = 125,
		['score-width'] = 25,
	})
end

p['32TeamBracket-Compact-Squash5'] = function (frame)
	return p._teamBracket(frame, '32TeamBracket-Compact-Squash5', {}, {
		['compact-final'] = 'yes',
		['team-width'] = 190,
		['score-width'] = 12,
		['seed-width'] = 10,
		RD1 = "1st round",
		RD2 = "2nd round",
		RD3 = "3rd round",
		RD4 = "4th round",
		RD5 = "5th round",
	})
end

p['32TeamBracket-Compact-Tennis3'] = function (frame)
	return p._teamBracket(frame, '32TeamBracket-Compact-Tennis3', {}, {
		seeds = '',
		['compact-final'] = 'yes',
		['team-width'] = 'auto',
		['score-width'] = 12,
		RD1 = "1st round",
		RD2 = "2nd round",
		RD3 = "Quarterfinals",
		RD4 = "Semifinals",
		RD5 = "Finals",
	})
end

p['32TeamBracket-Tennis3'] = function (frame)
	return p._teamBracket(frame, '32TeamBracket-Tennis3', {}, {
		compact = 'yes',
		RD1 = "First Round",
		RD2 = "Second Round",
		RD3 = "Quarterfinals",
		RD4 = "Semifinals",
		RD5 = "Final",
	})
end

p['64TeamBracket'] = function (frame)
	return p._teamBracket(frame, '64TeamBracket', {
		subgroup1 = 'RD1-group1',
		subgroup2 = 'RD1-group2',
		subgroup3 = 'RD1-group3',
		subgroup4 = 'RD1-group4',
		subgroup5 = 'RD1-group5',
		subgroup6 = 'RD1-group6',
		subgroup7 = 'RD1-group7',
		subgroup8 = 'RD1-group8',
		subgroup9 = 'RD1-group9',
		subgroup10 = 'RD1-group10',
		subgroup11 = 'RD1-group11',
		subgroup12 = 'RD1-group12',
		subgroup13 = 'RD1-group13',
		subgroup14 = 'RD1-group14',
		subgroup15 = 'RD1-group15',
		subgroup16 = 'RD1-group16',
		group1 = 'RD2-group1',
		group2 = 'RD2-group2',
		group3 = 'RD3-group3',
		group4 = 'RD2-group4',
		group5 = 'RD2-group5',
		group6 = 'RD3-group6',
		group7 = 'RD2-group7',
		group8 = 'RD2-group8',
		quarter1 = 'RD3-group1',
		quarter2 = 'RD3-group2',
		quarter3 = 'RD3-group3',
		quarter4 = 'RD3-group4',
		half1 = 'RD4-group1',
		half2 = 'RD4-group2',
		group9 = 'RD5-group1',
		['RD1-group01'] = 'RD1-group1',
		['RD1-group02'] = 'RD1-group2',
		['RD1-group03'] = 'RD1-group3',
		['RD1-group04'] = 'RD1-group4',
		['RD1-group05'] = 'RD1-group5',
		['RD1-group06'] = 'RD1-group6',
		['RD1-group07'] = 'RD1-group7',
		['RD1-group08'] = 'RD1-group8',
		['RD1-group09'] = 'RD1-group9',
	}, {
		autoseeds = 'y',
		RD1 = "1st Round",
		RD2 = "2nd Round",
		RD3 = "3rd Round",
	})
end

p['list'] = function (frame)
	local funcs={}

	for k,_ in pairs(p) do
		if string.match(k, "^%d+Team") then
			table.insert(funcs, k)
		end
	end
	table.sort(funcs, function (a,b)
		na, nb = tonumber(string.match(a, "^%d+")), tonumber(string.match(b, "^%d+"))
		return na == nb and string.match(a, "^%d*(.*)$") < string.match(b, "^%d*(.*)$") or na < nb
	end)
	
	return frame.preprocess(frame,
		"\n* {{tl|"..table.concat(funcs, "}}\n* {{tl|").."}}")
end

p['doc'] = function (frame)
	local pagename = mw.title.getCurrentTitle().rootText
	if type(p[pagename]) == "function" then
		return frame.preprocess(frame, "{{tlx|"..pagename.."}} can be " ..
			"replaced with {{mlx|bracket|"..pagename.."}}.")
	end
	return frame.preprocess(frame, "In most cases, the name of the bracket " ..
		"template can be used as the function name for {{ml|Bracket}}, e.g. " ..
		"{{tlx|16TeamBracket-Compact-Tennis3}}→" ..
		"{{mlx|bracket|16TeamBracket-Compact-Tennis3}}.")
end

return p