%D \module
%D   [       file=mult-chk,
%D        version=2009.04.13,
%D          title=\CONTEXT\ Multilingual Macros,
%D       subtitle=Checking,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Multilingual Macros / Checking}

%D This is very experimental code that eventually might be used
%D once we have split the whole code base.

%D \startbuffer
%D     \getcheckedparameters[MyTest][MyNamespace][a=1,b=2,c=3,d=4,crap=whatever]
%D
%D     \MyNamespacea\quad
%D     \MyNamespaceb\quad
%D     \MyNamespacec\quad
%D     \MyNamespaced\quad
%D     crap is \ifdefined\MyNamespacecrap\else un\fi defined
%D \stopbuffer
%D
%D \enablecheckparameters
%D
%D \setvalidparameterkeys[MyTest][a,b,c,d] \getbuffer \par
%D \addvalidparameterkeys[MyTest][crap]    \getbuffer \par

\unprotect

\registerctxluafile{mult-chk}{}

\unexpanded\def\setvalidparameterkeys{\dodoubleargument\mult_checkers_set_valid_parameter_keys}
\unexpanded\def\addvalidparameterkeys{\dodoubleargument\mult_checkers_add_valid_parameter_keys}

\def\mult_checkers_set_valid_parameter_keys[#1][#2]{\clf_setvalidinterfacekeys{#1}{#2}}
\def\mult_checkers_add_valid_parameter_keys[#1][#2]{\clf_addvalidinterfacekeys{#1}{#2}}

\def\mult_checkers_get_checked_parameters_yes[#1]#2[#3]#4[#5%
  {\if\noexpand#5]%
     \expandafter\gobblethreearguments
   \else
     \let\setsomevalue\dosetvalue % used at the lua end
     \expandafter\mult_checkers_get_checked_parameters_yes_indeed
   \fi{#1}{#3}#5}

\def\mult_checkers_get_checked_parameters_yes_indeed#1#2#3]%
  {\clf_getcheckedinterfaceparameters{#1}{#2}{\detokenize{#3}}}

\def\mult_checkers_get_checked_parameters_nop[#1]#2[#3]#4[#5%
  {\if\noexpand#5]%
     \expandafter\gobbletwoarguments
   \else
     \let\setsomevalue\dosetvalue % used at the lua end
     \expandafter\mult_checkers_get_checked_parameters_nop_indeed
   \fi{#3}#5}

\def\mult_checkers_get_checked_parameters_nop_indeed#1#2]% needs checking with adapted syst-aux.mkiv
  {\def\syst_helpers_get_parameters_assign{\syst_helpers_get_parameters_assign_indeed#1}% will change
   \syst_helpers_process_comma_item#2,],^^^^0004}

\unexpanded\def\disablecheckparameters{\let\getcheckedparameters\mult_checkers_get_checked_parameters_nop}
\unexpanded\def\enablecheckparameters {\let\getcheckedparameters\mult_checkers_get_checked_parameters_yes}

\disablecheckparameters

\protect \endinput

\starttext

\testfeatureonce{10000}{\getcheckedparameters[test][xx][a=b,c= d, e = f]} % 0.20 seconds

\enablecheckparameters

\testfeatureonce{10000}{\getcheckedparameters[test][xx][a=b,c= d, e = f]} % 0.35 seconds

\getcheckedparameters[test][xx][a=a]
\getcheckedparameters[test][xx][b= b]
\getcheckedparameters[test][xx][c = c]
\getcheckedparameters[test][xx][d = d d , e = e ,f = f ]
\getcheckedparameters[test][xx][g={oeps {oeps}}]
\getcheckedparameters[test][xx][crap=whatever]

\startlines
[a:\getvalue{xxa}][a]
[b:\getvalue{xxb}][b]
[c:\getvalue{xxc}][c]
[d:\getvalue{xxd}][d d ]
[e:\getvalue{xxe}][e ]
[f:\getvalue{xxf}][f ]
[g:\getvalue{xxg}][\detokenize\expandafter{\xxg}]
\stoplines

\setvalidparameterkeys[test][crap]

\getcheckedparameters[test][xx][crap=whatever]

\stoptext
