-- Returns the number of seats in the US House of Representatives held by a given state in a given year (If no year is given, the current number of seats is returned)
local p = {}

function p.main(frame)
	state = frame.args[1]
	if mw.ustring.len(state)>2 then
		state = frame:expandTemplate{ title = 'ISO 3166 code', args = {"US", state} }
		state = state:sub(4)
	end

	year = tonumber(frame.args[2])
	if year then
		census_number = (year - 1 - math.fmod(year-1,10))/10 - 178
	end
		
	states = {
	AK = {'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',  1,  1,  1,  1,  1,  1,  1},
	AL = {'-','-','-','-',  3,  5,  7,  7,  6,  8,  8,  9,  9, 10,  9,  9,  9,  8,  7,  7,  7,  7,  7,  7},
	AR = {'-','-','-','-','-','-',  1,  2,  3,  4,  5,  6,  7,  7,  7,  7,  6,  4,  4,  4,  4,  4,  4,  4},
	AZ = {'-','-','-','-','-','-','-','-','-','-','-','-','-',  1,  1,  2,  2,  3,  4,  5,  6,  8,  9,  9},
	CA = {'-','-','-','-','-','-','-',  2,  3,  4,  6,  7,  8, 11, 20, 23, 30, 38, 43, 45, 52, 53, 53, 52},
	CO = {'-','-','-','-','-','-','-','-','-','-',  1,  2,  3,  4,  4,  4,  4,  4,  5,  6,  6,  7,  7,  8},
	CT = {  5,  7,  7,  7,  6,  6,  4,  4,  4,  4,  4,  4,  5,  5,  6,  6,  6,  6,  6,  6,  6,  5,  5,  5},
	DE = {  1,  1,  1,  2,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1},
	FL = {'-','-','-','-','-','-','-',  1,  1,  2,  2,  2,  3,  4,  5,  6,  8, 12, 15, 19, 23, 25, 27, 28},
	GA = {  3,  2,  4,  6,  7,  9,  8,  8,  7,  9, 10, 11, 11, 12, 10, 10, 10, 10, 10, 10, 11, 13, 14, 14},
	HI = {'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',  2,  2,  2,  2,  2,  2,  2},
	IA = {'-','-','-','-','-','-','-',  2,  6,  9, 11, 11, 11, 11,  9,  8,  8,  7,  6,  6,  5,  5,  4,  4},
	ID = {'-','-','-','-','-','-','-','-','-','-','-',  1,  1,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2},
	IL = {'-','-','-','-',  1,  3,  7,  9, 14, 19, 20, 22, 25, 27, 27, 26, 25, 24, 24, 22, 20, 19, 18, 17},
	IN = {'-','-','-','-',  3,  7, 10, 11, 11, 13, 13, 13, 13, 13, 12, 11, 11, 11, 11, 10, 10,  9,  9,  9},
	KS = {'-','-','-','-','-','-','-','-',  1,  3,  7,  8,  8,  8,  7,  6,  6,  5,  5,  5,  4,  4,  4,  4},
	KY = {'-','2',  6, 10, 12, 13, 10, 10,  9, 10, 11, 11, 11, 11,  9,  9,  8,  7,  7,  7,  6,  6,  6,  6},
	LA = {'-','-','-',  1,  3,  3,  4,  4,  5,  6,  6,  6,  7,  8,  8,  8,  8,  8,  8,  8,  7,  7,  6,  6},
	MA = {  8, 14, 17, 20, 13, 12, 10, 11, 10, 11, 12, 13, 14, 16, 15, 14, 14, 12, 12, 11, 10, 10,  9,  9},
	MD = {  6,  8,  9,  9,  9,  8,  6,  6,  5,  6,  6,  6,  6,  6,  6,  6,  7,  8,  8,  8,  8,  8,  8,  8},
	ME = {'-','-','-','-',  7,  8,  7,  6,  5,  5,  4,  4,  4,  4,  3,  3,  3,  2,  2,  2,  2,  2,  2,  2},
	MI = {'-','-','-','-','-','-',  3,  4,  6,  9, 11, 12, 12, 13, 17, 17, 18, 19, 19, 18, 16, 15, 14, 13},
	MN = {'-','-','-','-','-','-','-','-',  2,  3,  5,  7,  9, 10,  9,  9,  9,  8,  8,  8,  8,  8,  8,  8},
	MO = {'-','-','-','-',  1,  2,  5,  7,  9, 13, 14, 15, 16, 16, 13, 13, 11, 10, 10,  9,  9,  9,  8,  8},
	MS = {'-','-','-','-',  1,  2,  4,  5,  5,  6,  7,  7,  8,  8,  7,  7,  6,  5,  5,  5,  5,  4,  4,  4},
	MT = {'-','-','-','-','-','-','-','-','-','-','-',  1,  1,  2,  2,  2,  2,  2,  2,  2,  1,  1,  1,  2},
	NC = {  5, 10, 12, 13, 13, 13,  9,  8,  7,  8,  9,  9, 10, 10, 11, 12, 12, 11, 11, 11, 12, 13, 13, 14},
	ND = {'-','-','-','-','-','-','-','-','-','-','-',  1,  2,  3,  2,  2,  2,  2,  1,  1,  1,  1,  1,  1},
	NE = {'-','-','-','-','-','-','-','-','-',  1,  3,  6,  6,  6,  5,  4,  4,  3,  3,  3,  3,  3,  3,  3},
	NH = {  3,  4,  5,  6,  6,  5,  4,  3,  3,  3,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2},
	NJ = {  4,  5,  6,  6,  6,  6,  5,  5,  5,  7,  7,  8, 10, 12, 14, 14, 14, 15, 15, 14, 13, 13, 12, 12},
	NM = {'-','-','-','-','-','-','-','-','-','-','-','-','-',  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  3},
	NV = {'-','-','-','-','-','-','-','-','-',  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  3,  4,  4},
	NY = {  6, 10, 17, 27, 34, 40, 34, 33, 31, 33, 34, 34, 37, 43, 45, 45, 43, 41, 39, 34, 31, 29, 27, 26},
	OH = {'-','-',  1,  6, 14, 19, 21, 21, 19, 20, 21, 21, 21, 22, 24, 23, 23, 24, 23, 21, 19, 18, 16, 15},
	OK = {'-','-','-','-','-','-','-','-','-','-','-','-','-',  8,  9,  8,  6,  6,  6,  6,  6,  5,  5,  5},
	OR = {'-','-','-','-','-','-','-','-',  1,  1,  1,  2,  2,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6},
	PA = {  8, 13, 18, 23, 26, 28, 24, 25, 24, 27, 28, 30, 32, 36, 34, 33, 30, 27, 25, 23, 21, 19, 18, 17},
	RI = {  1,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  3,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2},
	SC = {  5,  6,  8,  9,  9,  9,  7,  6,  4,  5,  7,  7,  7,  7,  6,  6,  6,  6,  6,  6,  6,  6,  7,  7},
	SD = {'-','-','-','-','-','-','-','-','-','-','-',  2,  2,  3,  2,  2,  2,  2,  2,  1,  1,  1,  1,  1},
	TN = {'-','-',  3,  6,  9, 13, 11, 10,  8, 10, 10, 10, 10, 10,  9, 10,  9,  9,  8,  9,  9,  9,  9,  9},
	TX = {'-','-','-','-','-','-','-',  2,  4,  6, 11, 13, 16, 18, 21, 21, 22, 23, 24, 27, 30, 32, 36, 38},
	UT = {'-','-','-','-','-','-','-','-','-','-','-','-',  1,  2,  2,  2,  2,  2,  2,  3,  3,  3,  4,  4},
	VA = { 10, 19, 22, 23, 22, 21, 15, 13, 11,  9, 10, 10, 10, 10,  9,  9, 10, 10, 10, 10, 11, 11, 11, 11},
	VT = {'-',  2,  4,  6,  5,  5,  4,  3,  3,  3,  2,  2,  2,  2,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1},
	WA = {'-','-','-','-','-','-','-','-','-','-','-',  2,  3,  5,  6,  6,  7,  7,  7,  8,  9,  9, 10, 10},
	WI = {'-','-','-','-','-','-','-',  3,  6,  8,  9, 10, 11, 11, 10, 10, 10, 10,  9,  9,  9,  8,  8,  8},
	WV = {'-','-','-','-','-','-','-','-','-',  3,  4,  4,  5,  6,  6,  6,  6,  5,  4,  4,  3,  3,  3,  2},
	WY = {'-','-','-','-','-','-','-','-','-','-','-',  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1}}

	if state=='DC' then
			seats = 0
	elseif not states[state] then
		seats = frame:expandTemplate{ title = 'error', args = {'Error: state not recognized'} }
	else
		if census_number then
			seats = states[state][census_number]
			if seats=='-' or not seats then
				seats = frame:expandTemplate{ title = 'error', args = {"Error: given year is prior to the first census of this state"} }
			end
		else
			seats = states[state][#states[state]]
		end
	end
	
	return seats
end

return p