% datemultical-engine.sty
% Date-conversion engine for Lua and TeX backends
%
% Copyright (C) 2026 Amer Amikhteh
%
% This file is part of the datemultical package.
%
% The datemultical 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.
%
% The datemultical 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. See the
% LaTeX Project Public License for more details.
%
% You should have received a copy of the LaTeX Project Public License
% along with the datemultical package. If not, see
% <https://www.latex-project.org/lppl/>.
%
% This work is maintained by Amer Amikhteh.
%
\NeedsTeXFormat{LaTeX2e}

\ProvidesPackage{datemultical-engine}[2026/09/17 v0.1.0 Date-conversion engine for Lua and TeX backends]

\RequirePackage{expl3}
\RequirePackage{xparse}
\RequirePackage{iftex}

\ExplSyntaxOn

% -------------------------------------------------
% State variables
% -------------------------------------------------

\bool_new:N \g_dmc_native_bool

% Without an explicit option, use the modern Lua backend
% with LuaLaTeX/LuaHBTeX (native = false) and the native
% TeX backend otherwise (native = true).

\ifLuaTeX
  \bool_gset_false:N \g_dmc_native_bool
\else
  \bool_gset_true:N \g_dmc_native_bool
\fi

% =================================================
% JDN -> weekday
% Expandable
% Returns 0..6
% 0 = Sunday, 1 = Monday, ..., 6 = Saturday
% =================================================

\cs_new:Npn \dmc_jdn_to_weekday:n #1
{
  \int_mod:nn { #1 + 1 } { 7 }
}

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

\DeclareOption{native}
{
  \bool_gset_true:N \g_dmc_native_bool
}

\DeclareOption*
{
  \PackageWarning{datemultical-engine}
  {Unknown option `\CurrentOption' ignored}
}

\ProcessOptions\relax

% -------------------------------------------------
% Load selected backend
% -------------------------------------------------

\bool_if:NTF \g_dmc_native_bool
{
  \PackageInfo{datemultical-engine}
  {Using the native backend}

  \input{datemultical-engine-gr.def}
  \input{datemultical-engine-sh.def}
  \input{datemultical-engine-lh.def}
  \input{datemultical-engine-jdn.def}
}
{
  \PackageInfo{datemultical-engine}
  {Using the modern Lua backend}

  \directlua{
    require("datemultical-engine")
  }
}

\ExplSyntaxOff

\endinput