% Copyright 2022 by Qrrbrbirlbel
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%

\pgfdeclarelayer{discard}

\def\tikzext@layers@swapfirstsetbox#1\setbox#2\tikzext@stop{%
  #1\tikzext@layers@setbox@which#2%
}
\def\tikzext@layers@whichbox{\tikz@figbox}%
\let\tikzext@layers@setbox@which\setbox

\def\tikzextshowdiscardlayeranyway{\box\pgf@layerbox@discard}
\pgfqkeys{/tikz/ext/layers}{
  discard layer/.code=\expandafter\let\csname pgf@layerbox@#1\endcsname\pgfutil@voidb@x,
  discard layer global/.code=\expandafter\global\expandafter\let\csname pgf@layerbox@#1\endcsname\pgfutil@voidb@x,
  in box/.code={%
    \expandafter\ifx\tikzext@layers@whichbox#1%
      \pgfutil@packagewarning{tikz-ext}{Layer is already set. Setting will be skipped.}%
    \else
      \def\tikzext@layers@whichbox{#1}%
      \def\tikz@whichbox{#1}%
      \def\tikzext@layers@setbox@which{\global\setbox}%
    \fi
  },
  on layer/.style={
    /tikz/ext/layers/in box/.expand once=\csname pgf@layerbox@#1\endcsname
  },
  discard/.style={
    /tikz/ext/layers/on layer=discard
  },
  patch/.is choice,
  patch/all/.style={
    /tikz/ext/layers/patch/@common patch,
    /tikz/ext/layers/patch/node,
    /tikz/ext/layers/patch/matrix,
    /tikz/ext/layers/patch/pic,
    /tikz/ext/layers/patch/edge,
  },
  patch/@common patch/.style={
    /tikz/behind path/.append code      =\let\tikzext@layers@setbox@which\setbox,
    /tikz/in front of path/.append code =\let\tikzext@layers@setbox@which\setbox,
    /tikz/ext/layers/patch/@common patch/.code=%
  },
  patch/node/.style={%
    /utils/exec=%
      \expandafter\expandafter\expandafter\def
      \expandafter\expandafter\expandafter\tikz@fig@continue
      \expandafter\expandafter\expandafter{\expandafter\tikzext@layers@swapfirstsetbox\tikz@fig@continue\tikzext@stop},%
    /tikz/ext/layers/patch/@common patch,
    /tikz/ext/layers/patch/node/.code=\pgfutil@packagewarning{tikz-ext}{You can't apply the layers patch twice.},
    /tikz/ext/discard node/.style ={/tikz/ext/layers/discard},
    /tikz/ext/node on layer/.style={/tikz/ext/layers/on layer={##1}},
    /tikz/ext/node in box/.style  ={/tikz/ext/layers/in box={##1}},
  },
  patch/matrix/.style={%
    /utils/exec=%
      \expandafter\expandafter\expandafter\def
      \expandafter\expandafter\expandafter\tikz@do@matrix
      \expandafter\expandafter\expandafter{\expandafter\tikzext@layers@swapfirstsetbox\tikz@do@matrix\tikzext@stop},%
    /tikz/ext/layers/patch/@common patch,
    /tikz/ext/layers/patch/matrix/.code=\pgfutil@packagewarning{tikz-ext}{You can't apply the layers patch twice.},
    /tikz/execute at begin matrix={% in case patched node/edge/pic is used inside matrix
      \let\tikzext@layers@setbox@which\setbox
      \def\tikz@whichbox{\tikz@figbox}%
      \tikzextset{layers/in box/.code=\pgfutil@packageerror{tikz-ext}{Layers inside a matrix can't be used.}}%
    },
    /tikz/ext/discard matrix/.style ={/tikz/ext/layers/discard},
    /tikz/ext/matrix on layer/.style={/tikz/ext/layers/on layer={##1}},
    /tikz/ext/matrix in box/.style  ={/tikz/ext/layers/in box={##1}},
  },
  patch/edge/.style={%
    /utils/exec=\let\tikz@do@edge\tikzext@layers@tikz@do@edge,
    /tikz/ext/layers/patch/@common patch,
    /tikz/ext/layers/patch/edge/.code=\pgfutil@packagewarning{tikz-ext}{You can't apply the layers patch twice.},
    /tikz/ext/discard edge/.style={/tikz-ext/layers/discard},
    /tikz/ext/edge on layer/.style={
      /tikz/ext/layers/on layer={##1}
    },
    /tikz/ext/edge in box/.style={/tikz/ext/layers/in box={##1}},
  },
  patch/pic/.style={%
    /utils/exec=\let\tikz@subpicture@handle@\tikzext@layers@tikz@subpicture@handle@,
    /tikz/ext/layers/patch/@common patch,
    /tikz/ext/layers/patch/pic/.code=\pgfutil@packagewarning{tikz-ext}{You can't apply the layers patch twice.},
    /tikz/ext/discard pic/.style={/tikz/ext/layers/discard},
    /tikz/ext/pic on layer/.style={/tikz/ext/layers/on layer={##1}},
    /tikz/ext/pic in box/.style={/tikz/ext/layers/in box={##1}},
  }
}
\def\tikzext@layers@tikz@do@edge{%
  \ifx\tikz@edge@macro\pgfutil@empty%
    \tikzext@layers@setbox@which\tikz@whichbox=\hbox\bgroup%
      \unhbox\tikz@whichbox%
      \hbox\bgroup
        \bgroup%
          \pgfinterruptpath%
            \pgfscope%
              \let\tikz@transform=\pgfutil@empty%
              \let\tikz@options=\pgfutil@empty%
              \tikz@clear@rdf@options%
              \let\tikz@tonodes=\tikz@collected@onpath%
              \def\tikztonodes{{\pgfextra{\tikz@node@is@a@labeltrue}\tikz@tonodes}}%
              \let\tikz@collected@onpath=\pgfutil@empty%
              \tikz@options%
              \tikz@do@rdf@pre@options%
              \pgfidscope%
              \tikz@do@rdf@post@options%
              \tikz@transform%
              \let\tikz@transform=\relax%
              % Typeset node:
              \let\tikz@after@path\pgfutil@empty%
              \tikz@atbegin@to%
                \tikz@enable@edge@quotes%
                % in case patched nodes/edges/pics/matrices are used
                \let\tikzext@layers@setbox@which\setbox
                \def\tikz@whichbox{\tikz@figbox}%
                \path[style=every edge]\expandafter[\tikz@@to@local@options](\tikztostart)\tikz@to@path
                \pgfextra{\global\let\tikz@after@path@smuggle=\tikz@after@path};%
              \tikz@atend@to%
              \endpgfidscope%
            \endpgfscope%
          \endpgfinterruptpath%
        \egroup
      \egroup%
    \egroup%
    \ifx\tikzext@layers@setbox@which\setbox
      \global\setbox\tikz@tempbox=\box\tikz@whichbox%
      \expandafter\endgroup%
      \expandafter\setbox\tikz@whichbox=\box\tikz@tempbox%
    \else
      \endgroup
    \fi
  \else%
      \expandafter\expandafter\expandafter\tikz@edge@macro%
      \expandafter\expandafter\expandafter{\expandafter\tikz@@to@local@options\expandafter}\expandafter{\tikz@collected@onpath}%
    \endgroup%
    \let\tikz@after@path@smuggle=\pgfutil@empty%
  \fi%
  \expandafter\tikz@scan@next@command\tikz@after@path@smuggle%
}%
\def\tikzext@layers@tikz@subpicture@handle@#1{
  \pgfkeys{/tikz/pics/.cd,#1}%
  \tikz@node@transformations%
  \let\tikz@transform=\relax%
  \let\tikz@picmode\tikz@mode%
  \tikzset{name prefix ../.style/.expanded={/tikz/name prefix=\pgfkeysvalueof{/tikz/name prefix}}}%
  \ifx\tikz@fig@name\pgfutil@empty\else%
    \tikzset{name prefix/.expanded=\tikz@fig@name}%
  \fi%
  \pgfkeysvalueof{/tikz/pics/setup code}%
  \pgfkeysgetvalue{/tikz/pics/code}{\tikz@pic@code}
  \ifx\tikz@pic@code\pgfutil@empty\else% Qrr: next line
  \tikzext@layers@setbox@which\tikz@whichbox=\hbox\bgroup%
    \unhbox\tikz@whichbox%
      \hbox\bgroup
        \bgroup%
          \pgfinterruptpath%
            \pgfscope%
              \tikz@options%
              % Qrr: in case patched node/matrix/edge/pic is used inside the pic
              \let\tikzext@layers@setbox@which\setbox
              \def\tikz@whichbox{\tikz@figbox}%
              \setbox\tikz@figbox=\box\pgfutil@voidb@x%
              \setbox\tikz@figbox@bg=\box\pgfutil@voidb@x%
              \tikz@atbegin@scope%
              \scope[every pic/.try]%
                \tikz@pic@code%
              \endscope%
              \tikz@atend@scope%
            \endpgfscope%
          \endpgfinterruptpath%
        \egroup
      \egroup%
    \egroup%
  \fi%
  \pgfkeysgetvalue{/tikz/pics/foreground code}{\tikz@pic@code}
  \ifx\tikz@pic@code\pgfutil@empty\else%
  \unless\ifx\tikzext@layers@setbox@which\setbox
    \pgfutil@packagewarning{tikz-ext}{Only the main pic code can be placed on selective layers.}
  \fi
  \setbox\tikz@figbox=\hbox\bgroup%
    \unhbox\tikz@figbox%
      \hbox\bgroup
        \bgroup%
          \pgfinterruptpath%
            \pgfscope%
              \tikz@options%
              \setbox\tikz@figbox=\box\pgfutil@voidb@x%
              \setbox\tikz@figbox@bg=\box\pgfutil@voidb@x%
              \tikz@atbegin@scope%
              \scope[every front pic/.try]%
                \tikz@pic@code%
              \endscope%
              \tikz@atend@scope%
            \endpgfscope%
          \endpgfinterruptpath%
        \egroup
      \egroup%
    \egroup%
  \fi%
  \pgfkeysgetvalue{/tikz/pics/background code}{\tikz@pic@code}
  \ifx\tikz@pic@code\pgfutil@empty\else%
  \unless\ifx\tikzext@layers@setbox@which\setbox
    \pgfutil@packagewarning{tikz-ext}{Only the main pic code can be placed on selective layers.}
  \fi
  \setbox\tikz@figbox@bg=\hbox\bgroup%
    \unhbox\tikz@figbox@bg%
      \hbox\bgroup
        \bgroup%
          \pgfinterruptpath%
            \pgfscope%
              \tikz@options%
              \setbox\tikz@figbox=\box\pgfutil@voidb@x%
              \setbox\tikz@figbox@bg=\box\pgfutil@voidb@x%
              \tikz@atbegin@scope%
              \scope[every behind pic/.try]%
                \tikz@pic@code%
              \endscope%
              \tikz@atend@scope%
            \endpgfscope%
          \endpgfinterruptpath%
        \egroup
      \egroup%
    \egroup%
  \fi%
  \tikz@node@finish%
}%
\endinput

%%% Compatibility
\tikzextset{
  /tikz-ext/compat/add library={layers}{pre 0.6},
  compat/layers/warn/.append code=%
    % \pgfkeyssetvalue{/tikz/ext/compat/layers@}{3}%
    \tikzextutil@compatwarning@key{layers}{pre 0.6}{/tikz-ext/patch}      {/tikz/ext/layers/patch}%
    \tikzextutil@compatwarning@key{layers}{pre 0.6}{/tikz/node on layer}  {/tikz/ext/node on layer}%
    \tikzextutil@compatwarning@key{layers}{pre 0.6}{/tikz/matrix on layer}{/tikz/ext/matrix on layer}%
    \tikzextutil@compatwarning@key{layers}{pre 0.6}{/tikz/edge on layer}  {/tikz/ext/edge on layer}%
    \tikzextutil@compatwarning@key{layers}{pre 0.6}{/tikz/pic on layer}   {/tikz/ext/pic on layer},
  compat/layers/pre 0.6/.append code=%
    % \pgfkeyssetvalue{/tikz/ext/compat/layers@}{2}%
    \pgfkeysdef{/tikz-ext/patch}      {\pgfkeysvalueof{/tikz/ext/layers/patch/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/node on layer}  {\pgfkeysvalueof{/tikz/ext/node on layer/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/matrix on layer}{\pgfkeysvalueof{/tikz/ext/matrix on layer/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/edge on layer}  {\pgfkeysvalueof{/tikz/ext/edge on layer/.@cmd}##1\pgfeov}%
    \pgfkeysdef{/tikz/pic on layer}   {\pgfkeysvalueof{/tikz/ext/pic on layer/.@cmd}##1\pgfeov},
  % compat/layers/0.6/.append code=\pgfkeyssetvalue{/tikz/ext/compat/layers@}{1},
  % compat/layers/newest/.append code=\pgfkeyssetvalue{/tikz/ext/compat/layers@}{0},
  compat/layers/\pgfkeysvalueof{/tikz/ext/compat/layers}%
}
\endinput