% Language definition file (part of polyglossia v2.6 -- 2025/06/30)
%
\ProvidesFile{gloss-mongolian.ldf}[polyglossia: module for Mongolian]

\RequirePackage{xpg-cyrillicnumbers}

\PolyglossiaSetup{mongolian}{
  bcp47=mn,
  bcp47-language=mn,
  bcp47-script=Cyrl,
  script=Cyrillic,
  scripttag=cyrl,
  langtag=MNG,
  hyphennames={mongolian},
  hyphenmins={2,2},
  frenchspacing=true,
  localnumeral=mongoliannumerals,
  Localnumeral=Mongoliannumerals
}

% BCP-47 compliant aliases
\setlanguagealias*{mongolian}{mn}

% This file is derived from mongolian-babel. which
% provides support for Mongolian (Khalkha variety)
% with Cyrillic script
% TODO: Implement script=mongolian and maybe other
%       variants of Mongolian

\newif\ifcyrillic@numerals
\newif\ifcyrillic@asbuk@numerals

\DeclareKeys[polyglossia/gloss/mongolian]
  {
    babelshorthands.if = mongolian@babelshorthands,
    babelshorthands.default:n = true,
    numerals.choice:,
    numerals / arabic.code =
      {%
        \cyrillic@numeralsfalse
        \cyrillic@asbuk@numeralsfalse
      },
    numerals / cyrillic.code =
      {%
        \cyrillic@numeralstrue
        \cyrillic@asbuk@numeralsfalse
      },
    numerals / cyrillic-trad.code =
      {%
        \cyrillic@numeralstrue
        \cyrillic@asbuk@numeralsfalse
      },
    numerals / cyrillic-alph.code =
      {%
        \cyrillic@numeralstrue
        \cyrillic@asbuk@numeralstrue
      },
    numerals.default:n = arabic,
    numerals/unknown.code = \xpg@warning{Unknown Mongolian numerals value `#1'}
  }

\ifsystem@babelshorthands
  \SetGlossOptions{mongolian}{babelshorthands=true}
  % Register default options
  \InitializeGlossOptions{mongolian}{babelshorthands=true,numerals=arabic}
\else
  \SetGlossOptions{mongolian}{babelshorthands=false}
  % Register default options
  \InitializeGlossOptions{mongolian}{babelshorthands=false,numerals=arabic}
\fi

\InitializeBabelShorthands

\def\mongolian@shorthands{%
  \xpg@activate@shorthands%
  \def\language@group{mongolian}%
  \declare@shorthand{mongolian}{"`}{„}%
  \declare@shorthand{mongolian}{"'}{“}%
  \declare@shorthand{mongolian}{"<}{«}%
  \declare@shorthand{mongolian}{">}{»}%
  \declare@shorthand{mongolian}{""}{\hskip\z@skip}%
  \declare@shorthand{mongolian}{"~}{\textormath{\leavevmode\hbox{-}}{-}}%
  \declare@shorthand{mongolian}{"=}{\nobreak-\hskip\z@skip}%
  \declare@shorthand{mongolian}{"|}{\textormath{\nobreak\discretionary{-}{}{\kern.03em}\allowhyphens}{}}%
  \declare@shorthand{mongolian}{"-}{%
     \def\mongolian@sh@tmp{%
       \if\mongolian@sh@next-\expandafter\mongolian@sh@emdash%
       \else\expandafter\mongolian@sh@hyphen\fi%
     }%
     \futurelet\mongolian@sh@next\mongolian@sh@tmp%
  }%
  \def\mongolian@sh@hyphen{%
    \nobreak\-\bbl@allowhyphens}%
  \def\mongolian@sh@emdash##1##2{\cdash-##1##2}%
  \def\cdash##1##2##3{\def\tempx@{##3}%
  \def\tempa@{-}\def\tempb@{~}\def\tempc@{*}%
   \ifx\tempx@\tempa@\@Acdash\else
    \ifx\tempx@\tempb@\@Bcdash\else
     \ifx\tempx@\tempc@\@Ccdash\else
      \errmessage{Wrong usage of cdash}\fi\fi\fi}%
  \def\@Acdash{\ifdim\lastskip>\z@\unskip\nobreak\hskip.2em\fi
    \cyrdash\hskip.2em\ignorespaces}%
  \def\@Bcdash{\leavevmode\ifdim\lastskip>\z@\unskip\fi
   \nobreak\cyrdash\penalty\exhyphenpenalty\hskip\z@skip\ignorespaces}%
  \def\@Ccdash{\leavevmode
   \nobreak\cyrdash\nobreak\hskip.35em\ignorespaces}%
  \ifx\cyrdash\undefined
    \def\cyrdash{\leavevmode\hbox to.8em{\textendash\hss\textendash}}%
  \fi
  \declare@shorthand{mongolian}{",}{\nobreak\hskip.2em\ignorespaces}%
}

\def\nomongolian@shorthands{%
  \xpg@deactivate@shorthands%
}

% Taken from babel-mongolian
\def\captionsmongolian{%
   \def\prefacename{Өмнөх үг}%
   \def\refname{Ашигласан ном}%
   \def\abstractname{Удиртгал}%
   \def\bibname{Номзүй}%
   \def\chaptername{Бүлэг}%
   \def\appendixname{Хавсралт}%
   \@ifundefined{thechapter}%
     {\def\contentsname{Агуулга}}%
     {\def\contentsname{Гарчиг}}%
   \def\listfigurename{Зургийн жагсаалт}%
   \def\listtablename{Хүснэгтийн жагсаалт}%
   \def\indexname{Товъёг}%
   \def\authorname{Нэрийн хэлхээ}%
   \def\figurename{Зураг}%
   \def\tablename{Хүснэгт}%
   \def\partname{Хэсэг}%
   \def\enclname{Ишлэл}%
   \def\ccname{э.с.}%
   \def\headtoname{}%
   \def\pagename{тал}%
   \def\seename{талд үз}%
   \def\alsoname{мөн талд үз}%
   \def\proofname{Баталгаа}%
}

\def\datemongolian{%
  \def\today{\number\year~оны \ifcase\month\or
  1-Ñ€\or 2-Ñ€\or 3-Ñ€\or 4-Ñ€\or 5-Ñ€\or 6-Ñ€\or
  7-Ñ€\or 8-Ñ€\or 9-Ñ€\or 10-Ñ€\or 11-Ñ€\or 12-Ñ€\fi
  ~сарын \number\day}}

\newcommand{\mongoliannumerals}[2]{\mongoliannumber{#2}}
\newcommand{\Mongoliannumerals}[2]{\Mongoliannumber{#2}}

\def\mongoliannumber#1{%
  \ifcyrillic@numerals
    \ifcyrillic@asbuk@numerals
      \mongolian@asbuk@alph{#1}%
    \else
      \cyr@alph{#1}%
    \fi
  \else
    \number#1%
  \fi%
}

\def\Mongoliannumber#1{%
  \ifcyrillic@numerals
    \ifcyrillic@asbuk@numerals
      \mongolian@asbuk@Alph{#1}%
    \else
      \cyr@Alph{#1}%
    \fi
  \else
    \number#1%
  \fi%
}

\let\mongoliannumeral=\mongoliannumber
\let\Mongoliannumeral=\Mongoliannumber

\def\Asbuk#1{\expandafter\mongolian@asbuk@Alph\csname c@#1\endcsname}
\def\asbuk#1{\expandafter\mongolian@asbuk@alph\csname c@#1\endcsname}

\def\AsbukTrad#1{\expandafter\cyr@Alph\csname c@#1\endcsname}
\def\asbukTrad#1{\expandafter\cyr@alph\csname c@#1\endcsname}

% This is a poor man's cyrillic alphanumeric. It just uses the alphabet and
% thus ends at 30.
\def\mongolian@asbuk@Alph#1{\ifcase#1\or
   А\or Б\or В\or Г\or Д\or Е\or Ж\or
   З\or И\or К\or Л\or М\or Н\or О\or
   П\or Р\or С\or Т\or У\or Ф\or Х\or
   Ц\or Ч\or Ш\or Щ\or Э\or Ю\or Я%
   \else\xpg@ill@value{#1}{mongolian@asbuk@Alph}\fi%
}

\def\mongolian@asbuk@alph#1{\ifcase#1\or
   а\or б\or в\or г\or д\or е\or ж\or
   з\or и\or к\or л\or м\or н\or о\or
   п\or р\or с\or т\or у\or ф\or х\or
   ц\or ч\or ш\or щ\or э\or ю\or я%
   \else\xpg@ill@value{#1}{mongolian@asbuk@alph}\fi%
}

\def\mongolian@numbers{%
   \let\latin@alph\@alph
   \let\latin@Alph\@Alph
   \ifcyrillic@numerals%
     \def\mongolian@alph##1{\expandafter\mongoliannumeral\expandafter{\the##1}}%
     \def\mongolian@Alph##1{\expandafter\Mongoliannumeral\expandafter{\the##1}}%
      \let\@alph\mongolian@alph%
      \let\@Alph\mongolian@Alph%
   \fi
}

\def\nomongolian@numbers{%
   \let\@alph\latin@alph%
   \let\@Alph\latin@Alph%
}

\def\noextras@mongolian{%
   \ifcyrillic@numerals\nomongolian@numbers\fi%
   \ifmongolian@babelshorthands\nomongolian@shorthands\fi%
}

\def\blockextras@mongolian{%
   \ifcyrillic@numerals\mongolian@numbers\else\nomongolian@numbers\fi%
   \ifmongolian@babelshorthands\mongolian@shorthands\else\nomongolian@shorthands\fi%
}

\def\inlineextras@mongolian{%
   \ifmongolian@babelshorthands\mongolian@shorthands\else\nomongolian@shorthands\fi%
}

\endinput