%% regulatory.sty
%% Copyright 2024 E. Nijenhuis
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
% http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status ‘maintained’.
%
% The Current Maintainer of this work is E. Nijenhuis.
%
% This work consists of the files regulatory.tex,
% regulatory-preamble.tex, regulatory-nl.tex, regulatory-en.tex,
% example1.bib, example2.bib,
% example1-nl.tex, example2-nl.tex,
% example1-en.tex, example2-en.tex,
% md-example.tex, example.md,
% fc-dutch.def and regulatory.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{regulatory}[2024/01/27 0.0.3 Xerdi's Regulatory Package]

\RequirePackage{pgfopts}
\RequirePackage{xifthen}
\RequirePackage{keyval}
\RequirePackage{xstring}
\RequirePackage{xspace}

\newif\ifregulatory@markdown
\newif\ifregulatory@bibtogls
\newif\ifregulatory@legacy
\newif\ifregulatory@alldefs
\pgfkeys{
    /regulatory/.is family,
    /regulatory,
    md/.is if=regulatory@markdown,
    md=false,
    bib2gls/.is if=regulatory@bibtogls,
    bib2gls=true,
    legacy/.is if=regulatory@legacy,
    legacy=false,
    alldefs/.is if=regulatory@alldefs,
    alldefs=false
}

\InputIfFileExists{regulatory.cfg}{%
    \PackageInfo{regulatory}{Using local configuration file}%
}{%
    \PackageInfo{regulatory}{No local configuration file}%
}

\newboolean{regulatory@nameinlink}
\setboolean{regulatory@nameinlink}{true}

\ProcessPgfPackageOptions{/regulatory}

% Markdown support
\ifregulatory@markdown
\RequirePackage[hashEnumerators,definitionLists,hybrid,headerAttributes=true]{markdown}
\setkeys{markdownRenderers}{
headingOne={\article{#1}},
olItemWithNumber={\item{}},
olBeginTight={\begin{paras}},
olEndTight={\end{paras}},
olBegin={\begin{paras}},
olEnd={\end{paras}},
dlBeginTight={\begin{labeling}{\@glswidestname}},
dlEndTight={\end{labeling}},
dlBegin={\begin{labeling}{\@glswidestname}},
dlEnd={\end{labeling}}%
\renewcommand\markdownRendererOlItemWithNumber[1]{\item{}\label{ml:##1}}
}
\fi

% Pakketten ná markdown (anders gaat enumitem kapot)
\RequirePackage{xcolor}
\RequirePackage{xassoccnt}
\RequirePackage{xpatch}
\RequirePackage{titlesec}
\RequirePackage{enumitem}
\RequirePackage{fmtcount}
\RequirePackage{scrextend}
\RequirePackage{xr-hyper}
\RequirePackage[user,counter,hyperref,xr]{zref}
\RequirePackage{zref-xr,zref-user,zref-clever,zref-hyperref}
\RequirePackage{hyperref}
\RequirePackage{pgf}
\RequirePackage[sanitize=none,nopostdot]{glossaries}
\ifregulatory@bibtogls
\RequirePackage[record,style=alttree]{glossaries-extra}
\else
\PackageWarning{regulatory}{Using glossaries for definitions without bib2gls. This is mostly untested.}
\fi

\RequirePackage{attachfile2}
\RequirePackage{translations}

\newboolean{regulatory@defslisted}
\setboolean{regulatory@defslisted}{false}

% Kleuren
\providecolor{artlabel}{rgb}{0,0,0}
\providecolor{arttitle}{rgb}{0,0,0}
\providecolor{paralabel}{rgb}{0,0,0}
\providecolor{paratitle}{rgb}{0,0,0}
\providecolor{sublabel}{rgb}{0,0,0}
\providecolor{subtitle}{rgb}{0,0,0}



% Artikelen
\newcounter{article}
\titleclass{\article}[0]{straight}
\titleformat{\article}{\normalfont\color{arttitle}}{\bfseries\color{artlabel}Artikel \thearticle.\quad}{1em}{}
\titlespacing*{\article}{0pt}{3.5ex plus 1ex minus .2ex}{5pt}

\newcounter{para}
\titleclass{\para}{straight}[\article]
\titleformat{\para}{\normalfont\color{paratitle}}{\normalfont\color{paralabel}\thearticle.\thepara.\quad}{1em}{}
\titlespacing*{\para}{0pt}{3.5ex plus 1ex minus .2ex}{5pt}
\counterwithin{para}{article}

\renewcommand{\thearticle}{\arabic{article}}
\renewcommand{\thepara}{\arabic{para}}
\makeatletter
\newcommand{\l@article}{\@dottedtocline{1}{1.5em}{2.3em}}
\def\toclevel@article{2}
\newcommand{\l@para}{\@dottedtocline{1}{3.8em}{2.3em}}
\def\toclevel@para{3}

\newcounter{provisions}
\newlist{provisions}{enumerate}{1}
\setlist[provisions,1]{label=\arabic*.,align=left,itemsep=0pt}
\counterwithin{provisions}{section}

\newlist{paras}{enumerate}{2}
\setlist[paras,1]{label=\thearticle.\arabic*.,ref=\arabic*,align=left,itemsep=0pt}
\setlist[paras,2]{label=\abalphnum{\arabic*}),ref=\arabic*,itemsep=0pt}
\counterwithin{parasi}{article}
\counterwithin{parasii}{parasi}

\zref@newprop{article}[-1]{\number\value{article}}
\zref@newprop{para}[-1]{\ifnum\value{para}>0\number\value{para}\else\number\value{parasi}\fi}
\zref@newprop{sub}[-1]{\number\value{parasii}}
\zref@addprops{main}{article,para,sub}

\DeclareTranslation{dutch}{article}{artikel}
\DeclareTranslation{dutch}{paragraph}{lid}
\DeclareTranslation{dutch}{subparagraph}{onderdeel}
\DeclareTranslation{dutch}{Article}{Artikel}
\DeclareTranslation{dutch}{Paragraph}{Lid}
\DeclareTranslation{dutch}{Subparagraph}{Onderdeel}
\DeclareTranslation{dutch}{to}{tot}
\DeclareTranslation{dutch}{Definitions}{Definities}
\DeclareTranslation{dutch}{of the}{van de}

\zcRefTypeSetup{article}{%
    Name-sg = \GetTranslation{Article},
    name-sg = \GetTranslation{article},
    Name-pl = Artikelen,
    name-pl = artikelen
}
\zcRefTypeSetup{para}{%
    Name-sg = \GetTranslation{Paragraph},
    name-sg = \GetTranslation{paragraph},
    Name-pl = Leden,
    name-pl = leden
}
\zcRefTypeSetup{parasi}{%
    Name-sg = \GetTranslation{Paragraph},
    name-sg = \GetTranslation{paragraph},
    Name-pl = Leden,
    name-pl = leden
}
\zcRefTypeSetup{parasii}{%
    Name-sg = \GetTranslation{Subparagraph},
    name-sg = \GetTranslation{subparagraph},
    Name-pl = Onderdelen,
    name-pl = onderdelen
}

\makeatother
{\catcode`\#=12 \gdef\hashchar{#}}
\makeatletter
\def\rref@linkpr{\href{\rref@current@fullurl}}

\newboolean{rref@cap}
\def\@ifrrefcap#1#2{%
    \ifthenelse{\boolean{rref@cap}}{%
        #1%
        \ifthenelse{\equal{#1}{}}{}{%
            \setboolean{rref@cap}{false}%
        }%
    }{%
        #2%
    }%
}
\def\@ifnameinlink#1#2{%
    \ifthenelse{\boolean{regulatory@nameinlink}}{#1}{#2}%
}

\newcommand\rref@middleconjunction{, }
\newcommand\rref@lastconjunction{ \GetTranslation{and} }
\newcommand\rref@rangeconjunction{ \GetTranslation{to} }

\newcommand\setmiddleconjunction[1]{\renewcommand\rref@middleconjunction{#1}}
\newcommand\setlastconjunction[1]{\renewcommand\rref@lastconjunction{#1}}
\newcommand\setrangeconjunction[1]{\renewcommand\rref@rangeconjunction{#1}}

\newcommand\setconjunction[3]{%
    \setmiddleconjunction{#1}%
    \setlastconjunction{#1}%
    \setrangeconjunction{#1}%
}

\newcommand\setjuncto{%
\setlastconjunction{ jo.\ }%
}

\newcommand\unsetjuncto{%
\setlastconjunction{ \GetTranslation{and} }%
}

\ifregulatory@legacy
\setjuncto
\fi

\newcommand\rref@refformat@noop[1]{#1}
\newcommand\rref@refformat@parenthesis[1]{(#1)}
\newcommand\rref@refformat@ordinal[1]{\@ifrrefcap{\Ordinalstringnum{#1}}{\ordinalstringnum{#1}}}
\newcommand\rref@refformat@alpha[1]{\@ifrrefcap{\ABAlphnum{#1}}{\abalphnum{#1}}}
\newcommand\rref@label@prefix[2]{#1 #2}
\newcommand\rref@label@postfix[2]{#2 #1}
\newcommand\rref@label@refonly[2]{\@gobble{#1}#2}
\newcommand\rref@group@braced[1]{{[}#1{]~}}

\newcommand\rref@init@lang@article[1]{%
    \pgfkeys{/rref/#1/article/.is family,
        /rref/#1/article,
        name/.initial=\GetTranslation{article},
        Name/.initial=\GetTranslation{Article},
        ref format/.initial=\rref@refformat@noop,
        label format/.initial=\rref@label@prefix,
        group conjunction/.initial={},
        group format/.initial=\rref@refformat@noop
    }
}
\newcommand\rref@init@lang@para[1]{%
    \pgfkeys{/rref/#1/para/.is family,
        /rref/#1/para,
        name/.initial=\GetTranslation{paragraph},
        Name/.initial=\GetTranslation{Paragraph},
        ref format/.initial=\rref@refformat@parenthesis,
        label format/.initial=\rref@label@refonly,
        group conjunction/.initial={,},
        group format/.initial=\rref@refformat@noop
    }
}
\newcommand\rref@init@lang@sub[1]{%
    \pgfkeys{/rref/#1/sub/.is family,
        /rref/#1/sub,
        name/.initial=\GetTranslation{subparagraph},
        Name/.initial=\GetTranslation{Subparagraph},
        ref format/.initial=\rref@refformat@ordinal,
        label format/.initial=\rref@label@postfix,
        group conjunction/.initial={,},
        group format/.initial=\rref@group@braced
    }
}

\newcommand\rref@setup@lang[3]{%
    \pgfkeys{/rref/#1/#2,#3}
}

\newcommand\rref@setup[4]{%
    \rref@init@lang@article{#1}
    \rref@init@lang@para{#1}
    \rref@init@lang@sub{#1}
    \rref@setup@lang{#1}{article}{#2}
    \rref@setup@lang{#1}{para}{#3}
    \rref@setup@lang{#1}{sub}{#4}
}

% The defaults are already based on English, so no arguments are given.
\rref@setup{english}{}{}{}

\rref@setup{dutch}{
    group conjunction={,~}
}{
    ref format=\rref@refformat@ordinal,
    label format=\rref@label@postfix,
    group conjunction={,~},
    group format=\rref@refformat@noop
}{
    ref format=\rref@refformat@alpha,
    label format=\rref@label@prefix,
    group conjunction={,~},
    group format=\rref@refformat@noop
}%

\newcommand\rref@set[4][\languagename]{%
    \pgfkeysgetvalue{/rref/#1/#2/#3}{#4}%
}

\newcommand\rref@get[3][\languagename]{%
    \pgfkeysvalueof{/rref/#1/#2/#3}%
}

\newcommand\rref@name[2][\languagename]{\@ifrrefcap{\rref@get[#1]{#2}{Name}}{\rref@get[#1]{#2}{name}}}
\def\rref@article@name{\rref@name{article}}
\def\rref@para@name{\rref@name{para}}
\def\rref@sub@name{\rref@name{sub}}

\newcommand\rref@refformat[2][\rref@current@type]{\rref@set{#1}{ref format}{\@@rrefformat}\@@rrefformat{#2}}
\newcommand\rref@labelformat[2][\rref@current@type]{\rref@set{#1}{label format}{\@@labelformat}\@@labelformat{\rref@name{#1}}{#2}}
\newcommand\rref@linkformat[2][\rref@current@type]{%
    \@ifnameinlink{%
        \rref@linkpr{\rref@labelformat[#1]{\rref@refformat[#1]{#2}}}%
    }{%
        \rref@labelformat[#1]{\rref@linkpr{\rref@refformat[#1]{#2}}}%
    }%
}
\newcommand\rref@reflink[2][\rref@current@type]{%
    \rref@linkpr{\rref@refformat[#1]{#2}}%
}
\newcommand\rref@groupformat[2][\rref@current@type]{%
    \rref@set{#1}{group format}{\@@groupformat}\@@groupformat{#2}%
}

\def\rref{\@ifstar\rref@star\rref@nostar}
\def\rref@star#1{%
    \rref@setcurrent{#1}%
    \ifnum\rref@current@value>0%
        \rref@refformat{\rref@current@value}%
    \else%
        \zref{#1}%
    \fi%
}
\def\rref@nostar#1{%
    \rref@setcurrent{#1}%
    \ifnum\rref@current@value>0%
        \rref@linkpr{\rref@refformat{\rref@current@value}}%
    \else%
        \zcref[noname]{#1}%
    \fi%
}
\def\Rref{\@ifstar\Rref@star\Rref@nostar}
\def\Rref@star#1{%
    \setboolean{rref@cap}{true}%
    \rref*{#1}%
}
\def\Rref@nostar#1{%
    \setboolean{rref@cap}{true}%
    \rref{#1}%
}

\def\nref{\@ifstar\nref@star\nref@nostar}
\def\nref@star#1{%
    \rref@setcurrent{#1}%
    \ifnum\rref@current@value>0%
        \rref@labelformat{\rref@refformat{\rref@current@value}}%
    \else%
        \zcref*{#1}%
    \fi%
}
\def\nref@nostar#1{%
    \rref@setcurrent{#1}%
    \ifnum\rref@current@value>0%
        \rref@linkformat{\rref@current@value}%
    \else%
        \zcref{#1}%
    \fi%
}
\def\Nref{\@ifstar\Nref@star\Nref@nostar}
\def\Nref@star#1{%
    \setboolean{rref@cap}{true}%
    \nref*{#1}%
}
\def\Nref@nostar#1{%
    \setboolean{rref@cap}{true}%
    \nref{#1}%
}

\def\rref@setcurrent#1{%
    \def\rref@current{#1}%
    \def\rref@current@counter{\zref@extract{#1}{counter}}%
    \def\rref@current@article{\zref@extract{#1}{article}}%
    \def\rref@current@para{\zref@extract{#1}{para}}%
    \def\rref@current@sub{\zref@extract{#1}{sub}}%
    \rref@settype%
    \rref@setlink%
}
\def\rref@settype{%
    \ifthenelse{\equal{article}{\rref@current@counter}}{%
        \def\rref@current@type{article}%
        \def\rref@current@value{\rref@current@article}%
        \def\rref@current@fullformat{}%
    }{%
        \ifthenelse{\equal{para}{\rref@current@counter}\or\equal{parasi}{\rref@current@counter}}{%
            \def\rref@current@type{para}%
            \def\rref@current@value{\rref@current@para}%
        }{%
            \ifthenelse{\equal{parasii}{\rref@current@counter}}{%
                \def\rref@current@type{sub}%
                \def\rref@current@value{\rref@current@sub}%
            }{%
                \def\rref@current@type{}%
                \def\rref@current@value{-1}%
            }%
        }%
    }%
}
\def\rref@setlink{%
    \def\rref@current@anchor{\zref@extract{\rref@current}{anchor}}%
    \def\rref@current@url{\zref@extractdefault{\rref@current}{url}{}}%
    \def\rref@current@fullurl{\rref@current@url\hashchar\rref@current@anchor}%
}
\newcommand\rref@number[1]{%
    \zref@extractdefault{#1}{\rref@current@type}{-1}%
}

\newcommand\aref@setcurrent[1]{%
    \def\aref@current@reflink{\rref@reflink[\rref@current@type]{\rref@current@value}}%
    \zref@ifrefcontainsprop{#1}{externaldocument}{%
        \filename@parse{\expanded{\zref@extract{#1}{externaldocument}}}%
        \edef\@aref@filelabel{\expanded{\filename@base}}%
        \def\@aref@postlink{%
            , \documentlabel{\@aref@filelabel}%
            \ifthenelse{\equal{\artifactfootnote{\@aref@filelabel}}{true}\and\equal{\artifactreferred{\@aref@filelabel}}{false}}{%
                \documentfootnote{\@aref@filelabel}%
            }{}%
        }%
    }{%
        \def\@aref@postlink{}%
    }%
    \ifthenelse{\equal{\rref@current@type}{article}}{%
        \def\@aref@prefix{}%
        \def\@aref@postfix{}%
    }{%
        \ifthenelse{\equal{\rref@current@type}{para}}{%
            \def\@aref@prefix{%
                \rref@labelformat[article]{\rref@current@article}\rref@get{article}{group conjunction}%
            }%
            \def\@aref@postfix{}%
        }{%
            \ifthenelse{\equal{\rref@current@type}{sub}}{%
                \def\@aref@prefix{%
                    \rref@labelformat[article]{\rref@refformat[article]{\rref@current@article}}\rref@get{article}{group conjunction}%
                    \rref@labelformat[para]{\rref@refformat[para]{\rref@current@para}}\rref@get{para}{group conjunction}%
                }%
                \def\@aref@postfix{}%
            }{%
                \def\@aref@prefix{}%
                \def\@aref@postfix{}%
            }%
        }%
    }%
}

\newcommand{\aref}[1]{%
    \def\@@arefsetup##1##2{%
        \rref@setcurrent{##1}%
        \aref@setcurrent{##1}%
        \rref@groupformat{\@aref@prefix}%
        ##2%
        \@aref@postfix\@aref@postlink%
    }%
    \IfSubStr{#1}{,}{%
        \StrBefore[1]{#1}{,}[\firstarg]%
        \@@arefsetup{\firstarg}{\rref@labelformat{\aref@group{#1}}}%
    }{%
        \@@arefsetup{#1}{\nref{#1}}%
    }%
}

\newcommand{\Aref}[1]{%
    \setboolean{rref@cap}{true}%
    \aref{#1}%
}

\def\listterminator{-1}

\newcommand\aref@group[1]{%
    \bubblesort{#1}%
    \expandafter\begincompaction\sortedlist,\listterminator,\relax%
}
\def\begincompaction#1,#2\relax{%
    \def\startlist{#1}%
    \def\currentendlist{#1}%
    \findendlist#2\relax%
}
\def\findendlist#1,#2\relax{%
    \ifnum\numexpr\rref@number{\currentendlist}+1\relax=\rref@number{#1}\relax%
        \def\currentendlist{#1}%
        \findendlist#2\relax%
    \else%
        \ifnum\rref@number{\startlist}=\rref@number{\currentendlist}\relax%
            \ignorespaces\rref{\startlist}\unskip%
        \else%
            \ifnum\numexpr\rref@number{\startlist}+1\relax=\rref@number{\currentendlist}\relax%
                \ignorespaces\rref{\startlist}\unskip%
                \ifnum\rref@number{#1}=\listterminator\relax%
                    \rref@lastconjunction%
                \else%
                    \rref@middleconjunction%
                \fi\ignorespaces%
                \rref{\currentendlist}\unskip%
            \else%
                \ignorespaces\rref{\startlist}\unskip\rref@rangeconjunction\ignorespaces%
                \rref{\currentendlist}\unskip%
            \fi%
        \fi%
        \ifnum\rref@number{#1}=\listterminator\else%
            \IfStrEq{#2}{\listterminator,}{\rref@lastconjunction}{\rref@middleconjunction}\begincompaction#1,#2\relax%
        \fi%
    \fi%
}
\newcommand\bubblesort[1]{\def\sortedlist{}\sortlist#1,\listterminator,\relax}
\def\sortlist#1,#2,#3\relax{%
    \rref@setcurrent{#1}%
    \aref@setcurrent{#1}%
    \ifnum\rref@number{#2}=\listterminator\relax%
        \edef\sortedlist{\sortedlist#1}%
    \else%
        \ifnum\rref@number{#1}<\rref@number{#2}\relax%
            \edef\sortedlist{\sortedlist#1,}%
            \sortlist#2,#3\relax%
        \else%
            \let\tmp\sortedlist%
            \def\sortedlist{}%
            \expandafter\sortlist\tmp#2,#1,#3\relax%
        \fi%
    \fi%
}

\GetAllResetLists
\RegisterPostLabelHook{\zlabel}

\newglossary*{definitions}{Definitions}
\newglossary*{externals}{Externals}

\newcommand\artifact@footnote@noop[1]{#1}
\newcommand\artifact@def@label@default[1]{~(\GetTranslation{see} #1)}

\newcommand\newartifact[2]{%
    \pgfkeys{
        /artifact/#1/.is family,
        /artifact/#1,
        name/.initial = #1,
        filename/.initial = #1.pdf,
        path/.initial = ./,
        ref label/.initial=,
        def label/.initial=\artifact@def@label@default,
        footnote/.initial = true,
        footnote label/.initial=\artifact@footnote@noop,
        url/.initial=,
        referred/.initial = false,
        defs/.initial=,
        author/.initial=<author>,
        subject/.initial=<subject>,
        description/.initial=<description>
    }
    \pgfkeys{/artifact/#1,#2}
}
\newcommand\@aget[2]{\pgfkeysvalueof{/artifact/#1/#2}}
\newcommand\artifact@setfootnotelabel[1]{\pgfkeysgetvalue{/artifact/#1/footnote label}{\artifactfootnotelabel}}
\newcommand\artifactname[1]{\@aget{#1}{name}}
\newcommand\artifactfullname[1]{\@aget{#1}{path}\@aget{#1}{name}}
\newcommand\artifactfilename[1]{\@aget{#1}{filename}}
\newcommand\artifactfullfilename[1]{\@aget{#1}{path}\@aget{#1}{filename}}
\newcommand\artifactfootnote[1]{\@aget{#1}{footnote}}
\newcommand\artifacturl[1]{\@aget{#1}{url}}
\newcommand\artifactreferred[1]{\@aget{#1}{referred}}
\newcommand\artifactdefs[1]{\@aget{#1}{defs}}
\newcommand\artifactauthor[1]{\@aget{#1}{author}}
\newcommand\artifactsubject[1]{\@aget{#1}{subject}}
\newcommand\artifactdescription[1]{\@aget{#1}{description}}

\renewcommand{\glossarysection}[2][]{\@gobble{#1}\@gobble{#2}}%
\setglossarypreamble[definitions]{%
    \ifnum\value{article}=0%
        \article{\GetTranslation{Definitions}}\label{art:definitions}%
        \vspace*{-\baselineskip}%
    \else%
        \ifnum\value{parasi}=0%
            \vspace*{-\baselineskip}%
        \fi%
    \fi%
}

\newcommand\describe[1]{\setboolean{regulatory@defslisted}{true}\glstarget{#1}{\glsentrydesc{#1}}}
\newcommand\printdefs[1]{%
    \renewcommand*{\glsnamefont}[1]{\textmd{##1}}%
    \setboolean{regulatory@defslisted}{true}%
    \glssetwidest{#1}%
    \ifnum\value{parasi}>0%
        \def\@@wrapper##1{\begin{minipage}{\linewidth}##1\end{minipage}}%
    \else%
        \def\@@wrapper##1{##1}%
    \fi%
    \@@wrapper{%
        \ifregulatory@bibtogls%
            \printunsrtglossary[type=definitions,style=alttree,title={},nonumberlist=true]%
        \else%
            \printglossary[type=definitions,style=alttree,title={},nonumberlist=true]%
        \fi%
    }%
}

\attachfilesetup{color=black}

\newcommand\documentattachment[2]{%
    \def\@tmpfile{\artifactfullfilename{#1}}%
    \IfFileExists{\@tmpfile}{%
    \textattachfile[description={\artifactdescription{#1}},subject={\artifactsubject{#1}},author={\artifactauthor{#1}}]{\@tmpfile}{#2}}%
    {\texttt{#2}}%
}

\newcommand\documentlabel[1]{%
    \def\@@label{\@aget{#1}{ref label}}%
    \ifthenelse{\equal{\@@label}{}}{%
        \GetTranslation{of the} \artifactsubject{#1}%
    }{\@@label}%
}

\newcommand\documentfootnote[2][]{%
    \pgfkeysifdefined{/artifact/#2/referred}{%
        \pgfkeys{/artifact/#2,referred=true}%
        \artifact@setfootnotelabel{#2}%
        \ifthenelse{\equal{#1}{}}{%
            \def\artifact@description{\artifactsubject{#2}}%
        }{%
            \def\artifact@description{#1}%
        }%
        \footnote{\artifactfootnotelabel{\documentattachment{#2}{\artifact@description}}}%
    }{\PackageWarning{regulatory}{Artifact does not exists '#2'}\footnote{DOCUMENT NOT FOUND '#2'}}
}

\edef\@regulatory@bib@selection{}
\ifregulatory@alldefs
\edef\@regulatory@bib@selection{,selection=all}
\fi

\newcommand\loadglsdefs[1]{%
    \setboolean{regulatory@defslisted}{true}%
    \ifregulatory@bibtogls%
        \GlsXtrLoadResources[type=definitions,src={#1},sort={nl-NL},category={defs}\@regulatory@bib@selection]%
    \else%
        \loadglsentries[definitions]{#1}%
    \fi%
}

\newcommand\loadextdefs[2][externals]{%
    \ifregulatory@bibtogls%
        \GlsXtrLoadResources[type=externals,src={#2},sort={nl-NL},category={#1}]%
    \else%}{%
        \loadglsentries[externals]{#2}%
    \fi%}%
    \glsdefpostlink{#1}{%
        \ifthenelse{\equal{\artifactreferred{#1}}{true}}{}{%
            \@aget{#1}{def label}{%
                \artifactsubject{#1}%
                \documentfootnote{#1}%
            }%
        }%
    }%
}

\newcommand\refdocument[3][ext-]{%
    \newartifact{#2}{footnote=false,#3}%
    \zexternaldocument[#1]{\artifactfullname{#2}}[#2]%
}

\newcommand\masterdocument[3][ext-]{%
    \newartifact{#2}{#3}%
    \zexternaldocument[#1]{\artifactfullname{#2}}[#2]%
    \ifthenelse{\equal{\string\artifactdefs{#2}}{}}{%
        \PackageWarning{regulatory}{No definitions given to the master document. \the\gls commands will therefore not work.}%
    }{%
        \PackageWarning{regulatory}{Agreement file loads #1, #2, #3.}
        \loadextdefs[#2]{\artifactdefs{#2}}%
        \ifregulatory@bibtogls%
            \glssetcategoryattribute{#2}{targeturl}{\artifactfilename{#2}}%
            \glssetcategoryattribute{#2}{targetname}{\glolinkprefix\glslabel}%
        \fi%
    }%
}

\AtEndPreamble{%
\ifregulatory@bibtogls\else%
\ifthenelse{\boolean{regulatory@defslisted}}{\makeglossaries}{}%
\fi%
}
\AtEndDocument{%
\ifregulatory@alldefs\glsaddallunused\fi%
}
