% Babelbidi: Bidirectional typesetting support for Babel.
%
% Copyright (C) 2026 Amer Amikhteh
%
% This package is free software: you may redistribute it and/or modify
% it under the terms of the LaTeX Project Public License as published
% by the LaTeX Project, either version 1.3c of the License, or
% (at your option) any later version.
%
% This package is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
%
% The full license text is available at:
% https://www.latex-project.org/lppl/
%
% This work is "maintained" by Amer Amikhteh.
%
% The current maintainer of this work is Amer Amikhteh.
%
% This package consists of the file babelbidi.sty and the files
% distributed with it.

\NeedsTeXFormat{LaTeX2e}

\ProvidesPackage{babelbidi}
[2026/10/26 v0.4.0 Bidi extensions for Babel]

\RequirePackage{iftex}
\RequirePackage{xkeyval}

% ------------------------------------------------
% Package options
% ------------------------------------------------

\define@key{bbd}{bidi}{%
    \gdef\bbd@bidi{#1}%
}

\define@key{bbd}{layout}{%
    \edef\bbd@layout{#1}%
}

\def\bbd@layout{}

\ifPDFTeX
    \setkeys{bbd}{bidi=default}
\fi

\ifXeTeX
    \setkeys{bbd}{bidi=bidi-r}
\fi

\ifLuaTeX
    \setkeys{bbd}{%
        bidi=basic,
        layout=counters graphics footnotes lists
    }
\fi

\DeclareOptionX{bidi}{%
    \setkeys{bbd}{bidi=#1}%
}

\DeclareOptionX{layout}{%
    \setkeys{bbd}{layout=#1}%
}

\DeclareOptionX*{%
    \edef\bbd@options{%
        \bbd@options,\CurrentOption
    }%
}

\def\bbd@options{}

% Cache Babel's repeated font mappings within each LuaTeX run.
\newif\ifbbd@font@cache
\bbd@font@cachetrue

\DeclareOptionX{fontcache}[true]{%
    \edef\bbd@fontcache@option{#1}%
    \def\bbd@fontcache@true{true}%
    \def\bbd@fontcache@false{false}%
    \ifx\bbd@fontcache@option\bbd@fontcache@true
        \bbd@font@cachetrue
    \else\ifx\bbd@fontcache@option\bbd@fontcache@false
            \bbd@font@cachefalse
        \else
            \PackageError{babelbidi}{Invalid fontcache value '#1'}%
            {Use fontcache=true or fontcache=false.}%
        \fi\fi
}

\DeclareOptionX{code}{%
    \PassOptionsToPackage{code=#1}{babelbidi-lang}%
}

\DeclareOptionX{latin}{%
    \PassOptionsToPackage{latin=#1}{babelbidi-lang}%
}

\DeclareOptionX{ltr}{%
    \PassOptionsToPackage{ltr=#1}{babelbidi-lang}%
}

\DeclareOptionX{rtl}{%
    \PassOptionsToPackage{rtl=#1}{babelbidi-lang}%
}

\ProcessOptionsX\relax

% ------------------------------------------------
% Beamer class detection
% ------------------------------------------------

\newif\ifbbd@beamer
\@ifclassloaded{beamer}{\bbd@beamertrue}{}

\newif\ifbbd@beamerrl
\@ifclassloaded{beamer-rl}{\bbd@beamerrltrue}{}

% ------------------------------------------------
% Arabic and Beamer compatibility
% ------------------------------------------------

\@ifundefined{if@rl}{%
    \newif\if@rl
}{}

\ifPDFTeX
    \ifbbd@beamer
        % \addto\extraarabic{
        \providecommand{\labelitemiv}
        {\if@rl\protect $-$ %
            \else\protect\textperiodcentred\fi}
        \providecommand{\labelitemi}
        {\if@rl\protect\textLR{$\bullet$}
            \else\protect\textbullet\fi}

        \providecommand{\labelitemii}
        {\if@rl\protect\textLR{$\circ$} %
            \else\protect\textLR{ \normalfont\bfseries\textendash}\fi}

        \providecommand{\labelitemiii}
        {\if@rl\protect\textLR{$*$} %
            \else\protect\textasteriskcentered\fi}

        \providecommand{\labelenumi}
        {\if@rl(\I{\number\c@enumi}).%
            \else{\number\c@enumi}.\fi}

        \providecommand{\labelenumii}
        {\if@rl\abjadnumeral{\number\c@enumii}. %
            \else({\number\c@enumii})\fi}

        \providecommand{\labelenumiii}
        {\if@rl\I{\number\c@enumiii}$\space-\space$ %
            \else({\number\c@enumiii})\fi}

        \providecommand{\labelenumiv}
        {\if@rl\abjadnumeral{\number\c@enumiv}$\space-\space$ %
            \else({\number\c@enumiv})\fi}
        % }
    \fi \fi

% ------------------------------------------------
% Engine setup
% ------------------------------------------------

\ifPDFTeX

    \RequirePackage[bidi=\bbd@bidi,\bbd@options]{babel}
\fi

\ifXeTeX
    \RequirePackage[
        bidi=\bbd@bidi,
        \bbd@options
    ]{babel}
\fi

\ifLuaTeX
    \ifbbd@beamerrl
    \else
        \RequirePackage[
            bidi=\bbd@bidi,
            layout=\bbd@layout,
            \bbd@options
        ]{babel}
    \fi
\fi

% Babel's language metadata must be ready before selecting the four aliases.
\RequirePackage{babelbidi-lang}

% ------------------------------------------------
% Keep counter direction markup out of label keys and link targets
% ------------------------------------------------

\long\def\bbd@firstofone#1{#1}

\AddToHook{begindocument/before}{%
    \@ifundefined{babelsublr}{}{%
        \let\bbd@label\label
        \protected\def\label#1{%
            \begingroup
            \let\babelsublr\bbd@firstofone
            \let\babelsubrl\bbd@firstofone
            \edef\bbd@tmp{#1}%
            % Restore display formatting before writing the reference value.
            \expandafter\endgroup
            \expandafter\bbd@label\expandafter{\bbd@tmp}%
        }
        % Hyperref runs this hook inside its link-target group.
        \AddToHook{makelinktarget}{%
            \let\babelsublr\bbd@firstofone
            \let\babelsubrl\bbd@firstofone
        }%
    }
}


% ------------------------------------------------
% Load package components
% ------------------------------------------------

% Beamer box compatibility.
\ifbbd@beamer
    \ifbbd@beamerrl
    \else
        \ifLuaTeX \input{babelbidi-beamer.def} \fi
    \fi
\fi

\input{babelbidi-api.def}
\input{babelbidi-minted.def}

\ifLuaTeX  \input{babelbidi-onchar.def} \fi
\ifLuaTeX
    \input{babelbidi-fontfallback.def}
\else
    \NewDocumentCommand{\fontfallback}{m m}{%
        \PackageWarning{babelbidi}{Font fallback requires LuaLaTeX; declaration ignored}%
    }
\fi
\ifTUTeX
    \input{babelbidi-fonts.def}
\fi

% The .def files reset @; restore it before reading the remaining code.
\makeatletter

% Delay engine patches until the preamble is complete.
\AddToHook{begindocument/before}{%
    \ifXeTeX    \input{babelbidi-xetex.def}     \fi
    \ifLuaTeX   \input{babelbidi-luatex.def}    \fi
}

% Inspect the main language after all preamble packages, end-of-preamble hooks,
% and engine setup. Keep the public interfaces ready before document hooks.
% Register the ordering here, before this one-time hook starts running.
\AddToHook{begindocument}[babelbidi/code]{%
    \input{babelbidi-latinverbatim.def}%
    \bbd@if@latin{\bbl@main@language}%
    \ifbbd@latin\else
        \input{babelbidi-code.def}%
    \fi
}
\DeclareHookRule{begindocument}{babelbidi/code}{after}{babelbidi}

% Run after babelbidi-api.def has set the main direction.
\AddToHook{begindocument}{
    \if@RTLmain
        \input{babelbidi-layout.def}
    \fi}



\makeatother

\endinput
