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

\begin{luacode}
    constants = {}
    function constantAdd(key, symbol, exp_or_def, fqname)
        constants[key] = {
            ["symbol"] = symbol,
            ["units"] = units,
            ["exp_or_def"] = exp_or_def,
            ["values"] = {}  -- array of {value, unit}
        }
        if fqname == "" then 
            constants[key]["fqname"] = fqnameGet()
        else
            constants[key]["fqname"] = fqname
        end
    end
    function constantAddValue(key, value, unit)
        table.insert(constants[key]["values"], { value = value, unit = unit })
    end
    function constantGetSymbol(key)
        local const = constants[key]
        if const == nil then return "???" end
        local symbol = const["symbol"]
        if symbol == nil then return "???" end
        return symbol
    end
    function constantGetFqname(key)
        local const = constants[key]
        if const == nil then return "const:"..key end
        local fqname_ = const["fqname"]
        if fqname_ == nil then return "const:"..key end
        return fqname_
    end
\end{luacode}

% [1]: label to point to
% 2: key
% 3: symbol
% 4: either exp or def; experimentally or defined constant
\newcommand{\constant@new}[4][]{%
    \directLuaAuxExpand{constantAdd(\luastring{#2}, \luastringN{#3}, \luastringN{#4}, \luastring{#1})}%
}

% 1: key
% 2: value
% 3: units
\newcommand{\constant@addValue}[3]{%
    \directlua{constantAddValue(\luastring{#1}, \luastringN{#2}, \luastringN{#3})}%
}
% 1: key
\newcommand{\constant@getSymbol}[1]{\luavar{constantGetSymbol(\luastring{#1})}}

% 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
        }
    \endgroup
}
