% Internal reusable layout component library.
% These components are not part of the public registry surface.

\ExplSyntaxOn

\prop_new:N \g__layout_component_loaded_prop
\providecommand{\NextBeamerRegisterSection}[2]{}

\makeatletter

\newcommand{\NextLayoutPageOneIn}{
  \if@twoside
    \RequirePackage[a4paper,margin=1in,bindingoffset=0.4cm]{geometry}
  \else
    \RequirePackage[a4paper,margin=1in]{geometry}
  \fi
}

\newcommand{\NextLayoutPageTwentySixMm}{
  \if@twoside
    \RequirePackage[a4paper,margin=2.6cm,bindingoffset=0.4cm]{geometry}
  \else
    \RequirePackage[a4paper,margin=2.6cm]{geometry}
  \fi
}

\newcommand{\NextLayoutHeadFancyChapter}{
  \RequirePackage{fancyhdr}
  \pagestyle{fancy}
  \fancyhf{}
  \renewcommand{\chaptermark}[1]{\NextSetRunningHeader{##1}\markboth{##1}{}}
  \renewcommand{\sectionmark}[1]{}
  \renewcommand{\subsectionmark}[1]{}
  \renewcommand{\subsubsectionmark}[1]{}
  \if@twoside
    \fancyhead[LE]{\small\thepage}
    \fancyhead[RE]{\itshape\small\NextHeaderTitleContent}
    \fancyhead[LO]{\itshape\small\NextRunningHeaderContent}
    \fancyhead[RO]{\small\thepage}
  \else
    \fancyhead[L]{\itshape\small\NextHeaderTitleContent}
    \fancyhead[R]{\small\thepage}
  \fi
  \renewcommand{\headrulewidth}{0.4pt}
  \renewcommand{\footrulewidth}{0pt}
  \setlength{\headheight}{14pt}
  \fancypagestyle{plain}{
    \fancyhf{}
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}
  }
}

\newcommand{\NextLayoutBookOpenRight}{
  \@openrighttrue
}

\newcommand{\NextLayoutBookBlankPageEmpty}{
  \renewcommand{\cleardoublepage}{
    \clearpage
    \if@twoside
      \ifodd\c@page
      \else
        \hbox{}
        \thispagestyle{empty}
        \newpage
        \if@twocolumn
          \hbox{}
          \newpage
        \fi
      \fi
    \fi
  }
}

% Standard LaTeX TOC entries reserve a fixed-width number box. Grow that box
% to the label's natural width when necessary, while retaining the class's
% original minimum width for short labels.
\newdimen\NextLayoutTocLabelWidth
\newcommand{\NextLayoutTocNumberline}[1]{%
  \settowidth{\NextLayoutTocLabelWidth}{#1\hspace{0.8em}}%
  \ifdim\NextLayoutTocLabelWidth<\@tempdima
    \NextLayoutTocLabelWidth=\@tempdima
  \fi
  \hb@xt@\NextLayoutTocLabelWidth{#1\hfil}%
}
\newcommand{\NextLayoutDynamicTocLabelSpacing}{%
  \let\numberline\NextLayoutTocNumberline
}

\makeatother

\cs_new_protected:cpn { __layout_component_page_a4_1in: }
  { \NextLayoutPageOneIn }

\cs_new_protected:cpn { __layout_component_page_a4_en_doc_145mm: }
  {
    \if@twoside
      \RequirePackage[a4paper,margin=32.5mm,bindingoffset=4mm]{geometry}
    \else
      \RequirePackage[a4paper,margin=32.5mm]{geometry}
    \fi
  }

\cs_new_protected:cpn { __layout_component_page_a4_26mm: }
  { \NextLayoutPageTwentySixMm }

\cs_new_protected:cpn { __layout_component_page_a4_book: }
  {
    \RequirePackage[
      a4paper,
      twoside,
      inner=3.2cm,
      outer=2.4cm,
      top=2.6cm,
      bottom=2.6cm,
      bindingoffset=0.4cm
    ]{geometry}
  }

\cs_new_protected:cpn { __layout_component_page_a4_en_book_145mm: }
  {
    \RequirePackage[
      a4paper,
      twoside,
      inner=3.6cm,
      outer=2.5cm,
      top=2.6cm,
      bottom=2.6cm,
      bindingoffset=0.4cm
    ]{geometry}
  }

\cs_new_protected:cpn { __layout_component_text_en_doc: }
  {
    \NextLayoutDynamicTocLabelSpacing
    \RequirePackage{setspace}
    \setstretch{1.40}
    \setlength{\parindent}{15pt}
    \setlength{\parskip}{0.25em}
  }

\cs_new_protected:cpn { __layout_component_text_en_book: }
  {
    \NextLayoutDynamicTocLabelSpacing
    \RequirePackage{setspace}
    \setstretch{1.42}
    \setlength{\parindent}{15pt}
    \setlength{\parskip}{0.25em}
  }

\cs_new_protected:cpn { __layout_component_text_en: }
  { \use:c { __layout_component_text_en_doc: } }

\cs_new_protected:cpn { __layout_component_text_zh: }
  {
    \NextLayoutDynamicTocLabelSpacing
    \RequirePackage{indentfirst}
    \RequirePackage{setspace}
    \RequirePackage{enumitem}
    \setstretch{1.70}
    \setlength{\parindent}{2em}
    \setlength{\parskip}{0.35em}
    \renewenvironment{quote}
      {\small
       \list{}{\leftmargin=4em
                \rightmargin=0em
                \topsep=0.5em
                \parsep=0pt}
       \item\relax}
      {\endlist}
    \setlist[enumerate]{
      leftmargin=2em,
      labelsep=0.5em,
      itemindent=0pt,
      listparindent=2em,
      topsep=0.15em,
      partopsep=0pt,
      parsep=0pt,
      itemsep=0.25em,
      before=\setstretch{1.35},
      after=\setstretch{1.70}
    }
    \setlist[itemize]{
      leftmargin=2em,
      labelsep=0.5em,
      itemindent=0pt,
      listparindent=2em,
      topsep=0.15em,
      partopsep=0pt,
      parsep=0pt,
      itemsep=0.25em,
      before=\setstretch{1.35},
      after=\setstretch{1.70},
      label=\textbullet
    }
  }

\cs_new_protected:cpn { __layout_component_text_beamer_dense: }
  { \setlength{\parskip}{0.35em} }

\cs_new_protected:cpn { __layout_component_head_fancy_chapter: }
  { \NextLayoutHeadFancyChapter }

\cs_new_protected:cpn { __layout_component_book_openright: }
  { \NextLayoutBookOpenRight }

\cs_new_protected:cpn { __layout_component_book_blankpage_empty: }
  { \NextLayoutBookBlankPageEmpty }

\cs_new_protected:cpn { __layout_component_slides_madrid_nav: }
  {
    \RequirePackage{etoolbox}
    \RequirePackage{multicol}
    \mode<presentation>
    \usetheme{Madrid}
    \usefonttheme{professionalfonts}
    \setbeamertemplate{navigation symbols}{}
    \ifdefined\NextSystemUiTag
      \def\NextLayoutZhTag{zh}%
      \ifx\NextSystemUiTag\NextLayoutZhTag
        \setbeamertemplate{footline}[frame number]%
      \fi
    \fi
    \providecommand{\NextBeamerAuthorShape}{\upshape}
    \setbeamerfont{title}{family=\sffamily,series=\bfseries}
    \setbeamerfont{subtitle}{family=\sffamily,series=\mdseries,size=\large}
    \setbeamerfont{author}{family=\sffamily,series=\mdseries,shape=\NextBeamerAuthorShape}
    \setbeamerfont{frametitle}{family=\sffamily,series=\bfseries}
    \setbeamerfont{block title}{family=\sffamily,series=\bfseries}
    \setbeamerfont{block body}{family=\sffamily}
    \setbeamerfont{section in head/foot}{family=\sffamily}
    \providecommand{\AgendaTitle}{Contents}
    \newcommand{\AgendaFrame}{
      \begingroup
      \Large
      \linespread{1.5}\selectfont
      \begin{frame}[plain]{\AgendaTitle}
        \hspace*{1.8em}
        \begin{minipage}{0.88\linewidth}
          \tableofcontents
        \end{minipage}
      \end{frame}
      \endgroup
    }
    \newenvironment{VocabCols}[1]{
      \begin{multicols}{##1}
        \raggedcolumns
        \setlength{\columnseprule}{0pt}
        \setlength{\columnsep}{1.2em}
    }{
      \end{multicols}
    }
  }

\cs_new_protected:Npn \__layout_apply_component:n #1
  {
    \prop_if_in:NnTF \g__layout_component_loaded_prop {#1}
      { }
      {
        \cs_if_exist:cTF { __layout_component_#1: }
          {
            \use:c { __layout_component_#1: }
            \prop_put:Nnn \g__layout_component_loaded_prop {#1} { loaded }
          }
          {
            \PackageError{layout-modules}
              {Unknown~internal~layout~component~`#1`}
              {Define~the~component~before~using~it~from~a~preset.}
          }
      }
  }

\cs_new_protected:Npn \__layout_apply_component_list:n #1
  {
    \tl_if_blank:nTF {#1}
      { }
      { \clist_map_inline:nn {#1} { \__layout_apply_component:n {##1} } }
  }

\ExplSyntaxOff
