local p = {} --p stands for package

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')

function plainlinks_span(text)
	if not text then
		return nil
	else
		return "<span class='plainlinksneverexpand plainlinks'>" .. text .. "</span>"
	end
end

function p._link(args)
	-- args
	local pipe = args.pipe
	local plainlinks = yesno(args.plainlinks)
	
	local title = args['title'] or args[1]
	local section = args['section']
	local part = args['part']
	local chapter = args['chapter']
	local subtitle = args['subtitle']
	local subchapter = args['subchapter']
	local posttitle = args['posttitle']
	local endtext = args['endtext']
	
	-- link
	local link = "https://www.law.cornell.edu/uscode/text/" .. title
	
	if posttitle then
		link = link .. "/" .. posttitle
	end
	
	if section then
		link = link .. "/" .. section
	else
		if subtitle then
			link = link .. "/subtitle-" .. subtitle
		end
		
		if part then
			link = link .. "/part-" .. part
		end
		
		if chapter then
			link = link .. "/chapter-" .. chapter
		end
		
		if subchapter then
			link = link .. "/subchapter-" .. subchapter
		end
	end
	
	if endtext then
		link = link .. "/" .. endtext
	end
	
	local text
	if pipe then
		text = "[" .. link .. " " .. pipe .. "]"
	else
		text = link
	end
	
	if plainlinks then
		return plainlinks_span(text)
	else
		return text
	end
end

function p.link(frame)
	return p._link(getArgs(frame))
end

function p.UnitedStatesCode(frame)
	local args = getArgs(frame)
	
	local title = args[1] or args['title']
	local section = args[2] or args['sect'] or args['section']
	local pipe = args['pipe']
	
	local text
	if pipe then
		text = p._link({title, section = section, pipe = pipe})
	else
		local titleLink = p._link({title, ['pipe'] = title})
		local sectionLink = p._link({title, ['section'] = section, ['pipe'] = section})
		text = titleLink .. " [[United States Code|U.S.C.]] " .. sectionLink
	end
	return plainlinks_span(text)
end

function p.uscClause(frame)
	local args = getArgs(frame)
	
	local title = args[1] or args['title']
	local section = args[2] or args['sect'] or args['section']
	local clause = args[3] or args['clause']
	local pipe = args.pipe
	
	local text
	if pipe then
		text = p._link({title, section = section, pipe = pipe})
	else
		local titleLink = p._link({title, pipe = title})
		
		local pipeText
		if clause then
			pipeText = section .. "§&nbsp;" .. clause
		else
			pipeText = section
		end
		
		local sectionLink = p._link({title, section = section, pipe = pipeText})
		
		text = titleLink .. " [[United States Code|U.S.C.]] " .. sectionLink
	end
	return plainlinks_span(text)
end

function subLinks(args)
	local ids = {}
	local idsInParens = {}
	
	for k, arg in pairs(args) do
		if tonumber(k) and k > 2 then
			table.insert(ids, arg)
			table.insert(idsInParens, "(" .. arg .. ")")
		end
	end
	
	local idJumpText = table.concat(ids, "_")
	if #ids > 0 then
		idJumpText = "#" .. table.concat(ids, "_")
	end
	
	local sectionText = args[2] .. table.concat(idsInParens)
	
	return {idJumpText = idJumpText, sectionText = sectionText}
end

function p._UnitedStatesCodeSub(args)
	local title = args[1]
	local section = args[2]
	local compact = yesno(args.compact)
	
	local subLinks = subLinks(args)
	local idJumpText = subLinks.idJumpText
	local sectionText = subLinks.sectionText
	
	local text
	if compact then
		text = p._link({title, section = section, endtext = idJumpText, pipe = sectionText})
	elseif args.pipe then
		text = p._link({title, section = section, endtext = idJumpText, pipe = args.pipe})
	else
		local liiLink = p._link({title, section = section, endtext = idJumpText, "§&nbsp;" .. sectionText})
		text = "[[United States Code/Title " .. title .. "|" .. title .. " U.S.C.]] " .. liiLink
	end
	
	return plainlinks_span(text)
end

function p.UnitedStatesCodeSub(frame)
	return p._UnitedStatesCodeSub(getArgs(frame))
end

return p