% Copyright 2026 Daniel Nagel
%
% 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 Daniel Nagel
%
\@ifpackageloaded{phosphoricons}{}{\PackageError{phosphoricons-utex-helper}
  {This package should not be loaded individually.
   Load phosphoricons instead.}{}}

\ProvidesExplPackage{phosphoricons-utex-helper}{2026/08/23}{2.1.2-1}
  {XeTeX/LuaTeX helper for phosphoricons}

% Mapping line:
%   \__phosphoricons_def_icon:nnnnnn {macro} {name} {cp}
%     {{plain-chunk}{plain-slot}} {{duo-chunk}{duo-slot}} {{cp2}{chunk2}{slot2}}
% Unicode engines render by codepoint; the chunk/slot pairs are only used on
% pdfTeX. Stored per icon: {cp} {cp2 or empty}
\cs_new_protected:Nn\__phosphoricons_def_icon:nnnnnn{
  \tl_if_empty:nF{#1}{
    \cs_new_protected:Npn#1{\ppPreselectedIcon{#2}}
  }
  \tl_const:cx{c__phosphoricons_icon_#2_tl}
    {{\exp_not:n{#3}}{\tl_if_blank:nF{#6}{\__phosphoricons_first_cp:nw#6\q_stop}}}
}
\cs_new:Npn\__phosphoricons_first_cp:nw#1#2\q_stop{\exp_not:n{#1}}

\cs_new_protected:Npn\__phosphoricons_glyph:nn#1#2{
  % {weight}{cp}
  {\usefont{TU}{phosphoricons}{#1}{n}\char#2 }
}

\cs_new_protected:Npn\__phosphoricons_icon_at:nnnn#1#2#3#4{
  % {cp} {cp2 or empty} {weight} {name}
  \str_if_eq:nnTF{#3}{duotone}{
    \tl_if_blank:nTF{#2}{
      \phosphoricons_duotone_layers:nn{\__phosphoricons_glyph:nn{duotone}{#1}}{}
    }{
      \phosphoricons_duotone_layers:nn
        {\__phosphoricons_glyph:nn{duotone}{#1}}
        {\__phosphoricons_glyph:nn{duotone}{#2}}
    }
  }{
    \__phosphoricons_glyph:nn{#3}{#1}
  }
}

\cs_new_protected:Nn\phosphoricons_use_icon:nn{
  \cs_if_exist:cTF{c__phosphoricons_icon_#2_tl}{
    \exp_last_unbraced:Nv
      \__phosphoricons_icon_at:nnnn
      {c__phosphoricons_icon_#2_tl}
        {#1}{#2}
  }{
    \msg_error:nnx{phosphoricons}{icon-not-found}{#2}
  }
}
