\ProvidesPackage{mqfqname}
\edef\fqname{NULL}
\RequirePackage{mqlua}
\RequirePackage{etoolbox}

\begin{luacode}
    sections = sections or {}

    function fqnameEnter(name)
        table.insert(sections, name)
        -- table.sort(sections)
    end

    function fqnameLeave()
        if table.getn(sections) > 0 then
            table.remove(sections)
        end
    end

    function fqnameGet()
        return table.concat(sections, ":")
    end

    function fqnameLeaveOnlyFirstN(n)
        if n >= 0 then
            while table.getn(sections) > n do
                table.remove(sections)
            end
        end
    end
\end{luacode}

\begin{luacode}
    function fqnameGetDepth()
        return table.getn(sections)
    end

    function fqnameGetN(N)
        if N == nil or table.getn(sections) < N then
            luatexbase.module_warning('fqnameGetN', 'N = ' .. N .. ' is larger then the table length')
            return "?!?"
        end
        s = sections[1]
        for i = 2, N do
            s = s .. ":" .. sections[i]
        end
        return s
    end
\end{luacode}


% Allow using :<key>, ::<key> and so on
% where : points to current fqname, :: to the upper one and so on
\begin{luacode*}
    function translateRelativeFqname(target)
        local relN = 0

        local relTarget = ""
        warning('translateRelativeFqname', '(target=' .. target .. ') ');
        for i = 1, #target do
            local c = target:sub(i,i)
            if c == ":" then
                relN = relN + 1
            else
                relTarget = target:sub(i,#target)
                break
            end
        end
        if relN == 0 then
            return target
        end
        
        local N = fqnameGetDepth()
        local newtarget = fqnameGetN(N - relN + 1) .. ":" .. relTarget
        warning('translateRelativeFqname', '(relN=' .. relN .. ') ' .. newtarget);
        return newtarget
    end
\end{luacode*}

\newcommand{\mqfqname@update}{%
    \edef\fqname{\luavar{fqnameGet()}} %
}
\newcommand{\mqfqname@enter}[1]{%
    \directlua{fqnameEnter("\luaescapestring{#1}")}%
    \mqfqname@update
}
\newcommand{\mqfqname@leave}{%
    \directlua{fqnameLeave()}%
    \mqfqname@update
}

\newcommand{\mqfqname@leaveOnlyFirstN}[1]{%
    \directlua{fqnameLeaveOnlyFirstN(#1)}%
}


% Define translations for the current fqname
% [1]: language
% 2: name
% 3: description -> :desc
% 4: definitions/links -> :defs
\newcommand{\desc}[4][english]{
    % language, name, description, definitions
    \ifblank{#2}{}{\dt{#1}{#2}}
    \ifblank{#3}{}{\dt[desc]{#1}{#3}}
    \ifblank{#4}{}{\dt[defs]{#1}{#4}}
}

% SECTIONING
% start <section>, get heading from translation, set label
% fqname is the fully qualified name of all sections and formulas, the keys of all previous sections joined with a ':'
% fqname is secFqname:<key> where <key> is the key/id of some environment, like formula
% [1]: code to run after setting \fqname, but before the \part, \section etc
% 2: key

% 1: depth
% 2: key
% 3: Latex section command
\newcommand\mqfqname@section[3]{
    \mqfqname@leaveOnlyFirstN{#1}
    \mqfqname@enter{#2}
    % this is necessary so that \part/\section... takes the fully expanded string. Otherwise the pdf toc will have just the fqname
    \edef\fqnameText{\GT{\fqname}}
    #3{\fqnameText} 
    \mqfqname@label
    \IfTranslationExists{\fqname:desc}{
        {\color{fg1} \GT{\fqname:desc}}
    }{}
}


\newcommand{\Part}[1]{
    \newpage
    \mqfqname@section{0}{#1}{\part}
}
\newcommand{\Section}[1]{
    \mqfqname@section{1}{#1}{\section}
}
\newcommand{\Subsection}[1]{
    \mqfqname@section{2}{#1}{\subsection}
}
\newcommand{\Subsubsection}[1]{
    \mqfqname@section{3}{#1}{\subsubsection}
}
\newcommand{\Paragraph}[1]{
    \mqfqname@section{4}{#1}{\paragraph}
}

\newcommand\printFqName{\expandafter\detokenize\expandafter{\fqname}}
