\ProvidesExplPackage {tagpdf-attr-code} {2019/03/20} {0.60}
 {part of tagpdf - code related to attributes and attribute classes}

% the obj is written in tagpdf-tree-code.

\seq_new:N  \g__uftag_attr_class_used_seq
\prop_new:N \g__uftag_attr_objnum_prop %will contain obj num of used attributes

\prop_new:N \g__uftag_attr_entries_prop
\tl_new:N   \g__uftag_attr_class_content_tl
\tl_new:N   \l__uftag_attr_objtmp_tl
\tl_new:N   \l__uftag_attr_value_tl


\cs_new_protected:Nn \__uftag_attr_new_entry:nn %#1:name, #2: content
 {
  \prop_gput:Nnn \g__uftag_attr_entries_prop
   {#1}{#2}
 }

\keys_define:nn { uftag / setup }
 {
  newattribute .code:n =
   {
    \__uftag_attr_new_entry:nn #1
   }
 }


% the key for the structure:
\keys_define:nn { uftag / struct }
{
 attribute-class .code:n =
 {
   \clist_set:No \l_tmpa_clist { #1 }
   \seq_set_from_clist:NN \l_tmpa_seq \l_tmpa_clist
   \seq_map_inline:Nn \l_tmpa_seq
    {
     \prop_if_in:NnF \g__uftag_attr_entries_prop {##1}
      {
       \msg_error:nnn { uftag } { attr-unknown } { ##1 }
      }
     \seq_gput_left:Nn\g__uftag_attr_class_used_seq { ##1}
    }
   \seq_set_map:NNn \l_tmpb_seq \l_tmpa_seq
   {
     /##1
   }
  \tl_set:Nx \l_tmpa_tl
   {
    \int_compare:nT { \seq_count:N \l_tmpa_seq > 1 }{[}
    \seq_use:Nn \l_tmpb_seq  { \c_space_tl  }
    \int_compare:nT { \seq_count:N \l_tmpa_seq > 1 }{]}
   }
   \int_compare:nT { \seq_count:N \l_tmpa_seq > 0 }
    {
      \__uftag_prop_gput:cnx
       { g__uftag_struct_\int_eval:n {\c@g__uftag_struct_abs_int}_prop }
       { C }
       { \l_tmpa_tl }
      %\prop_show:c  { g__uftag_struct_\int_eval:n {\c@g__uftag_struct_abs_int}_prop }
    }
   }
}

\keys_define:nn { uftag / struct }
 {
  attribute .code:n  = % A property (attribute, value currently a dictionary)
   {
    \clist_set:No          \l_tmpa_clist { #1 }
    \seq_set_from_clist:NN \l_tmpa_seq \l_tmpa_clist
    \tl_set:Nx \l__uftag_attr_value_tl
    {
     \int_compare:nT { \seq_count:N \l_tmpa_seq > 1 }{[}
    }
    \seq_map_inline:Nn \l_tmpa_seq
    {
     \prop_if_in:NnF \g__uftag_attr_entries_prop {##1}
      {
       \msg_error:nnn { uftag } { attr-unknown } { ##1 }
      }
     \prop_if_in:NnF \g__uftag_attr_objnum_prop {##1}
      {
       \__uftag_pdfreserveobjnum:N \l_tmpa_tl
       \__uftag_pdfuseobjnum:Nx    \l_tmpa_tl
        {
          \prop_item:Nn\g__uftag_attr_entries_prop {##1}
        }
        \prop_gput:NnV \g__uftag_attr_objnum_prop {##1} {\l_tmpa_tl}
      }
     \tl_put_right:Nx \l__uftag_attr_value_tl
      {
       \c_space_tl
       \prop_item:Nn \g__uftag_attr_objnum_prop {##1}
       \c_space_tl 0 \c_space_tl R
      }
 %    \tl_show:N \l__uftag_attr_value_tl
    }
    \tl_put_right:Nx \l__uftag_attr_value_tl
     {
      \int_compare:nT { \seq_count:N \l_tmpa_seq > 1 }{]}
     }
 %   \tl_show:N \l__uftag_attr_value_tl
    \__uftag_prop_gput:cnx
     { g__uftag_struct_\int_eval:n {\c@g__uftag_struct_abs_int}_prop }
     { A }
     { \l__uftag_attr_value_tl }
   },
 }
\endinput
