%%
%%  Copyright (C) 2020 by Leon Sixt <ctan@leon-sixt.de>
%%
%%  This work 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.3 or later is part of all distributions of
%%  LaTeX version 2005/12/01 or later.
%%
%%  This work has the LPPL maintenance status `maintained'.
%%
%%  The Current Maintainer of this work is Leon Sixt.
%%
\NeedsTeXFormat{LaTeX2e}[2020/02/02]
\ProvidesExplPackage{emoji}{2020/05/13}{0.3}{Add Emojis to Citations (in LuaLaTeX)}

\usepackage{emoji}
\usepackage{natbib}

\usepackage{xparse}
\ExplSyntaxOn

\makeatletter
\NewDocumentCommand{\emojicite}{m}
 {
  \emojicite_multicitep:n { #1 }
 }
\makeatother


\makeatletter
\NewDocumentCommand{\emojicitep}{m}
 {
  \NAT@open
  \emojicite_multicitep:n { #1 }
  \NAT@close
 }
\makeatother

\seq_new:N \l_emojicite_multicite_in_seq
\seq_new:N \l_emojicite_multicite_out_seq
\seq_new:N \l_emojicite_cite_seq

\cs_new_protected:Npn \emojicite_multicitep:n #1
 {
  \seq_set_split:Nnn \l_emojicite_multicite_in_seq { ; } { #1 }
  \seq_clear:N \l_emojicite_multicite_out_seq
  \seq_map_inline:Nn \l_emojicite_multicite_in_seq
   {
    \emojicite_cite_process:n { ##1 }
   }
  \seq_use:Nn \l_emojicite_multicite_out_seq { ;~ }
 }

\cs_new_protected:Npn \emojicite_cite_process:n #1
{
    \seq_set_split:Nnn \l_emojicite_cite_seq { , } { #1 }
    \int_compare:nTF { \seq_count:N \l_emojicite_cite_seq == 1 }
    {
        \seq_put_right:Nn \l_emojicite_multicite_out_seq
        { \citeauthor{#1},~\citeyear{#1} }
    }
    {
        \int_compare:nTF { \seq_count:N \l_emojicite_cite_seq == 2 }
        {
            \seq_put_right:Nx \l_emojicite_multicite_out_seq
             {
                 \exp_not:N \citeauthor{\seq_item:Nn \l_emojicite_cite_seq { 1 }},~
                 \exp_not:N \citeyear{\seq_item:Nn \l_emojicite_cite_seq { 1 }}~
                 \emoji{\seq_item:Nn \l_emojicite_cite_seq { 2 }}
             }
        }
        % we can handle up to 2 emojis
        {
            \int_compare:nTF { \seq_count:N \l_emojicite_cite_seq == 3 }
            {
                \seq_put_right:Nx \l_emojicite_multicite_out_seq
                 {
                     \exp_not:N \citeauthor{\seq_item:Nn \l_emojicite_cite_seq { 1 }},~
                     \exp_not:N \citeyear{\seq_item:Nn \l_emojicite_cite_seq { 1 }}~
                     \emoji{\seq_item:Nn \l_emojicite_cite_seq { 2 }}
                     \emoji{\seq_item:Nn \l_emojicite_cite_seq { 3 }}
                 }
            }
            {
                \seq_put_right:Nx \l_emojicite_multicite_out_seq
                 {
                     \exp_not:N \citeauthor{\seq_item:Nn \l_emojicite_cite_seq { 1 }},~
                     \exp_not:N \citeyear{\seq_item:Nn \l_emojicite_cite_seq { 1 }}~
                     \exp_not:N \textbf{emojicite~does~not~support~more~than~two~emojis}.
                 }
            }
        }
    }
 }
\ExplSyntaxOff

