local stack = require('Module:String', 'Module:UnitTests');

--[[
top

Vrací vrcholovou šablonu ze stacktrace, poslední použitou, tedy nejspíše i error rootcause.

Použití:
{{#invoke:Stacktrace|top|stacktrace}}
 NEBO
{{#invoke:Stacktrace|top|stacktrace=stacktrace}}

Parametry
    stacktrace: Řetězcová konstrukce pro stacktrace, jak si jí šablony předávají
        mezi sebou; povinný vstup.

If invoked using named parameters, Mediawiki will automatically remove any leading or
trailing whitespace from the parameter.  In some circumstances this is desirable, in 
other cases one may want to preserve the whitespace.

Vrací se opět string, tedy název poslední šablony, které jsou od sebe odděleny
znakem ">", což se naprosto předpokládá jako klíčový princip.

This function should be safe for UTF-8 strings.
]]
function stacktrace.top(frame)
    local new_args = _getParameters(frame.args, {'stacktrace'});
    local stacktrace_str = new_args['stacktrace'] or '';
    if stacktrace_str=='' then return nil; end -- požadován
    local members = string.split(stacktrace_str, '>', 1, true); -- stringy
    return members[table.getn(members)]; -- poslední je top
end

--[[ helpers ]]
-- args --
function stack._getParameters(frame_args, arg_list)
	return string._getParameters(frame_args, arg_list);
end

-- err --
function stack._error(error_str)
	return string._error(error_str);
end

-- boolean --
function stack._getBoolean(boolean_str)
	return string._getBoolean(boolean_str);
end

return stack;