%%%
% R\'eponses \`a relier
%%%
\def\filedateQRelier{2025/05/28}%
\def\fileversionQRelier{0.1a}%
\message{-- \filedateQRelier\space v\fileversionQRelier}%
%
\setKVdefault[ClesRelie]{Solution=false,LargeurG=5cm,LargeurD=2cm,Stretch=1.5,Ecart=2cm,Couleur=black,Graine={}}%
\defKV[ClesRelie]{Graine=\ifempty{#1}{}{\PfCGraineAlea{#1}}}%

\newcounter{NbRelie}%
\newcounter{NbRelieD}

\NewDocumentCommand\Relie{som}{%
  \useKVdefault[ClesRelie]%
  \setKV[ClesRelie]{#2}%
  \colorlet{PfCRelieCouleurTrace}{\useKV[ClesRelie]{Couleur}}%
  \setsepchar[*]{,*/}\reademptyitems%
  \readlist*\ListeRelie{#3}%
  \ignoreemptyitems%
  \IfBooleanTF{#1}{%
    \buildtabrelieauto%
    \par
    \ifboolKV[ClesRelie]{Solution}{%
      \xintFor* ##1 in {\xintSeq {1}{\ListeRelielen}}\do{%
        \itemtomacro\PfCListeReponsesMelangees[##1]\PfCNumReponses%
        \itemtomacro\ListeRelie[\PfCNumReponses,1]\untest%
        % Le test est \untest\\
        \ifx\bla\untest\bla%
        \else%
          \tikz[remember picture,overlay]{\draw[PfCRelieCouleurTrace] (RelieG-\PfCNumReponses) -- (RelieD-##1);}%
        \fi%
      }%
    }{%
    }%
  }{%
    \buildtabrelie%
    \ifboolKV[ClesRelie]{Solution}{%
      \xintFor* ##1 in {\xintSeq {1}{\ListeRelielen}}\do{%
        \itemtomacro\ListeRelie[##1,1]\untest%
        \ifx\bla\untest\bla%
        \else%
          \itemtomacro\ListeRelie[##1,3]\Foo%
          \setsepchar{-}\ignoreemptyitems%
          \readlist*\ListeRelieReponses{\Foo}%
          \reademptyitems%
          \foreachitem\compteur\in\ListeRelieReponses{%
            \tikz[remember picture,overlay]{\draw[PfCRelieCouleurTrace] (RelieG-##1) -- (RelieD-\ListeRelieReponses[\compteurcnt]);}%
            }%
        \fi%
      }%
    }{%
    }%
  }%
}%

\def\buildtabrelie{%
  \setcounter{NbRelie}{0}%
  \setcounter{NbRelieD}{0}%
  \renewcommand{\arraystretch}{\useKV[ClesRelie]{Stretch}}%
  \begin{tabular}{p{\useKV[ClesRelie]{LargeurG}}cp{\useKV[ClesRelie]{Ecart}}cp{\useKV[ClesRelie]{LargeurD}}}%
    \xintFor* ##1 in {\xintSeq {1}{\ListeRelielen}}\do{%
    \ListeRelie[##1,1]\itemtomacro\ListeRelie[##1,1]\untest%
    \ifx\bla\untest\bla%                                     
    \uppercase{&}\stepcounter{NbRelie}%
      \else%
      \uppercase{&}\stepcounter{NbRelie}\tikz[remember
                   picture,overlay]{\node[name=RelieG-\theNbRelie,inner
                   sep=0pt]{};\fill[]
                   (RelieG-\theNbRelie) circle[radius=1.5pt];}
                   \fi&&
                         \itemtomacro\ListeRelie[##1,2]\untest
                         \ifx\bla\untest\bla\else\stepcounter{NbRelieD}\tikz[remember
                         picture]{\node[name=RelieD-\theNbRelieD,inner
                         sep=0pt]{};\fill[] (RelieD-\theNbRelieD) circle[radius=1.5pt]}
                         \fi
    &\itemtomacro\ListeRelie[##1,2]\untest%
                          \ifx\bla\untest\bla
                          \else
                          \ListeRelie[##1,2]
                          \fi\\}%
  \end{tabular}%
  %\setcounter{NbRelie}{0}%
  %\setcounter{NbRelieD}{0}%
}%

\NewDocumentCommand\buildtabrelieauto{}{%
  \setcounter{NbRelie}{0}%
  \setcounter{NbRelieD}{0}%
  \xdef\PfCFooListeNbQ{1}%
  \xintFor* ##1 in{\xintSeq{2}{\ListeRelielen}}\do{%
    \xdef\PfCFooListeNbQ{\PfCFooListeNbQ,##1}%
  }%
  \setsepchar{,}\ignoreemptyitems%
  \MelangeListe{\PfCFooListeNbQ}{\ListeRelielen}%
  \setsepchar{,}\ignoreemptyitems%
  \readlist*\PfCListeReponsesMelangees{\faa}%
  \renewcommand{\arraystretch}{\useKV[ClesRelie]{Stretch}}%
  \begin{tabular}{p{\useKV[ClesRelie]{LargeurG}}cp{\useKV[ClesRelie]{Ecart}}>{\tikz[remember
          picture]{\node[name=RelieD-\theNbRelie,inner
            sep=0pt]{};\fill[] (RelieD-\theNbRelie) circle[radius=1.5pt]}}cp{\useKV[ClesRelie]{LargeurD}}}%
    \xintFor* ##1 in {\xintSeq {1}{\ListeRelielen}}\do{%
      {\ListeRelie[##1,1]}\itemtomacro\ListeRelie[##1,1]\untest%
      \ifx\bla\untest\bla%                                     
      \uppercase{&}\stepcounter{NbRelie}\tikz[remember
        picture,overlay]{\node[name=RelieG-\theNbRelie,inner
          sep=0pt]{};}%
      \else%
      \uppercase{&}\stepcounter{NbRelie}\tikz[remember
        picture,overlay]{\node[name=RelieG-\theNbRelie,inner
          sep=0pt]{};\fill[]
        (RelieG-\theNbRelie) circle[radius=1.5pt];}%
                   \fi&&&\itemtomacro\PfCListeReponsesMelangees[##1]\PfCNumReponses{\ListeRelie[\PfCNumReponses,2]}\\%
    }%
  \end{tabular}%
  \setcounter{NbRelie}{0}%
}%

