%% This is file `hyperxmp-patches-tmp-ltx.sty"
% Copyright (C) 2019-2021 The LaTeX Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version.  The latest version
% of this license is in the file
%
%    https://www.latex-project.org/lppl.txt
%
% This file is part of the "pdfmanagement bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
%    https://github.com/latex3/pdfresources
%
% for those people who are interested.
\NeedsTeXFormat{LaTeX2e}[2020/10/01]
\ProvidesExplPackage{hyperxmp-patches-tmp-ltx}
  {2021-03-07} {0.95b}
  {Store hyperref metadata in XMP format / temporay patches to test pdfresource management ... UF}

\cs_if_exist:NT \pdfmanagement_add:nnn
  {
    \pdfmanagement_if_active:T
     {
       \renewcommand\hyxmp@embed@packet{\hyxmp@embed@packet@generic}
     }
  }

%
\newcommand*{\hyxmp@embed@packet@generic}
 {%
   \typeout{}
   \typeout{!!~new-hyperxmp--generic~packet~command~used!!}{}
   \typeout{}
   \hyxmp@construct@packet
   %or some other command to write a object:
   \exp_args:Nnx
    \pdf_object_unnamed_write:nn{stream}{{/Type~/Metadata~/Subtype~/XML}{\hyxmp@xml}}%
    % reference in the Catalog:
    \pdfmanagement_add:nnx {Catalog} {Metadata}{\pdf_object_ref_last:}
 }

% A standard should be retrieved from the document settings.
\cs_new:Npn \__hypxmp_get_Astandard:w #1-#2#3#4\q_stop
 {
   \tl_if_eq:nnT{#1}{A}
    {
      \Hy@pdfatrue
      \tl_set:Nn \@pdfapart{#2}
      \tl_set:Nx \@pdfaconformance{#3}
    }
 }
\cs_new_protected:Npn \__hyxmp_get_metadata:
 {
  \exp_last_unbraced:Ne \__hypxmp_get_Astandard:w
    {\GetDocumentProperties{document/pdfstandard}}Z-ZZZ\q_stop
   \clist_map_inline:nn
    {
      % hyperxmp keys:
      ,pdfcopyright
      ,pdftype
      ,pdflicenseurl
      ,pdfauthortitle
      ,pdfcaptionwriter
      ,pdfmetalang
      %,pdfapart %document
      %,pdfaconformance %document
      ,pdfuapart %probably document too but later ...
      ,pdfxstandard
      ,pdfsource
      ,pdfdocumentid
      ,pdfinstanceid
      ,pdfversionid
      ,pdfrendition
      ,pdfpublication
      ,pdfpubtype
      ,pdfbytes
      ,pdfnumpages
      ,pdfissn
      ,pdfeissn
      ,pdfisbn
      ,pdfbookedition
      ,pdfpublisher
      ,pdfvolumenum
      ,pdfissuenum
      ,pdfpagerange
      ,pdfdoi
      ,pdfurl
      ,pdfidentifier
      ,pdfsubtitle
      ,pdfpubstatus
      ,pdfcontactaddress
      ,pdfcontactcity
      ,pdfcontactregion
      ,pdfcontactpostcode
      ,pdfcontactcountry
      ,pdfcontactphone
      ,pdfcontactemail
      ,pdfcontacturl
      ,pdfdate
    %hyperref, needs probably special handling
      ,pdftitle
      ,pdfsubject
      ,pdfkeywords
      ,pdfproducer
    }
    {
     \tl_if_exist:cF{@##1}{\tl_new:c{@##1}}
     \tl_set:cx {@##1}{\GetDocumentProperties{hyperref/##1}}
    }

    % pdfauthor, this is only a work around for simple author
    % TODO needs improvement
    \pdfstringdef\@pdfauthor{\GetDocumentProperties{hyperref/pdfauthor}}
    \cs_set_eq:NN\hyxmp@pdfauthor\@pdfauthor

    % pdflang,
    \tl_if_exist:cF{@pdflang}{\tl_new:c{@pdflang}}
    \tl_set:cx {@pdflang}{\GetDocumentProperties{document/lang}}
  }

% we need to provide a few commands so that hyperxmp stops to overwrite them
% in \AtEndPreamble
\AddToHook{begindocument/before}[pdfmanagement/firstaid/hyperxmp]{%
 \def\@pdfauthor{Author}\def\@pdftitle {Title}}

\DeclareHookRule{begindocument/before}{pdfmanagement/firstaid/hyperxmp}{before}{hyperxmp}

\AddToHook{enddocument}[pdfmanagement-firstaid]{\__hyxmp_get_metadata:}

\DeclareHookRule{enddocument}{pdfmanagement-firstaid}{before}{hyperxmp}


\endinput
