\ProvidesPackage{sty/acref}

\RequirePackage{etoolbox}
\RequirePackage{listofitems}

%{{{
\def\acref@instring#1#2{TT\fi\begingroup
	\edef\x{\endgroup\noexpand\in@{#1}{#2}}\x\ifin@}
%}}}

%{{{ cleveref bindings "
\def\acref@cleveref@getname#1#2{\csname #1@#2@name\endcsname}
%}}}

%{{{ acref "
\let\acref@cref\cref
\renewcommand*{\cref}[1]{\begin{otherlanguage}{english}\acref@cref{#1}\end{otherlanguage}\xspace}

\newcommand*{\acref@error@article}[2]{%
	\PackageError{acref}{undefined article for '#1' (type: '#2', page: \thepage) (possible solution is to add:
	\string\acrefarticle{#2}{}{})}{}%
}

\newcommand*{\acref@error@type}[2]{%
	\PackageError{acref}{undefined type for '#1' (type: '#2', page: \thepage) (require changes in
	package in \string\acref@article and \string\acref@Article: add
	\string\acref@printarticle{\#1}{#2}{\string\acref@art@#2}}{}
}

\newcommand*{\acref@warning@prefix}[1]{%
	\ClassWarning{acref}{Reference '#1' on page \thepage\space has unknown prefix}%
}

\newcommand*{\acref@printarticle}[3]{\expandafter\ifstrequal\expandafter{#1}{#2}{%
	\csname acref@#3@#2\endcsname\def\acref@article@isset{1}}{}%
}

\newcommand*{\acref@prefixtotype}[1]{\csname acref@prefix@#1\endcsname}
\newcommand*{\acref@crefname}[1]{\acref@cleveref@getname{cref}{#1}}

\newcommand*{\acref@article}[2]{%
	\acref@printarticle{#1}{algorithm}{#2}%
	\acref@printarticle{#1}{chapter}{#2}%
	\acref@printarticle{#1}{enumi}{#2}%
	\acref@printarticle{#1}{equation}{#2}%
	\acref@printarticle{#1}{figure}{#2}%
	\acref@printarticle{#1}{listing}{#2}%
	\acref@printarticle{#1}{section}{#2}%
	\acref@printarticle{#1}{subfigure}{#2}%
	\acref@printarticle{#1}{subsection}{#2}%
	\acref@printarticle{#1}{subsubsection}{#2}%
	\acref@printarticle{#1}{table}{#2}%
	\acref@printarticle{#1}{theorem}{#2}%
	\acref@printarticle{#1}{plural}{#2}%
}

\newcommand*{\acref@acref}[2]{%
	\if\acref@instring{,}{#1}%
		\acref@article{plural}{#2}%
	\else
		\begin{otherlanguage}{english}
			\ifcsname r@#1@cref\endcsname
				\cref@gettype{#1}{\acref@type}%
				\ifcsname acref@art@\acref@type\endcsname
					\def\acref@article@isset{}%
					\acref@article{\acref@type}{#2}%
					\expandafter\ifstrequal\expandafter{\acref@article@isset}{1}{}{\acref@error@type{#1}{\acref@type}}%
				\else
					\acref@error@article{#1}{\acref@type}
				\fi
			\else
				{% invalid reference
					\setsepchar{:}%
					\edef\@arg{#1}%
					\readlist*\@tmp{\@arg}%
					\edef\@prefix{\@tmp[1]}%
					\ifcsname acref@prefix@\@prefix\endcsname
						\edef\acref@type{\acref@prefixtotype{\@prefix}}%
						\acref@article{\acref@type}{#2}%
						\acref@crefname{\acref@type}%
					\else
						\textbf{\@prefix}%
						\acref@warning@prefix{#1}%
					\fi
				}
			\fi
		\end{otherlanguage}
	\fi
	\cref{#1}%
}

\newcommand*{\acrefarticle}[3]{%
	\expandafter\def\csname acref@art@#1\endcsname{#2}
	\expandafter\def\csname acref@Art@#1\endcsname{#3}
}

\newcommand*{\acrefprefix}[2]{%
	\expandafter\def\csname acref@prefix@#1\endcsname{#2}
}

\newcommand*{\acref}[2][n]{\acref@acref{#2}{art}}
\newcommand*{\Acref}[2][n]{\acref@acref{#2}{Art}}
%}}}

%{{{ " default configuration
\acrefarticle{algorithm}{l'}{L'}
\acrefarticle{chapter}{le }{Le }
\acrefarticle{enumi}{le }{Le }
\acrefarticle{equation}{l'}{L'}
\acrefarticle{figure}{la }{La }
\acrefarticle{listing}{la }{La }
\acrefarticle{section}{la }{La }
\acrefarticle{subfigure}{la }{La }
\acrefarticle{subsection}{la }{La }
\acrefarticle{subsubsection}{la }{La }
\acrefarticle{table}{le }{Le }
\acrefarticle{theorem}{le }{Le }

\acrefarticle{plural}{les }{Les }

\acrefprefix{alg}{algorithm}
\acrefprefix{ch}{chapter}
\acrefprefix{enumi}{enumi}
\acrefprefix{eq}{equation}
\acrefprefix{fig}{figure}
\acrefprefix{lst}{listing}
\acrefprefix{sec}{section}
\acrefprefix{subsec}{subsection}
\acrefprefix{subsubsec}{subsubsection}
\acrefprefix{tab}{table}
\acrefprefix{th}{theorem}
%}}}
