%
% Copyright (c) 2021-2023 Zeping Lee
% Released under the MIT License.
% Repository: https://github.com/zepinglee/citeproc-lua
%

\NeedsTeXFormat{LaTeX2e}

\RequirePackage{expl3}
\RequirePackage{xparse}

\ProvidesExplPackage {citation-style-language} {2023-07-04} {0.4.2}
  {Citation Style Language for LaTeX}

\RequirePackage { l3keys2e }
\RequirePackage { filehook }
\RequirePackage { url }


% Check incompatible packages
\msg_new:nnn { citation-style-language } { incompatible-package }
  { The~ `#1'~ package~ is~ incompatible~ with~ `citation-style-language'. }

\clist_map_inline:nn
  {
    babelbib , backref , biblatex , bibtopic , bibunits , chapterbib ,
    cite , citeref , inlinebib , jurabib , mcite , mciteplus ,
    multibib , natbib , splitbib
  }
  {
    \AtBeginOfPackageFile* {#1}
      { \msg_error:nnn { citation-style-language } { incompatible-package } {#1} }
  }


% Load Lua module in LuaTeX
\sys_if_engine_luatex:T
  { \lua_now:n { csl = require("citeproc-latex") } }


% Global options

\NewDocumentCommand \cslsetup { m }
  { \keys_set:nn { csl } {#1} }


\bool_new:N \l__csl_regression_test_bool
\tl_new:N \l__csl_style_tl
\newcommand \csl@style {}  % For latex2e interface
\tl_new:N \l__csl_class_tl
\bool_new:N \l__csl_note_bool
\clist_new:N \l__csl_bib_resources_clist
\tl_new:N \l__csl_locale_tl
\bool_new:N \l__csl_back_ref_bool
\str_new:N \l__csl_back_ref_type_str
% Citation formatting
\tl_new:N \l__csl_prefix_separator_tl
\tl_new:N \l__csl_suffix_separator_tl
% Bibliography formatting
\tl_new:N \bibsection
\tl_new:N \l__csl_bib_font_tl
\bool_new:N \l__csl_bib_entry_page_break_bool
\tl_new:N \l__csl_bib_item_sep_tl
\tl_new:N \l__csl_bib_hang_tl


\keys_define:nn { csl }
  {
    regression-test .bool_set:N = \l__csl_regression_test_bool ,
    style .code:n =
      {
        \tl_set:Nn \l__csl_style_tl {#1}
        \renewcommand \csl@style {#1}
      } ,
    class .code:n =
      {
        \tl_set:Nn \l__csl_class_tl {#1}
        \tl_if_eq:NnT \l__csl_class_tl { note }
          { \bool_set_true:N \l__csl_note_bool }
      } ,
    locale .tl_set:N = \l__csl_locale_tl ,
    backref .choices:nn =
      { true, page, section, false }
      { \exp_args:NV \__csl_set_back_ref:n \l_keys_choice_tl } ,
    % Citation formatting
    prefix-separator .tl_set:N = \l__csl_prefix_separator_tl ,
    suffix-separator .tl_set:N = \l__csl_suffix_separator_tl ,
    % Bibliography formatting
    bib-section .tl_set:N = \bibsection ,
    bib-font .tl_set:N = \l__csl_bib_font_tl ,
    bib-entry-page-break .bool_set:N = \l__csl_bib_entry_page_break_bool ,
    bib-item-sep .tl_set:N = \l__csl_bib_item_sep_tl ,
    bib-hang .tl_set:N = \l__csl_bib_hang_tl ,
    % The following keys are for easier interaction between LaTeX and
    % citeproc-lua engine and they are not designed for end-users.
    undefined-cites .code:n =
      {
        \clist_map_inline:nn {#1}
          { \__csl_warn_citation_undefined:n {##1} }
      } ,
  }

\cs_new:Npn \__csl_set_back_ref:n #1
  {
    \str_if_eq:nnTF {#1} {false}
      {
        \bool_set_false:N \l__csl_back_ref_bool
      }
      {
        \bool_set_true:N \l__csl_back_ref_bool
        \str_if_eq:nnTF {#1} {true}
          { \str_set:Nn \l__csl_back_ref_type_str { page } }
          { \str_set:Nn \l__csl_back_ref_type_str {#1} }
      }
  }

% Default settings
\keys_set:nn { csl }
  {
    backref = false ,
    prefix-separator = { ~ } ,
    suffix-separator = { , ~ } ,
    bib-entry-page-break = true ,
    bib-hang = { 1 em } ,
  }


\ProcessKeysPackageOptions { csl }


\DeclareDocumentCommand \bibliographystyle { m }
  {
    \__csl_if_preamble:F
      { \__csl_write_aux_bibstyle:n {#1} }
    \tl_set:Nn \l__csl_style_tl {#1}
  }

\tl_new:N \g__csl_aux_bibstyle_tl
\cs_set:Npn \csl@aux@style #1
  { \tl_gset:Nn \g__csl_aux_bibstyle_tl {#1} }


% Write to an empty `.ccf` file for use with latexmk
\iow_new:N \l__csl_ccf_iow
\iow_open:Nn \l__csl_ccf_iow { \c_sys_jobname_str .ccf }
\iow_now:Nx \l__csl_ccf_iow { }
\iow_close:N \l__csl_ccf_iow


% Load other modules
\input{citation-style-language-init.sty}
\input{citation-style-language-data.sty}
\input{citation-style-language-cite.sty}
\input{citation-style-language-bib.sty}
\input{citation-style-language-compatible.sty}


\endinput
