\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{turkce-sayi}[2026/06/11 v1.0 Turkish suffix harmony for numbers]
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% See https://www.latex-project.org/lppl.txt
%% LPPL maintenance status: maintained.
%% Maintainer: Sonat Suer <sonatsuer@gmail.com>

\RequirePackage{iftex}
\ifluatex\else
  \PackageError{turkce-sayi}{LuaTeX required. Compile with lualatex.}{}%
  \expandafter\endinput
\fi

\RequirePackage{luatexbase}
\RequirePackage{refcount}

\directlua{trk = require('turkce-sayi')}

% \trksuffix{<integer>}{<suffix>}
%   Outputs only the apostrophe + transformed suffix (no digit).
\newcommand*{\trksuffix}[2]{%
  \directlua{trk.output_suffix(#1, "\luatexluaescapestring{#2}")}%
}

% \trkappend{<integer>}{<suffix>}
%   Outputs the digit, then the apostrophe + transformed suffix.
%   Use with literal integers or \the\value{counter} expressions.
\newcommand*{\trkappend}[2]{%
  #1\directlua{trk.output_suffix(#1, "\luatexluaescapestring{#2}")}%
}

% \trkpageref{<label>}{<suffix>}
%   Like \pageref{label} but with the correct Turkish suffix appended.
\newcommand*{\trkpageref}[2]{%
  \pageref{#1}%
  \directlua{trk.output_suffix(\getpagerefnumber{#1}, "\luatexluaescapestring{#2}")}%
}

% \trkref{<label>}{<suffix>}
%   Like \ref{label} but with the correct Turkish suffix appended.
\newcommand*{\trkref}[2]{%
  \ref{#1}%
  \directlua{trk.output_suffix("\getrefnumber{#1}", "\luatexluaescapestring{#2}")}%
}

% \trkbibappend{<integer>}{<suffix>}
%   Outputs [n] then the transformed suffix. Use when the citation number
%   is known directly (analogous to \trkappend for bibliography).
\newcommand*{\trkbibappend}[2]{%
  [#1]\directlua{trk.output_suffix(#1, "\luatexluaescapestring{#2}")}%
}

% \trkbibref{<keys>}{<suffix>}
%   Like \cite{keys} but with the correct Turkish suffix based on the last
%   key in the (possibly comma-separated) list.  Works with standard numeric
%   bibliography (\bibitem); requires two compilation passes.
\newcommand*{\trkbibref}[2]{%
  \cite{#1}%
  \directlua{trk.bib_output_suffix("\luatexluaescapestring{#1}", "\luatexluaescapestring{#2}")}%
}

% \trkmanual{<display>}{<literal-suffix>}
%   Escape hatch: outputs display + literal-suffix with no transformation.
%   Use when the automatic rules produce wrong results.
\newcommand*{\trkmanual}[2]{#1#2}

% \trksetoverride{<integer>}{<algo-output>}{<replacement>}
%   When the algorithm would produce <algo-output> for <integer>, use
%   <replacement> instead. Other suffixes for the same integer are unaffected.
\newcommand*{\trksetoverride}[3]{%
  \directlua{trk.set_override(#1, "\luatexluaescapestring{#2}", "\luatexluaescapestring{#3}")}%
}

% \trkremoveoverride{<integer>}{<algo-output>}
%   Remove the override for the given integer / algo-output pair.
\newcommand*{\trkremoveoverride}[2]{%
  \directlua{trk.remove_override(#1, "\luatexluaescapestring{#2}")}%
}

\DeclareOption{debug}{\directlua{trk.debug = true}}
\ProcessOptions\relax
