Documentation for this module may be created at Module:Ref/doc

local p = {}

function ContainsOr(base, options)
	local found = false
	for _, option in ipairs(options) do
	    if base == option then
	        found = true
	        break
	    end
	end
	return found
end
	
function CreateLink(url, title)
	return "[" .. url .. " " .. title .. "]"
end

function p.main(frame)
	local parent_args = frame:getParent().args
	local final_content = ""
	
	-- Variables from parent template.
		-- Ref tag parameters.
		local name = parent_args["name"]
		local group = parent_args["group"] -- "lower-alpha" or nil mostly
		-- MLA citation parameters.
		local title = parent_args["title"]
		local url = parent_args["url"]
		local author = parent_args["author"]
		local publisher = parent_args["publisher"]
		local platform = parent_args["platform"]
		local handle = parent_args["handle"]
		local date = parent_args["date"]
		local archived = parent_args["archived"]
		local isbn = parent_args["isbn"]
		local journal = parent_args["journal"]
		local page = parent_args["page"]
	
	-- Link platform-specific handles and get name.
	local handle_link = ""
	local platform_name = ""
	if ContainsOr(platform, {"Twitter", "X", "twitter", "x"}) then
		handle_link = "[https://twitter.com/@" .. handle .. " @" .. handle .. "]"
		platform_name = "Twitter / X"
	elseif ContainsOr(platform, {"YouTube", "youtube", "YT", "yt", "Youtube"}) then
		handle_link = CreateLink("https://www.youtube.com/@" .. handle, "@" .. handle)
		platform_name = "YouTube"
	elseif ContainsOr(platform, {"JoJo News", "jojonews", "jojo news", "JOJO News", "JoJo-News", "jojo-news"}) then
		handle_link = CreateLink("https://jojo-news.com/author/" .. handle, handle)
		platform_name = "JoJo-News"
	end
		
	if parent_args[1] then
		final_content = parent_args[1]
	elseif title then
		if author then
			final_content = final_content .. author
			if handle then
				final_content = final_content .. " <nowiki>[</nowiki>" .. handle_link .. "]"
			end
			if date then 
				final_content = final_content .. " (" .. date .. ")"
			end
			-- Add comma after author/date if there's a title
			final_content = final_content .. ", "
		end
		if url then
			final_content = final_content .. "''\"" .. CreateLink(url, title) .. "\"''"
			if archived == true then
				final_content = final_content .. " (Archived)"
			end
		else
			final_content = final_content .. "''\"" .. title .. "\"''"
		end
		if journal then
			final_content = final_content .. ", " .. journal
		end
		if page then
			final_content = final_content .. ", p. " .. page
		end
		if publisher then
			final_content = final_content .. ", " .. publisher
		elseif platform then
			final_content = final_content .. ", on " .. platform_name
		end
		if (not author) and date then
			final_content = final_content .. ", " .. date
		end
		final_content = final_content .. "."
		if isbn then
			final_content = final_content .. " ISBN: " .. frame:expandTemplate{ title = "ISBN", args = { isbn }}
		end
	end
	
	return frame:extensionTag{ name = "ref", content = final_content, args = { name = name, group = group }}
end

return p