local process_params = require("Module:parameters").process
local remove = table.remove

local export = {}

--[=[
	Modules used:
	[[Module:links]]
	[[Module:languages]]
	[[Module:scripts]]
	[[Module:parameters]]
	[[Module:debug]]
]=]

do
	local function get_args(frame)
		-- `compat` is a compatibility mode for {{term}}.
		-- If given a nonempty value, the function uses lang= to specify the
		-- language, and all the positional parameters shift one number lower.
		local iargs = frame.args
		iargs.compat = iargs.compat and iargs.compat ~= ""
		iargs.langname = iargs.langname and iargs.langname ~= ""
		iargs.notself = iargs.notself and iargs.notself ~= ""
		local alias_of_4 = {alias_of = 4}
		local boolean_default_false = {type = "boolean", default = false}
		local plain = {}
		local params = {
			[1] = {required = true, type = "language", etym_lang = true, default = "und"},
			[2] = plain,
			[3] = plain,
			[4] = plain,
			["g"] = {list = true},
			["gloss"] = alias_of_4,
			["id"] = plain,
			["lit"] = plain,
			["pos"] = plain,
			["sc"] = {type = "script"},
			["t"] = alias_of_4,
			["tr"] = plain,
			["ts"] = plain,
			["accel-form"] = plain,
			["accel-translit"] = plain,
			["accel-lemma"] = plain,
			["accel-lemma-translit"] = plain,
			["accel-gender"] = plain,
			["accel-nostore"] = {type = "boolean"},
		}
		if iargs.compat then
			params["lang"] = {type = "language", etym_lang = true, default = "und"}
			remove(params, 1)
			alias_of_4.alias_of = 3
		end
		if iargs.langname then
			params["w"] = boolean_default_false
		end
		return process_params(frame:getParent().args, params), iargs
	end
	
	-- Used in [[Template:l]] and [[Template:m]].
	function export.l_term_t(frame)
		local m_debug = require("Module:debug")
		
		local args, iargs = get_args(frame)
		local compat = iargs.compat
		local lang = args[compat and "lang" or 1]
		
		-- Tracking for und.
		if not compat and lang:getCode() == "und" then
			m_debug.track("link/und")
		end
		
		local term = args[(compat and 1 or 2)]
		local alt = args[(compat and 2 or 3)]
		term = term ~= "" and term or nil
		
		if not term and not alt and iargs.demo then
			term = iargs.demo
		end
		
		local langname = iargs.langname and (
			args.w and lang:makeWikipediaLink() or
			lang:getCanonicalName()
		) or nil
		
		if langname and term == "-" then
			return langname
		end
		-- Forward the information to full_link
		return (langname and langname .. " " or "") .. require("Module:links").full_link(
			{
				lang = lang,
				sc = args.sc,
				track_sc = true,
				term = term,
				alt = alt,
				gloss = args[4],
				id = args["id"],
				tr = args["tr"],
				ts = args["ts"],
				genders = args["g"],
				pos = args["pos"],
				lit = args["lit"],
				accel = args["accel-form"] and {
					form = args["accel-form"],
					translit = args["accel-translit"],
					lemma = args["accel-lemma"],
					lemma_translit = args["accel-lemma-translit"],
					gender = args["accel-gender"],
					nostore = args["accel-nostore"],
				} or nil
			},
			iargs.face,
			not iargs.notself
		)
	end
end

-- Used in [[Template:ll]].
do
	local function get_args(frame)
		local plain = {}
		return process_params(frame:getParent().args, {
			[1] = {required = true, type = "language", etym_lang = true, default = "und"},
			[2] = {allow_empty = true},
			[3] = plain,
			["id"] = plain,
			["sc"] = {type = "script"},
		})
	end
	
	function export.ll(frame)
		local args = get_args(frame)
		local lang = args[1]
		local sc = args["sc"]
		local term = args[2]
		term = term ~= "" and term or nil
		
		return require("Module:links").language_link{
			lang = lang,
			sc = sc,
			term = term,
			alt = args[3],
			id = args.id
		} or "<small>[Mục từ gì?]</small>" ..
			require("Module:utilities").format_categories(
				{"Yêu cầu mục từ " .. lang:getFullName()},
				lang, "-", nil, nil, sc
			)
	end
end

function export.def_t(frame)
	local args = process_params(frame:getParent().args, {
		[1] = {required = true, default = ""},
	})
	
	local lang = require("Module:languages").getByCode("en")
	local sc = require("Module:scripts").getByCode("Latn")
	
	local text = require("Module:links").embedded_language_links{
		term = args[1],
		lang = lang,
		sc = sc
	}
	return require("Module:script utilities").tag_text(text, lang, sc)
end


function export.linkify_t(frame)
	local args = process_params(frame:getParent().args, {
		[1] = {required = true, default = ""},
	})
	
	args[1] = mw.text.trim(args[1])
	
	if args[1] == "" or args[1]:find("[[", nil, true) then
		return args[1]
	else
		return "[[" .. args[1] .. "]]"
	end
end

function export.section_link_t(frame)
	local args = process_params(frame:getParent().args, {
		[1] = {},
	})
	
	return require("Module:links").section_link(args[1])
end

return export