\ProvidesPackage{mqquantity}
\RequirePackage{mqlua}
\RequirePackage{mqfqname}
\RequirePackage{etoolbox}

% TODO: MAYBE:
% store the fqname where the quantity is defined
% In qtyRef then use the stored label to reference it, instead of linking to qty:<name>
% Use the mqlua hyperref function

\begin{luacode}
    quantities = {}
    function quantityAdd(key, symbol, units, comment, fqname)
        quantities[key] = {
            ["symbol"] = symbol,
            ["units"] = units,
            ["comment"] = comment
        }
        if fqname == "" then 
            quantities[key]["fqname"] = fqnameGet()
        else
            quantities[key]["fqname"] = fqname
        end
    end
    function quantityGetSymbol(key)
        local qty = quantities[key]
        if qty == nil then return "???" end
        local symbol = qty["symbol"]
        if symbol == nil then return "???" end
        return symbol
    end
    function quantityGetFqname(key)
        local qty = quantities[key]
        if qty == nil then return "qty:"..key end
        local fqname_ = qty["fqname"]
        if fqname_ == nil then return "qty:"..key end
        return fqname_
    end
\end{luacode}

% [1]: label to point to, if not given use current fqname
% 2: key - must expand to a valid lua string!
% 3: symbol
% 4: units
% 5: comment key to translation
\newcommand{\quantity@new}[5][]{%
    \directLuaAuxExpand{quantityAdd(\luastring{#2}, \luastringN{#3}, \luastringN{#4}, \luastringN{#5}, \luastring{#1})}
}

% 1: key
\newcommand{\quantity@getSymbol}[1]{\luavar{quantityGetSymbol(\luastring{#1})}}

% 1: key
\newcommand\quantity@print[1]{
    \begingroup % for label
        Symbol: $\luavar{quantityGetSymbol(\luastring{#1})}$
        \hfill Unit: $\directlua{split_and_print_units(quantities["#1"]["units"])}$ %
    \endgroup%
}
