\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{jrpie-yaref}[2023/07/28 - yet another ref]

\RequirePackage{hyperref}
\RequirePackage{amstext}
\RequirePackage{xspace}

\newwrite\yaref@output
\immediate\openout\yaref@output=\jobname.yaref.json
\write\yaref@output{data = [}

\newcounter{yarefproof@depth}
\setcounter{yarefproof@depth}{0}

\edef\yaref@hastag{\string#}%

\let\yaref@oldlabel\label
\def\publicurl{https://josia-notes.users.abstractnonsen.se/w23-logic-2/logic2.pdf}

\newenvironment{yarefproof}[1]{%
    \ifnum\the\value{yarefproof@depth}=0%
        \global\def\yarefproof@current{#1}%
    \fi%
    \addtocounter{yarefproof@depth}{1}%
    \begin{refproof}{#1}% Depth: \theyarefproof@depth
}{%
    \end{refproof}%
    \addtocounter{yarefproof@depth}{-1}%
    %\ifnum\yarefproof@depth=0%
    %    \def\yarefproof@current{nothing}%
    %\fi%
    %\let\yarefproof@current=\undefined%
    \ignorespacesafterend%
}

\newcommand{\yaref@writedependency}[1]{%
    \ifnum\the\value{yarefproof@depth}=0%
        % not in a proof environment
        %\write\yaref@output{EDGE: #1 - \theyarefproof@depth}%
    \else%
        \ifcsname yarefproof@current\endcsname%
            \write\yaref@output{{data: {id:"#1\yarefproof@current", source:"#1", target: "\yarefproof@current"}},}%
        \else%
            % in a proof env, but no label defined ???
            %\write\yaref@output{{#1 - ERROR}}%
        \fi%
    \fi%
}

\newcommand{\yaref@text@large}[1]{%
    \ifcsname yaref@longlabel@#1\endcsname%
        \hyperref[#1]{\csname yaref@longlabel@#1\endcsname\ (\ref*{#1})}%
    \else%
        \autoref{#1}%
    \fi%
}
\newcommand{\yaref@text@small}[1]{%
    \ifcsname yaref@shortlabel@#1\endcsname%
        \hyperref[#1]{\csname yaref@shortlabel@#1\endcsname}%
    \else%
        (\ref{#1})%
    \fi%
}
\newcommand{\yaref@math@large}[1]{%
    \text{\yaref@text@large{#1}}%
}
\newcommand{\yaref@math@small}[1]{%
    \text{\yaref@text@small{#1}}%
}
\newcommand{\yaref@math@verysmall}[1]{%
    \yaref@math@small{#1}%
}

\newcommand{\yalabel}[3]{%
    \yaref@oldlabel{#3}%
    \write\@auxout{\noexpand\expandafter\noexpand\gdef\noexpand\csname yaref@longlabel@#3\noexpand\endcsname{#1}}%
    \write\@auxout{\noexpand\expandafter\noexpand\gdef\noexpand\csname yaref@shortlabel@#3\noexpand\endcsname{#2}}%
    \write\yaref@output{{data: {id: "#3", label: "#1", short: "#2", href: "\publicurl\yaref@hastag\getrefbykeydefault{#3}{anchor}{}"}},}%
    \expandafter\gdef\csname yaref@longlabel@#3\endcsname{#1}%
    \expandafter\gdef\csname yaref@shortlabel@#3\endcsname{#2}%
}

\newcommand{\yaref}[1]{%
    \yaref@writedependency{#1}%
    \relax\ifmmode%
        \mathchoice
            {\yaref@math@large{#1}}      % display style
            {\yaref@math@large{#1}}      % text style
            {\yaref@math@small{#1}}      % script style
            {\yaref@math@verysmall{#1}}  % scriptscript style
    \else%
        \yaref@text@large{#1}\xspace%
    \fi%
}
% Force a small reference
\newcommand{\yarefs}[1]{%
    %\yaref@writedependency{#1}%
    \relax\ifmmode%
        \yaref@math@verysmall{#1}% scriptscript style
    \else%
        \yaref@text@small{#1}\xspace%
    \fi%
}
\renewcommand{\label}[1]{%
    \yaref@oldlabel{#1}%
    % \getrefbykeydefault{#1}{name}{}
    \write\yaref@output{{data: {id: "#1", label: "\getrefnumber{#1}", href: "\publicurl\yaref@hastag\getrefbykeydefault{#1}{anchor}{}"}},}%
}

\AtEndDocument{\write\yaref@output{{}]}}
