-- This module implements [[قالب:Parameters]].
-- [SublimeLinter luacheck-globals:mw]
local DEFINITIONS =
{
    ["image"] =    {
        ["code"] = '<!--  مثل example.jpg -->',
        ["dlist"] = 'مثل example.jpg',
        },
    ["صورة"] =    {
        ["code"] = '<!--  مثل example.jpg -->',
        ["dlist"] = 'مثل example.jpg',
        },
	["alt"] = {
		["code"] = '<!-- text alternative for image; see WP:ALT -->',
		["dlist"] = 'text alternative for image; see [[WP:ALT]]'},
	["coordinates"] = {
		["code"] = '<!-- استخدم {{Coord}} -->',
		["dlist"] = 'استخدم {{قا|Coord}}'},
	["coords"] = {
		["code"] = '<!-- استخدم {{Coord}} -->',
		["dlist"] = 'استخدم  {{قا|Coord}}'},
	["native_name"] = {
		["code"] = '<!-- name in local language; if more than one, separate ' ..
			'using {{قائمة بسيطة}} use {{رمز لغة}}, and omit native_name_lang -->',
		["dlist"] = 'name in local language; if more than one, separate ' ..
			'using {{قا|قائمة بسيطة}}, use {{قا|رمز لغة}}, and omit {{Para|native_name_lang}}'},
	["native_name_lang"] = {
		["code"] = '<!-- language two- or three-letter ISO code -->',
		["dlist"] = 'language two- or three-letter ISO code'},
	["start_date"] = {
		["code"] = '<!-- {{تاريخ بداية|YYYY|MM|DD|df=y}} -->',
		["dlist"] = 'استخدم {{Tlx|Start date|YYYY|MM|DD|df=y}}'},
	["end_date"] = {
		["code"] = '<!-- {{تاريخ نهاية|YYYY|MM|DD|df=y}} -->',
		["dlist"] = 'استخدم {{Tlx|Start date|YYYY|MM|DD|df=y}}'},
	["url"] = {
		["code"] = '<!-- استخدم {{مسار|example.com}} -->',
		["dlist"] = 'استخدم {{قا|URL}}'},
	["website"] = {
		["code"] = '<!-- استخدم {{مسار|example.com}} -->',
		["dlist"] = 'استخدم {{Tls|URL|example.com}}'},}

local p = {}
local removeDuplicates = require('Module:TableTools').removeDuplicates
local yesno = require('Module:Yesno')

local function makeInvokeFunction(funcName)
	return function(frame)
		local getArgs = require('Module:Arguments').getArgs
		return p[funcName](getArgs(frame, {removeBlanks = false}))
	end
end

local function extractParams(page)
	local source = mw.title.new(page, 'Template'):getContent()

	local parameters = {}
	for parameter in string.gmatch(source, '{{{(.-)%f[}|<>]') do
		table.insert(parameters, parameter)
	end
     return removeDuplicates(parameters)
end

local function map(tbl, transform)
	local returnTable = {}
	for k, v in pairs(tbl) do
		returnTable[k] = transform(v)
	end
	return returnTable
end

local function strMap(tbl, transform)
	local returnTable = map(tbl, transform)
	return table.concat(returnTable)
end

function p._check(args)
	local title = args._base or mw.title.getCurrentTitle().fullText
	local unknown = args.unknown or '[[تصنيف:صفحات تستخدم '..title ..' مع وسائط غير معروفة|_VALUE_]]' --' '--
	--local preview = args.preview or "الوسيط '''_VALUE_''' غير مدعوم في [[".. title .."]]"
	local template = string.gsub(title, 'قالب:', '')
	return string.format(
		'<!-- هذا النص تم توليده تلقائياً بواسطة [[قالب:Parameters]]  \n' ..
		' -->' ..
		'{{#استدعاء:Check for unknown parameters|check\n' ..
		'|unknown=%s\n' ..
		--'|preview =%s\n' ..
		'|template =%s\n' ..
		'|%s}}<!-- '..
		'\n -->', unknown
	--	, preview,
		,template,
		table.concat(extractParams(args.base), '|'))
end

function p._code(args)
	local definitions = yesno(args.definitions, true)
	local pad = yesno(args.pad)

	local parameters = extractParams(args.base)
	-- Space-pad the parameters to align the equal signs vertically
	if pad then
		local lengthPerPara = map(parameters, function (parameter)
			return string.len(parameter) end)
		-- Lua doesn't support printf's <*> to specify the width, apparently
		local fs = string.format('%%-%ss', math.max(unpack(lengthPerPara)))
		for i, parameter in pairs(parameters) do
			parameters[i] = string.format(fs, parameter)
		end
	end

	local title = args._base or mw.title.getCurrentTitle().baseText
	return string.format([[{{%s
%s}}]], title, strMap(parameters,
		function(s)
			if definitions then
				return string.format('| %s = %s\n', s,
					DEFINITIONS[s] and DEFINITIONS[s].code or '')
			else
				return string.format('| %s = \n', s)
			end
		end))
end


function p._codeauto(args)
	local definitions = yesno(args.definitions, true)
	--local definitions = getpramvalue(args.definitions)
	local pad = yesno(args.pad)

	local parameters = extractParams(args.base)
	-- Space-pad the parameters to align the equal signs vertically
	if pad then
		local lengthPerPara = map(parameters, function (parameter)
			return string.len(parameter) end)
		-- Lua doesn't support printf's <*> to specify the width, apparently
		local fs = string.format('%%-%ss', math.max(unpack(lengthPerPara)))
		for i, parameter in pairs(parameters) do
			parameters[i] = string.format(fs, parameter)
		end
	end

	local title = args._base or mw.title.getCurrentTitle().baseText
	local params = strMap(parameters,
		function(s)
			if definitions then
				dee =  string.format('| %s = %s\n', s,
					DEFINITIONS[s] and DEFINITIONS[s].code or '')
			else
				dee =  string.format('| %s = \n', s)
			end
			return dee
	end)
	local dert =  string.format([[{{%s
%s}}]], title, params )
	
	if not dee or dee == '' or params == "| state = \n" or params == "|  = \n"
	then 
		return nil
	else
		mw.log( "'" .. params .. "'" )
		return dert
	end

end


function p._compare(args)
	local Set = require('Module:Set')

	local function normaliseParams(parameters)
		local paramsNorm = {}
		-- Prepare a key lookup metatable, which will hold the original
		-- parameter names for each normalised parameter, e.g.
		-- [test] = {TEST, Test}. paramIndex functions like a Python
		-- defaultdict, where the default is a table.
		local paramIndex = setmetatable({}, {__index = function(t, k)
			if not rawget(t, k) then
				rawset(t, k, {})
			end
			return rawget(t, k)
		end})
		for _, parameter in pairs(parameters) do
			table.insert(paramsNorm,
				string.lower(string.gsub(parameter, '%A', '')))
			table.insert(paramIndex[
				string.lower(string.gsub(parameter, '%A', ''))], parameter)
		end

		paramsNorm = removeDuplicates(paramsNorm)
		-- Overload key lookup in paramsNorm. While [[Module:Set]] will
		-- operate on the table (which is to say, the normalised parameters
		-- array), key access will be by way of the paramIndex metatable.
		setmetatable(paramsNorm, {__index = paramIndex})
		return paramsNorm
	end

	local baseParams = extractParams(args.base)
	local otherParams = extractParams(args.other)
	local baseNormParams = normaliseParams(Set.valueComplement(
			otherParams, baseParams))
	local otherNormParams = normaliseParams(otherParams)

	return string.format([[وسائط مطابقة:

%s

وسائط القالب %s:

%s

متفرقة بين القالبين:

%s]],
	strMap(Set.valueIntersection(baseParams, otherParams),
		function(v) return string.format('%s؛ ', v) end),
	args.other ,
	strMap(Set.valueIntersection(baseNormParams, otherNormParams),
		function(v) return string.format('* %s < %s [%s]\n',
			table.concat(baseNormParams[v], '؛ '),
			table.concat(otherNormParams[v], '؛ '),
			v)
		end),
	strMap(Set.valueComplement(otherNormParams, baseNormParams),
		function(v) return strMap(baseNormParams[v],
			function(s) return string.format('%s؛ ', s) end)
		end))
end

function p._demo(args)
	local title = args._base and ('|_template=' .. args._base) or ''
	local display = args._display and ('|_display=' .. args._display) or ''

	return string.format(
		'<!-- هذا النص تم توليده تلقائياً بواسطة [[قالب:Parameters]]  \n' ..
		' -->' ..
		'{{مثال وسائط قالب \n' ..
		'%s\n'..
		'%s\n'..
		'|%s}}<!-- '..
		'\n -->', title, display,
		table.concat(extractParams(args.base), '|'))
end

function p._vlist(args)

	return string.format(
		'\n{{رأس بيانات القالب}}\n' ..
		'{{div|700|70}}' ..
		'\n<templatedata>\n' ..
		'{\n' ..
		'	"params": {\n' ..
		'		"%s'..
		'": {}\n'..
		'},\n"format": "block"\n'..
		'}'..
		'</templatedata>\n{{div2}}', 
		table.concat(extractParams(args.base), '": {},\n		"'))
end
function p._dlist(args)
	local definitions = yesno(args.definitions, true)
	return strMap(extractParams(args.base),
		function(s)
			if definitions then
				return string.format('; %s: %s\n', s,
					DEFINITIONS[s] and DEFINITIONS[s].dlist or '')
			else
				return string.format('; %s: \n', s)
			end
		end)
end

function p._list(args)
	return strMap(extractParams(args.base),
		function(s) return string.format('* %s\n', s) end)
end



p.check = makeInvokeFunction('_check')
p.code = makeInvokeFunction('_code')
p.codeauto = makeInvokeFunction('_codeauto')
p.compare = makeInvokeFunction('_compare')
p.demo = makeInvokeFunction('_demo')
p.dlist = makeInvokeFunction('_dlist')
p.list = makeInvokeFunction('_list')
p.vlist = makeInvokeFunction('_vlist')

return p