% Feature module: math

\ifdefined\NextFeatureMathLoaded
  \endinput
\fi
\def\NextFeatureMathLoaded{1}

\RequirePackage{amsmath}
\RequirePackage{amsthm}
\RequirePackage{mathtools}
\RequirePackage{bm}
\RequirePackage{fix-cm}

\newif\ifNextMathUnicode
\newif\ifNextMathLegacyMlmodern
\makeatletter
\providecommand{\NextMathFontSelection}{auto}
\newcommand{\NextMathResolvedFont}{}
\makeatother

\ExplSyntaxOn
\tl_new:N \l__next_math_selection_tl
\tl_new:N \l__next_math_font_tl

\cs_new_protected:Npn \__next_math_select_unicode:n #1
  {
    \NextMathUnicodetrue
    \tl_set:Nn \l__next_math_font_tl {#1}
    \tl_gset:Nn \g_tmpa_tl {#1}
    \xdef\NextMathResolvedFont{\tl_use:N \g_tmpa_tl}
  }

\cs_new_protected:Npn \__next_math_resolve_font:
  {
    \NextMathUnicodefalse
    \NextMathLegacyMlmodernfalse
    \tl_clear:N \l__next_math_font_tl
    \tl_set:Nx \l__next_math_selection_tl { \NextMathFontSelection }
    \tl_if_blank:VT \l__next_math_selection_tl
      { \tl_set:Nn \l__next_math_selection_tl { auto } }
    \str_case:VnF \l__next_math_selection_tl
      {
        { auto }
          {
            \cs_if_exist:NT \NextFontFamilyLoadedTF
              {
                \NextFontFamilyLoadedTF { mlmodern }
                  { \NextMathLegacyMlmoderntrue }
                  { }
              }
          }
        { default } { }
        { legacy } { }
        { libertinus }
          { \__next_math_select_unicode:n { LibertinusMath-Regular.otf } }
        { libertinusmath }
          { \__next_math_select_unicode:n { LibertinusMath-Regular.otf } }
        { libertinus~math }
          { \__next_math_select_unicode:n { LibertinusMath-Regular.otf } }
        { newcm }
          { \__next_math_select_unicode:n { NewComputerModernMath } }
        { newcomputermodern }
          { \__next_math_select_unicode:n { NewComputerModernMath } }
        { newcomputermodernmath }
          { \__next_math_select_unicode:n { NewComputerModernMath } }
        { mlmodern }
          { \NextMathLegacyMlmoderntrue }
      }
      { \__next_math_select_unicode:n { \NextMathFontSelection } }
  }

\__next_math_resolve_font:
\ExplSyntaxOff

\ifNextMathUnicode
  \RequirePackage{unicode-math}
  \setmathfont{\NextMathResolvedFont}%
\else\ifNextMathLegacyMlmodern
  \RequirePackage{mlmodern}
  \RequirePackage{amssymb}
  \RequirePackage{amsfonts}
  \RequirePackage{mathrsfs}
\else
  \RequirePackage{amssymb}
  \RequirePackage{amsfonts}
  \RequirePackage{mathrsfs}
\fi\fi

% Default theorem-like environments controlled by NextSystem UI tag.
% Numbering follows section to keep references compact in long documents.
\makeatletter
\def\NextMathTheoremName{Theorem}
\def\NextMathLemmaName{Lemma}
\def\NextMathPropositionName{Proposition}
\def\NextMathCorollaryName{Corollary}
\def\NextMathDefinitionName{Definition}
\def\NextMathExampleName{Example}
\def\NextMathRemarkName{Remark}
\@ifundefined{NextSystemUiTag}{}{%
  \edef\NextMathUiTag{\NextSystemUiTag}%
  \def\NextMathZhTag{zh}%
  \ifx\NextMathUiTag\NextMathZhTag
    \def\NextMathTheoremName{定理}%
    \def\NextMathLemmaName{引理}%
    \def\NextMathPropositionName{命題}%
    \def\NextMathCorollaryName{推論}%
    \def\NextMathDefinitionName{定義}%
    \def\NextMathExampleName{例}%
    \def\NextMathRemarkName{註}%
  \fi
}
\makeatother

\theoremstyle{plain}
\newtheorem{theorem}{\NextMathTheoremName}[section]
\newtheorem{lemma}[theorem]{\NextMathLemmaName}
\newtheorem{proposition}[theorem]{\NextMathPropositionName}
\newtheorem{corollary}[theorem]{\NextMathCorollaryName}

\theoremstyle{definition}
\newtheorem{definition}[theorem]{\NextMathDefinitionName}
\newtheorem{example}[theorem]{\NextMathExampleName}

\theoremstyle{remark}
\newtheorem{remark}[theorem]{\NextMathRemarkName}

\everymath{\displaystyle}
