% !Mode:: "TeX:UTF-8:Main"
% !Mode:: "TeX:UTF-8:Main"
\DocumentMetadata{testphase={phase-III,firstaid}} 

% # store and restore section command:
\makeatletter
\let\ori@startsection\@startsection 
\let\ori@xsect\@xsect 
\let\ori@sect\@sect 
\let\ori@ssect\@ssect 
\documentclass[]{acmart}
\let\@startsection\ori@startsection %acmart definition is like kernel, so tagging version should be ok
\let\@xsect\ori@xsect %acmart definition is like kernel, so tagging version should be ok
% everyone patches \@sect and @ssect, here we should change the internal nameref version ;-(
\let\NR@ssect\ori@ssect %acmart definition is like kernel, so tagging version should be ok
\let\NR@sect\ori@sect %TODO: must be checked! acmart definition is slightly different.


% part is level 9??? It must be checked if this confuse the new tagging code.
\renewcommand\part{\@startsection{part}{9}{\z@}%
  {-10\p@ \@plus -4\p@ \@minus -2\p@}%
  {4\p@}%
  {\ACM@NRadjust\@parfont}}
\def\toclevel@part{9} %???

% # tableofcontents. Insert hooks into \tocline (I hope arguments + placement are correct)
\def\@tocline#1#2#3#4#5#6#7{\relax
  \ifnum #1>\c@tocdepth % then omit
  \else
    \par \addpenalty\@secpenalty\addvspace{#2}%
    \begingroup \hyphenpenalty\@M
    \@ifempty{#4}{%
      \@tempdima\csname r@tocindent\number#1\endcsname\relax
    }{%
      \@tempdima#4\relax
    }%
    \parindent\z@ \leftskip#3\relax \advance\leftskip\@tempdima\relax
    \rightskip\@pnumwidth plus4em \parfillskip-\@pnumwidth
    #5\leavevmode\hskip-\@tempdima
    \UseHookWithArguments{contentsline/text/before}{4}{#1}{#6}{#7}{\@contentsline@destination}%NEW 
    #6%
    \UseHookWithArguments{contentsline/text/after}{4}{#1}{#6}{#7}{\@contentsline@destination}%NEW    
    \nobreak\relax
    \hfil\hbox to\@pnumwidth{%
     \UseHookWithArguments{contentsline/page/before}{4}{#1}{#6}{#7}{\@contentsline@destination}%NEW
     \@tocpagenum{#7}
     \UseHookWithArguments{contentsline/page/after}{4}{#1}{#6}{#7}{\@contentsline@destination}%NEW
     }\par
    \nobreak
    \endgroup
  \fi}
% add config points to starttoc
% TODO: extend kernel version so that acmart no longer has to redefine it.
\def\@starttoc#1#2{\begingroup\makeatletter
  \setTrue{#1}%
  \par\removelastskip\vskip\z@skip
  \@startsection{section}\@M\z@{\linespacing\@plus\linespacing}%
    {.5\linespacing}{\centering\contentsnamefont}{#2}%
  \@starttoc@cfgpoint@before{#1}%NEW<<<<<<<<<< (name will change)
  \@input{\jobname.#1}%
  \@starttoc@cfgpoint@after{#1}%NEW<<<<<<<<<<<<  (name will change)
  \if@filesw
    \@xp\newwrite\csname tf@#1\endcsname
    \immediate\@xp\openout\csname tf@#1\endcsname \jobname.#1\relax
  \fi
  \global\@nobreakfalse \endgroup
  \addvspace{32\p@\@plus14\p@}%
}



% # maketitle is much too complex, so we stop all tagging for now
\ExplSyntaxOn
\AddToHook{cmd/maketitle/before}
 {\tagstructbegin{tag=Title}\tagmcbegin{} \tag_stop:}
\AddToHook{cmd/maketitle/after}
 {\tag_start:\tagmcend\tagstructend }
\ExplSyntaxOff

% # TODO
% \endminipage must be restored (acmart wants to remove the footnoterule.) 
% \@footnotetext is changed, but no in all variants

\title{title}

\usepackage{blindtext}

\begin{document}

\maketitle

\tableofcontents 

\section{section}
\subsection{sub}
\subsubsection{subsub}

\paragraph{paragraph} some text\footnote{xxxx}

\subsection{sub}
\subsubsection{subsub}

\paragraph{paragraph} some text

\begin{figure}
a figure
\caption{test caption figure}
\end{figure}

\begin{table}
a table
\caption{test caption table}
\end{table}

\blinddocument

\end{document}
