% \iffalse meta-comment
%
% Copyright (C) 2008-2014 by Ulrich M. Schwarz
% Copyright (C) 2019      by Frank Mittelbach
% Copyright (C) 2020-     by Yukai Chou
%
% This file may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, version 1.3c.
% The license can be obtained from
% http://www.latex-project.org/lppl/lppl-1-3c.txt
%
% \fi
%
%\iffalse (hide this from DocInput)
%<*autoref>
%\fi
%
% \pkg{hyperref}'s |\autoref| command does not work well with theorems that
% share a counter: it'll always think it's a Lemma even if it's a Remark
% that shares the Lemma counter. Load this package to fix it. No further
% intervention needed.
%
%\StopEventually{}
%    \begin{macrocode}

\RequirePackage{thm-patch, aliasctr, parseargs, keyval}

\let\@xa=\expandafter
\let\@nx=\noexpand

\newcommand\thmt@autorefsetup{%
  \@xa\def\csname\thmt@envname autorefname\@xa\endcsname\@xa{\thmt@thmname}%
  \ifthmt@hassibling
    \@counteralias{\thmt@envname}{\thmt@sibling}%
    \@xa\def\@xa\thmt@autoreffix\@xa{%
      \@xa\global\@xa\let\csname the\thmt@envname\@xa\endcsname
        \csname the\thmt@sibling\endcsname
      \def\thmt@autoreffix{}%
    }%
    \protected@edef\thmt@sibling{\thmt@envname}%
  \fi
}
\g@addto@macro\thmt@newtheorem@predefinition{\thmt@autorefsetup}%
\g@addto@macro\thmt@newtheorem@postdefinition{\csname thmt@autoreffix\endcsname}%

\def\thmt@refnamewithcomma #1#2#3,#4,#5\@nil{%
  \@xa\def\csname\thmt@envname #1utorefname\endcsname{#3}%
  \ifcsname #2refname\endcsname
    \csname #2refname\@xa\endcsname\@xa{\thmt@envname}{#3}{#4}%
  \fi
}
\define@key{thmdef}{refname}{\thmt@trytwice{}{%
  \thmt@refnamewithcomma{a}{c}#1,\textbf{?? (pl. #1)},\@nil
}}
\define@key{thmdef}{Refname}{\thmt@trytwice{}{%
  \thmt@refnamewithcomma{A}{C}#1,\textbf{?? (pl. #1)},\@nil
}}


\ifcsname Autoref\endcsname\else
\let\thmt@HyRef@testreftype\HyRef@testreftype
\def\HyRef@Testreftype#1.#2\\{%
  \ltx@IfUndefined{#1Autorefname}{%
    \thmt@HyRef@testreftype#1.#2\\%
  }{%
    \edef\HyRef@currentHtag{%
      \expandafter\noexpand\csname#1Autorefname\endcsname
      \noexpand~%
    }%
  }%
}


\let\thmt@HyPsd@@autorefname\HyPsd@@autorefname
\def\HyPsd@@Autorefname#1.#2\@nil{%
  \tracingall
  \ltx@IfUndefined{#1Autorefname}{%
    \thmt@HyPsd@@autorefname#1.#2\@nil
  }{%
    \csname#1Autorefname\endcsname\space
  }%
}%
\def\Autoref{%
  \parse{%
  {\parseFlag*{\def\thmt@autorefstar{*}}{\let\thmt@autorefstar\@empty}}%
  {\parseMand{%
    \bgroup
    \let\HyRef@testreftype\HyRef@Testreftype
    \let\HyPsd@@autorefname\HyPsd@@Autorefname
    \@xa\autoref\thmt@autorefstar{##1}%
    \egroup
    \let\@parsecmd\@empty
  }}%
  }%
}
\fi % ifcsname Autoref

% not entirely appropriate here, but close enough: 
\AtBeginDocument{%
  \@ifpackageloaded{nameref}{%
    \addtotheorempostheadhook{%
      \expandafter\NR@gettitle\expandafter{\thmt@shortoptarg}%
  }}{}
}

\AtBeginDocument{%
  \@ifpackageloaded{cleveref}{%
    \@ifpackagelater{cleveref}{2010/04/30}{%
    % OK, new enough
    }{%
      \PackageWarningNoLine{thmtools}{%
        Your version of cleveref is too old!\MessageBreak
        Update to version 0.16.1 or later%
      }
    }
  }{}
}
%    \end{macrocode}
%\iffalse
%</autoref>
%\fi
