\ProvidesPackage{mqconstant}
\RequirePackage{mqlua}
\RequirePackage{etoolbox}

\directLuaAux{
    if constants == nil then
        constants = {}
    end
}

% [1]: label to point to
% 2: key
% 3: symbol
% 4: either exp or def; experimentally or defined constant
\newcommand{\constant@new}[4][\relax]{
    \directLuaAux{
        constants["#2"] = {}
        constants["#2"]["symbol"]  = [[\detokenize{#3}]]
        constants["#2"]["exp_or_def"]   = [[\detokenize{#4}]]
        constants["#2"]["values"] = {} %-- array of {value, unit}
    }
    \ifstrempty{#1}{}{
        \directLuaAuxExpand{
            constants["#2"]["linkto"] = [[#1]] %-- fqname required for getting the translation key
        }
    }
}

% 1: key
% 2: value
% 3: units
\newcommand{\constant@addValue}[3]{
    \directlua{
        table.insert(constants["#1"]["values"], { value = [[\detokenize{#2}]], unit = [[\detokenize{#3}]] })
    }
}

% 1: key
\newcommand\constant@print[1]{
    \begingroup % for label
        Symbol: $\luavar{constants["#1"]["symbol"]}$
        % \\Unit: $\directlua{split_and_print_units(constants["#1"]["units"])}$
        \directlua{
            tex.print("\\\\\\GT{const:"..constants["#1"]["exp_or_def"].."}")
        }
        \directlua{
            %--tex.sprint("Hier steht Luatext" .. ":", #constVals)
            for i, pair in ipairs(constants["#1"]["values"]) do
            tex.sprint("\\\\\\hspace*{1cm}${", pair["value"], "}\\,\\si{", pair["unit"], "}$")
                %--tex.sprint("VALUE ", i, v)
            end
        }
        % label it only once
        \directlua{ 
            if constants["#1"]["labeled"] == nil then
                constants["#1"]["labeled"] = true
                tex.print("\\label{const:#1}")
            end
        }
    \endgroup
}
\newcounter{constant}
