\ProvidesExplPackage {tagpdf-checks-code} {2018/08/06} {0.3}
 {part of tagpdf - code related to checks and messages}


%messages

% mc
\msg_new:nnn {uftag} {mc-nested}        { nested~marked~content~found~-~mcid~#1 }
\msg_new:nnn {uftag} {mc-tag-missing}   { required~tag~missing~-~mcid~#1 }
\msg_new:nnn {uftag} {mc-label-unknown} { label~#1~unknown~-~rerun }
\msg_new:nnn {uftag} {mc-used-twice}    { mc~#1~has~been~already~used }
\msg_new:nnn {uftag} {mc-not-open}      { there~is~no~mc~to~end~at~#1 }

% structures
\msg_new:nnn {uftag} {struct-no-objnum}      { objnum~missing~for~structure~#1 }
\msg_new:nnn {uftag} {struct-faulty-nesting} { there~is~no~open~structure~on~the~stack }
\msg_new:nnn {uftag} {struct-missing-tag}    { a~structure~must~have~a~tag! }
\msg_new:nnn {uftag} {struct-show-closing}   { closing~structure~#1~tagged~\prop_item:cn{g__uftag_struct_#1_prop}{S} }
\msg_new:nnn {uftag} {struct-used-twice}     { structure~with~label~#1~has~already~been~used}
\msg_new:nnn {uftag} {struct-label-unknown}  { structure~with~label~#1~is~unknown~rerun}


%Roles
\msg_new:nnn {uftag} {role-missing}     { tag~#1~has~no~role~assigned  }
\msg_new:nnn {uftag} {role-unknown}     { role~#1~is~not~known  }
\msg_new:nnn {uftag} {role-unknown-tag} { tag~#1~is~not~known  }
\msg_new:nnn {uftag} {role-new-tag}     { adding~new~tag~#1~mapped~to~role~#2  }


% trees
\msg_new:nnn {uftag} {tree-mcid-index-wrong} {something~is~wrong~with~the~mcid}

% obj
\msg_new:nnn {uftag} {obj-write-num} {write~obj~#1~to~pdf}


%checks
%structures

\cs_new:Nn \__uftag_check_structure_has_tag:n %#1 struct num
 {
  \prop_if_in:cnF { g__uftag_struct_#1_prop }
   {S}
   {
    \msg_error:nn {uftag} {struct-missing-tag}
   }
 }

\cs_new:Nn \__uftag_check_structure_tag:N
 {
  \prop_if_in:NoF \g__uftag_role_tags_prop {#1}
   {
    \msg_warning:nnx {uftag} {role-unknown-tag} {#1}
   }
 }

\cs_new:Nn \__uftag_check_info_closing_struct:n %#1 struct num
 {
  \msg_info:nnn {uftag} {struct-show-closing} {#1}
 }

\cs_generate_variant:Nn \__uftag_check_info_closing_struct:n {o,x}

\cs_new:Nn \__uftag_check_no_open_struck:
 {
  \msg_error:nn {uftag} {struct-faulty-nesting}
 }

\cs_new:Nn \__uftag_check_struct_used:n %#1 label
 {
  \prop_get:cnNT
   {g__uftag_struct_\zref@extractdefault{tagpdfstruct-#1}{tagstruct}{unknown}_prop}
   {P}
   \l_tmpa_tl
   { \msg_warning:nnn {uftag} {struct-used-twice} {#1} }
 }

%roles

\cs_new:Nn \__uftag_check_add_tag_role:nn %#1 tag, #2 role
 {
  \tl_if_empty:nTF {#2}
  {
   \msg_warning:nnn {uftag} {role-missing} {#1}
  }
  {
   \prop_get:NnNF \g__uftag_role_tags_prop {#2} \l_tmpa_tl
    {
     \msg_warning:nnn {uftag} {role-unknown} {#2}
    }
    {
     \msg_info:nnnn {uftag} {role-new-tag} {#1} {#2}
    }
  }
 }

%mc
\cs_new:Nn \__uftag_check_mc_if_nested:
 {
  \_uftag_mc_if_in:T
   {
    \msg_warning:nnx {uftag} {mc-nested} { \__uftag_get_mc_abs_cnt: }
   }
 }

\cs_new:Nn \__uftag_check_mc_if_open:
 {
  \_uftag_mc_if_in:F
   {
    \msg_warning:nnx {uftag} {mc-not-open} { \__uftag_get_mc_abs_cnt: }
   }
 }

\cs_new:Nn \__uftag_check_mc_tag:N
 {
  \tl_if_empty:NT #1
   {
    \msg_error:nnx {uftag} {mc-tag-missing} { \__uftag_get_mc_abs_cnt: }
   }
   \prop_if_in:NoF \g__uftag_role_tags_prop {#1}
   {
    \msg_warning:nnx {uftag} {role-unknown-tag} {#1}
   }
 }

\seq_new:N \g__uftag_check_mc_used_seq
\cs_new:Nn \__uftag_check_mc_used:n
 {
  \seq_if_in:NnTF \g__uftag_check_mc_used_seq {#1}
   {
    \msg_warning:nnn {uftag} {mc-used-twice} {#1}
   }
   {
    \seq_gput_right:Nx \g__uftag_check_mc_used_seq {#1}
   }
 }



\cs_new:Nn \__uftag_check_show_MCID_by_page:
 {
  \tl_set:Nx \l__uftag_tmpa_tl
  {
   \zref@extractdefault
    {LastPage} {abspage} {-1}
  }
  \int_step_inline:nnnn {1}{1}
  {
   \l__uftag_tmpa_tl
  }
  {
   \seq_clear:N \l_tmpa_seq
   \int_step_inline:nnnn {1}{1}
    {
     \zref@extractdefault
     {LastPage} {tagmcabs} {-1}
    }
    {
     \int_compare:nT
     {
      \zref@extractdefault
      {mcid-####1} {tagabspage} {-1}
      = ##1
     }
     {
      \seq_gput_right:Nx \l_tmpa_seq
      {
       Page##1-####1-\zref@extractdefault
       {mcid-####1} {tagmcid} {-1}
      }
     }
    }
    \seq_show:N \l_tmpa_seq
  }
 }

\cs_new:Nn \__uftag_check_record_pdfobj_num:n
 {
  \int_compare:nT { \l__uftag_loglevel_int >= 3 }
   {
    \msg_info:nnx {uftag} {obj-write-num} {#1}
   }
 }

\endinput
