ХӀокху модулах лаьцна хааман Модуль:Sports kit/doc агӀо кхолла мега

local p = {} -- p stands for package

function get_param(args, names, duplicate_errors)
	local value = nil
	local chosen_names = {}
	for _, name in pairs(names) do
		if args[name] and args[name] ~= '' then
			value = args[name]
			table.insert(chosen_names, name)
		end
	end
	
	if #chosen_names > 1 then
		table.insert(duplicate_errors, chosen_names)
	end
	
	return value
end

function has_value (tab, val)
    for _, value in ipairs(tab) do
        if value == val then
            return true
        end
    end

    return false
end

function p.main(frame)
	local args = frame:getParent().args
	local data = require('Module:Sports kit/' .. frame.args[1])
	if data.preformat then
		data.preformat(args)
	end
	
	local color_errors = {}
	local pattern_errors = {}
	local empty_errors = {}
	local duplicate_errors = {}
	
	local filetype = get_param(args, data.filetype_params or {}, duplicate_errors) or 'png'
	local title = get_param(args, data.title_params or {}, duplicate_errors)
	
	local result = ''
	local max_right = 0
	local max_bottom = 0
	local found_elements = false
	
	
	for _, element in pairs(data.elements) do
		local element_result = ''
		
		local pattern = get_param(args, element.pattern_params, duplicate_errors)
		local pattern_error = false
		if pattern then
			pattern_filename = element.prefix .. pattern .. '.' .. filetype
			if not mw.title.new('Media:' .. pattern_filename).exists then
				pattern_error = true
				table.insert(pattern_errors, element)
			end
		end
		
		local color = get_param(args, element.color_params, duplicate_errors)
		local ignore_element = has_value(data.none_values or {}, color)
		local color_error = false
		if color ~= nil and not ignore_element and not ((#color == 3 or #color == 6) and string.match(color, '^[0-9a-fA-F]+$')) then
			color_error = true
			table.insert(color_errors, element)
		end
		
		if pattern == nil and color == nil then
			table.insert(empty_errors, element)
		else
			found_elements = true -- "none" value?
		end
		
		local alt = nil
		if data.alt_element == element then
			alt = get_param(args, data.alt_params or {}, duplicate_errors) or data.default_alt
		end
		
		if not ignore_element then
			max_right = math.max(max_right, element.left + element.width)
			max_bottom = math.max(max_bottom, element.top + element.height)
			
			position = 'position: absolute; left: ' .. element.left .. 'px; top: ' .. element.top ..
				'px; width: ' .. element.width .. 'px; height: ' .. element.height .. 'px;'
			
			element_result = '<div style="' .. position
			if color and not color_error then
				element_result = element_result .. ' background-color: #' .. color .. ';'
			end
			element_result = element_result .. '">'
			if pattern and not pattern_error then
				element_result = element_result .. '[[File:' .. pattern_filename .. '|top|link='
				element_result = element_result .. '|alt=' .. (alt or '')
				element_result = element_result .. ']]'
			end
			element_result = element_result .. '</div>'
			element_result = element_result .. '<div style="' .. position .. '">'
			element_result = element_result .. '[[File:' .. (element.main_file or (element.prefix .. '.svg')) .. '|top|link='
			element_result = element_result .. '|alt=' .. (alt or '')
			element_result = element_result .. ']]</div>'
		end
		
		result = result .. element_result
	end
	
	
	result = '<div style="width: ' .. max_right .. 'px; margin: 0 auto; padding: 0;">' ..
		'<div style="background-color: white; position: relative; left: 0px; top: 0px; width: ' .. max_right ..
			'px; height: ' .. max_bottom ..  'px; margin: 0 auto; padding: 0;">' ..
		result .. '</div>'
	if title then
		result = result .. '<div style="padding-top: 0.6em; text-align: center; font-weight: bold; line-height: 1.4em;">' .. title .. '</div>'
	end
	result = result .. '</div>'
	
	
	error_messages = {}
	if #color_errors > 0 then
		color_errors_messages = {}
		for _, element in pairs(color_errors) do
			table.insert(color_errors_messages, element.genitive)
		end
		table.insert(error_messages, data.errors.in_colors .. ' ' .. mw.text.listToText(color_errors_messages))
	end
	if #pattern_errors > 0 then
		pattern_errors_messages = {}
		for _, element in pairs(pattern_errors) do
			table.insert(pattern_errors_messages, element.genitive)
		end
		table.insert(error_messages, data.errors.in_patterns .. ' ' .. mw.text.listToText(pattern_errors_messages))
	end
	if #empty_errors > 0 then
		empty_errors_messages = {}
		for _, element in pairs(empty_errors) do
			table.insert(empty_errors_messages, element.genitive)
		end
		table.insert(error_messages, data.errors.empty_pairs .. ' ' .. mw.text.listToText(empty_errors_messages))
	end
	if #duplicate_errors > 0 then
		duplicate_errors_messages = {}
		for _, parameter_list in pairs(duplicate_errors) do
			table.insert(duplicate_errors_messages, mw.text.listToText(parameter_list))
		end
		table.insert(error_messages, data.errors.duplicate_args .. ' ' .. table.concat(duplicate_errors_messages, '; '))
	end
	
	
	local params = {}
	for _, element in pairs(data.elements) do
		if element.color_params then
			for _, param in pairs(element.color_params) do
				table.insert(params, param)
			end
		end
		if element.pattern_params then
			for _, param in pairs(element.pattern_params) do
				table.insert(params, param)
			end
		end
	end
	if data.alt_params then
		for _, param in pairs(data.alt_params) do
			table.insert(params, param)
		end
	end
	if data.filetype_params then
		for _, param in pairs(data.filetype_params) do
			table.insert(params, param)
		end
	end
	if data.title_params then
		for _, param in pairs(data.title_params) do
			table.insert(params, param)
		end
	end
	
	local unknown_arguments = {}
	for param, _ in pairs(args) do
		if not has_value(params, param) then
			table.insert(unknown_arguments, param)
		end
	end
	if #unknown_arguments > 0 then
		table.insert(error_messages, data.errors.unknown_args .. ' ' .. mw.text.listToText(unknown_arguments))
	end
	
	
	if #error_messages > 0 then 
		result = result .. '<div style="padding-top: 1em; text-align: center;"><span class="error">' .. data.error_prefix .. 
			':</span> ' .. table.concat(error_messages, ';<br>') .. '.'
		if data.error_category then
			result = result .. '[[Category:' .. data.error_category .. ']]'
		end
		result = result .. '</div>'
	end
	
	if found_elements then
		return result
	else
		return ''
	end
end

return p