%% This is file `coloredtheorem.sty',
%%
%% Copyright (C) 2024, 2025 by João Lourenço <joao.lourenco@fct.unl.pt>
%%
%% This file 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 2006/05/20 or later.
%%
\def\cthfileversion{1.1.2}
\def\cthfiledate   {2025/04/01}
\edef\cthfilename  {\@currname}

\ProvidesPackage{coloredtheorem}[%
  \cthfiledate\  v\cthfileversion (João M. Lourenço) - A colorful boxed theorem environment.]

\RequirePackage[most]{tcolorbox}
\tcbuselibrary{breakable}

\gdef\tcbth{cth}

\NewDocumentCommand{\TcbthNewDocumentCommand}{m}{%
    \expandafter\NewDocumentCommand\csname\tcbth#1\endcsname}
\NewDocumentCommand{\TcbthNewDocumentEnvironment}{m}{%
    \expandafter\NewDocumentEnvironment\expandafter{\tcbth#1}}
\NewDocumentCommand{\TcbthGdef}{O{\tcbth}mm}{%
    \expandafter\gdef\csname#1#2\endcsname{#3}}

\TcbthNewDocumentCommand{newtheorem} { m m O{} }{%
  \expandafter\newcounter\expandafter{\tcbth#1}%
  \TcbthGdef{#1name}{#2}%
  \TcbthGdef{list#1name}{List of #2s}%
  \TcbthGdef[]{#1autorefname}{#2}%
  % \AtBeginDocument{%
      \ifdefined\crefname
        \expandafter\crefname\expandafter{\tcbth#1}{#2}{#2s}
        \expandafter\Crefname\expandafter{\tcbth#1}{\MakeUppercase#2}{\MakeUppercase#2s}
      \fi%
      % }%
  \TcbthGdef{listof#1s}
  {%
    \ifdefined\chapter\chapter*{\csname\tcbth list#1name\endcsname}%
        \else\section*{\csname\tcbth list#1name\endcsname}\fi%
    \expandafter\@starttoc\expandafter{lo#1}%
  }
  \TcbthNewDocumentEnvironment{#1} {O{} m O{}}%
  {% \begin{tcb<SOMETHING>}
    \begin{center}%
      \ifx\relax##2\relax
      \else
        \expandafter\refstepcounter\expandafter{\tcbth#1}%
        \ifx\relax##1\relax
          \expandafter\addcontentsline\expandafter{lo#1}{subsection}
                        {\protect\numberline{\csname the\tcbth#1\endcsname}##2}%
        \else
          \expandafter\addcontentsline\expandafter{lo#1}{subsection}
                        {\protect\numberline{\csname the\tcbth#1\endcsname}##1}%
        \fi
      \fi
      \begin{tcolorbox}[breakable,lowerbox=ignored,left=0mm,right=0mm,top=0mm,bottom=0mm,
                        title={\textbf{\csname\tcbth#1name\endcsname
                               \ifx\relax##2\relax\else
                                   ~\csname the\tcbth#1\endcsname\fi}~~##2},
                        #3, ##3,
                       ]%
      % \let\origpar=\par%
      % \def\par{\origpar\medskip}%
      \ignorespaces%
  }{% \end{tcb<SOMETHING>}
       \end{tcolorbox}%
     \end{center}%
  }%
  \TcbthNewDocumentEnvironment{#1*} {O{} m O{}}%
  {% \begin{tcb<SOMETHING>}
    \begin{center}%
      \begin{tcolorbox}[breakable,lowerbox=ignored,left=0mm,right=0mm,top=0mm,bottom=0mm,
                        title={\textbf{\csname\tcbth#1name\endcsname}~~##2},
                        #3, ##3,
                       ]%
      % \let\origpar=\par%
      % \def\par{\origpar\medskip}%
      \ignorespaces%
  }{% \end{tcb<SOMETHING>}
       \end{tcolorbox}%
     \end{center}%
  }%
}
