% 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.
%

\usetikzlibrary{calendar}
\pgfutil@usemodule{pgfcalendar-ext}

% Shift between days

\def\tikz@lib@cal@xshift{\pgfkeysvalueof{/tikz/day xshift}}
\def\tikz@lib@cal@yshift{\pgfkeysvalueof{/tikz/day yshift}}
\pgfkeyslet{/tikz/day xshift/.@cmd}\@undefined
\pgfkeyslet{/tikz/day xshift/.@body}\@undefined
\pgfkeyslet{/tikz/day xshift/.@def}\@undefined
\pgfkeyslet{/tikz/day yshift/.@cmd}\@undefined
\pgfkeyslet{/tikz/day yshift/.@body}\@undefined
\pgfkeyslet{/tikz/day yshift/.@def}\@undefined
\tikzset{day xshift/.initial=3ex,day yshift/.initial=3.5ex}

% Shift between months

\def\tikz@lib@cal@month@xshift{\pgfkeysvalueof{/tikz/month xshift}}
\def\tikz@lib@cal@month@yshift{\pgfkeysvalueof{/tikz/month yshift}}
\pgfkeyslet{/tikz/month xshift/.@cmd}\@undefined
\pgfkeyslet{/tikz/month xshift/.@body}\@undefined
\pgfkeyslet{/tikz/month xshift/.@def}\@undefined
\pgfkeyslet{/tikz/month yshift/.@cmd}\@undefined
\pgfkeyslet{/tikz/month yshift/.@body}\@undefined
\pgfkeyslet{/tikz/month xshift/.@def}\@undefined
\tikzset{month xshift/.initial=9ex,month yshift/.initial=9ex}

% Internal option for storing the "width" of a calendar as a number of
% days

\def\tikz@lib@cal@width{\pgfkeysvalueof{/tikz/tikz@lib@cal@width}}
\pgfkeyslet{/tikz/tikz@lib@cal@width/.@cmd}\@undefined
\pgfkeyslet{/tikz/tikz@lib@cal@width/.@body}\@undefined
\pgfkeyslet{/tikz/tikz@lib@cal@width/.@def}\@undefined
\tikzset{tikz@lib@cal@width/.initial=1}

% Weeks

\def\tikzextweekcode{\pgfkeysvalueof{/tikz/ext/week code}}
\def\tikzextweektext{\pgfkeysvalueof{/tikz/ext/week text}}

\tikzextset{
  week code/.initial={%
    \node[ext/every week]{\pgfkeysvalueof{/tikz/ext/week text}};},
  week text/.initial={\%n=},
  every week/.code=,
  week label left/.style={
    /tikz/ext/every week/.append style={
      /tikz/anchor=base east,
      /tikz/xshift=-1.25*\pgfkeysvalueof{/tikz/day xshift}},
    /tikz/execute before day scope=%
      \ifdate{Monday, equals=\pgfcalendarbeginiso}{\tikzextweekcode}{}}}

%
% Overwriting original \tikz@lib@cal@if@else@code
%
\def\tikz@lib@cal@if@else@code#1#2#3{%
  \tikzext@cal@doif\tikz@lib@cal@ifs{\ifdate{#1}{#2}{#3}}%
  \tikz@lib@cal@scanner%
}

\let\tikzext@cal@doif\tikzextutil@addto@macro
\tikzset{execute at begin day scope=\let\tikzext@cal@doif\pgfutil@secondoftwo}


\pgfmathdeclarefunction{ext_weeksinmonthofyear}{3}{% #1 = first weekday, #2 = month, #3 = year
  \begingroup
    \pgfmathlastdayinmonthofyear@{#2}{#3}%
    \pgfmathadd@{#1}{\pgfmathresult}%
    \pgfmathdivide@{\pgfmathresult}{7}%
    \pgfmathceil@{\pgfmathresult}%
    \pgfmathint@{\pgfmathresult}%
    \pgfmathsmuggle\pgfmathresult
  \endgroup
}
\pgfmathdeclarefunction{ext_lastdayinmonthofyear}{2}{% #1 = month, #2 = year
  \begingroup
    \pgfextcalendar@getlastYMX{#2}{#1}{\pgfutil@tempcnta}%
    \edef\pgfmathresult{\the\pgfutil@tempcnta}%
    \pgfmathsmuggle\pgfmathresult
  \endgroup
}
\expandafter\let\expandafter\pgfmathextweeksinmonthofyear\csname pgfmathext_weeksinmonthofyear\endcsname
\expandafter\let\expandafter\pgfmathextweeksinmonthofyear@\csname pgfmathext_weeksinmonthofyear@\endcsname
\expandafter\let\expandafter\pgfmathextlastdayinmonthofyear\csname pgfmathext_lastdayinmonthofyear\endcsname
\expandafter\let\expandafter\pgfmathextlastdayinmonthofyear@\csname pgfmathext_lastdayinmonthofyear@\endcsname

%%% Compatibility
\tikzextset{
  compat/pgfcalendar-ext/warn/.append code={%
    \tikzextutil@compatwarning@cmd{calendar-plus}{pre 0.6}{pgfmathlastdayinmonthofyear}\pgfmathextlastdayinmonthofyear\tikzextutil@gobblethree
    \tikzextutil@compatwarning@cmd{calendar-plus}{pre 0.6}{pgfmathweeksinmonthofyear}\pgfmathextweeksinmonthofyear\pgfutil@gobbletwo
    \pgfutil@for\tikzext@temp:=Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec,%
      leap year,and,not,week of month,week of month',calendar week of month,%
      calendar week of month',yesterday,week\do{%
      \pgfkeys@expanded{\noexpand\tikzextutil@compatwarning@key
        {pgfcalendar-ext}{pre 0.6}{/pgf/calendar/\tikzext@temp}{/pgf/calendar/ext/\tikzext@temp}%
      }%
    }%
    \pgfkeyssetvalue{/pgf/calendar/leap year/.@def}{\pgfcalendarifdateyear}%
    \pgfkeyssetvalue{/pgf/calendar/calendar week of month/.@def}{1}%
    \pgfkeyssetvalue{/pgf/calendar/calendar week of month'/.@def}{1}%
    \tikzextutil@compatwarning@cmd{pgfcalendar-ext}{pre 0.6}
      {pgfcalendarjulianyeartoweek}\pgfextcalendarjulianyeartoweek\tikzextutil@gobblethree
  },
  compat/pgfcalendar-ext/pre 0.6/.append code={%
    \pgfutil@for\tikzext@temp:=Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec,%
      leap year,and,not,week of month,week of month',calendar week of month,%
      calendar week of month',yesterday,week\do{%
      \pgfkeysedef{/pgf/calendar/\tikzext@temp}{%
        \noexpand\pgfkeysvalueof{/pgf/calendar/ext/\tikzext@temp/.@cmd}##1\noexpand\pgfeov
      }%
      \let\pgfcalendarjulianyeartoweek\pgfextcalendarjulianyeartoweek
      \let\pgfmathweeksinmonthofyear\pgfmathextweeksinmonthofyear
      \let\pgfmathweeksinmonthofyear@\pgfmathextweeksinmonthofyear@
      \let\pgfmathlastdayinmonthofyear\pgfmathextlastdayinmonthofyear
      \let\pgfmathlastdayinmonthofyear@\pgfmathextlastdayinmonthofyear@
      \def\pgfcalendarcurrentweek{\pgfextcalendarcurrentweek}%
      \def\pgfcalendarifdateweek{\pgfextcalendarifdateweek}%
    }%
  },
  compat/pgfcalendar-ext/\pgfkeysvalueof{/tikz/ext/compat/pgfcalendar-ext}%
}
\endinput