\ProvidesExplPackage{stexthm}{2025/11/11}{4.0.0}{Default Typesetting for sTeX Statements}

\RequirePackage{stex}
\RequirePackage{amsthm}
\RequirePackage{thmtools}

\ExplSyntaxOn

\AddToHook{begindocument}{
   \ExplSyntaxOn\makeatletter
    \input{stexthm-english.ldf}
    \ltx@ifpackageloaded{babel}{
      \clist_set:Ne \l_tmpa_clist {\exp_args:No \tl_to_str:n \bbl@loaded}
      \exp_args:NNe \clist_if_in:NnT \l_tmpa_clist {\detokenize{ngerman}}{
        \input{stexthm-ngerman.ldf}
      }
      \exp_args:NNe \clist_if_in:NnT \l_tmpa_clist {\detokenize{finnish}}{
         \input{stexthm-finnish.ldf}
      }
      \exp_args:NNe \clist_if_in:NnT \l_tmpa_clist {\detokenize{french}}{
         \input{stexthm-french.ldf}
      }
      \exp_args:NNe \clist_if_in:NnT \l_tmpa_clist {\detokenize{russian}}{
         \input{stexthm-russian.ldf}
      }
 }{}
  \makeatother\ExplSyntaxOff
}

\cs_new_protected:Nn \_@@_stex_patch_env:nnn {
  \AddToHook{begindocument}{
    \cs_if_exist:cF {#1}{
      \declaretheorem[#2]{#1}
    }
  }
  \clist_map_inline:nn{#3}{
    \use:c{stexstyle##1}[#1]{
      \tl_if_empty:NTF\thistitle{
        \begin{#1}
      }{
        \begin{#1}[\thistitle]
      }
    }{ \end{#1} }
  }
}

\stexstyleexample{
  \tl_if_empty:NTF\thistitle{
    \begin{example}
  }{
    \begin{example}[\thistitle]
  }
}{
    \end{example}
}
\stexstyledefinition{
  \tl_if_empty:NTF\thistitle{
    \begin{definition}
  }{
    \begin{definition}[\thistitle]
  }
}{
    \end{definition}
}

\cs_new:Nn \_stexthm_css:n {
  font-weight:~bold;
  font-size:inherit;
  &::before~{
    content:~"#1"~"~"counter(ftml-section)~"."~counter(ftml-para);
  }
  &::after~{
    content: ".~";
  }
}

\AddToHook{begindocument}{
  \stexcssassertion[theorem,counter=para,parent=section]{
  }{\_stexthm_css:n \stexthm@kw@theorem}
  \stexcssassertion[observation,counter=para,parent=section]{
  }{\_stexthm_css:n \stexthm@kw@observation}
  \stexcssassertion[corollary,counter=para,parent=section]{
  }{\_stexthm_css:n \stexthm@kw@corollary}
  \stexcssassertion[lemma,counter=para,parent=section]{
  }{\_stexthm_css:n \stexthm@kw@lemma}
  \stexcssassertion[axiom,counter=para,parent=section]{
  }{\_stexthm_css:n \stexthm@kw@axiom}
  \stexcssassertion[remark,counter=para,parent=section]{
  }{\_stexthm_css:n \stexthm@kw@remark}
  \stexcssparagraph[remark,counter=para,parent=section]{
  }{\_stexthm_css:n \stexthm@kw@remark}
  \stexcssdefinition[counter=para,parent=section]{
  }{\_stexthm_css:n \stexthm@kw@definition}
  \stexcssexample[counter=para,parent=section]{
  }{\_stexthm_css:n \stexthm@kw@example}
}

\_@@_stex_patch_env:nnn{theorem}{parent=section,name=\stexthm@kw@theorem}{assertion}
\_@@_stex_patch_env:nnn{observation}{sibling=theorem,name=\stexthm@kw@observation}{assertion}
\_@@_stex_patch_env:nnn{corollary}{sibling=theorem,name=\stexthm@kw@corollary}{assertion}
\_@@_stex_patch_env:nnn{lemma}{sibling=theorem,name=\stexthm@kw@lemma}{assertion}
\_@@_stex_patch_env:nnn{axiom}{sibling=theorem,name=\stexthm@kw@axiom}{assertion}
\_@@_stex_patch_env:nnn{remark}{sibling=theorem,name=\stexthm@kw@remark}{assertion,paragraph}
\AddToHook{begindocument}{
  \cs_if_exist:cF {example}{
    \declaretheorem[sibling=theorem,style=definition,name=\stexthm@kw@example]{example}
  }
  \cs_if_exist:cF {definition}{
    \declaretheorem[sibling=theorem,style=definition,name=\stexthm@kw@definition]{definition}
  }
}

\ExplSyntaxOff
