\NeedsTeXFormat{LaTeX2e}
\ProvidesExplPackage{PrimeTeX/packages/Caption}{2025-09-10}{1.0}{
	Custom Caption commands.
}

\NewDocumentCommand{\RaggedOutside}{}{
	\bool_if:NTF \g_electronic_bool {
		\RaggedRight%
	} {%
		\checkoddpage%
		\ifoddpage%
			\RaggedRight%
		\else%
			\RaggedLeft%
		\fi%
	}%
}

\cs_new_protected:Nn \__MyCaption_format_default: {\Centering\small\color{captionColor}}

\keys_define:nn {MyCaption} {
	before	.bool_set:N	= \l_MyCaption_before_bool,
	before	.initial:n	= {false},

	figure	.code:n		= {\str_set:Nn \l_MyCaption_type_str {figure}},

	format	.cs_set_protected:Np	= \__MyCaption_format:,
	format	.initial:n	= {},
	format	.default:n	= {},

	skip	.dim_set:N	= \l_MyCaption_skip_dim,
	skip	.initial:n	= {\smallskipamount},

	table	.code:n		= {
		\str_set:Nn \l_MyCaption_type_str {table}
		\bool_set:Nn \l_MyCaption_before_bool {true}
	},

	type	.choices:nn	= {figure,table}{
		\str_set:Ne \l_MyCaption_type_str {\l_keys_choice_tl}
	},
	type	.initial:n	= {figure},
}

\str_new:N \l_MyCaption_label_str
\str_new:N \l_MyCaption_number_str

\NewDocumentEnvironment{MyCaption}{
	% #1: Key-value options.
	O{}
	% #2: Label
	>{\TrimSpaces}m
	% #3: Caption
	m
	% #4: Body
	+b
}{
	\group_begin:
		\keys_set:nn {MyCaption} {format}
		\keys_set:nn {MyCaption} {#1}
		\str_case:en {\l_MyCaption_type_str} {
			{figure} {
				\refstepcounter{figure}
				\str_set:Nn \l_MyCaption_label_str {Figure}
				\str_set:Ne \l_MyCaption_number_str {
					\arabic{chapter}.\arabic{figure}
				}
				\addcontentsline{lof}{figure}{
					\protect\numberline{\l_MyCaption_number_str}\protect\ignorespaces~#3
				}
			}
			{table} {
				\refstepcounter{table}
				\str_set:Nn \l_MyCaption_label_str {Table}
				\str_set:Ne \l_MyCaption_number_str {
					\arabic{chapter}.\arabic{table}
				}
				\addcontentsline{lot}{table}{
					\protect\numberline{\l_MyCaption_number_str}\protect\ignorespaces~#3
				}
			}
		}
		\bool_if:NF \l_MyCaption_before_bool {
			\label{#2}
			#4
		}
		\setlength{\parskip}{\l_MyCaption_skip_dim}
		\par
		\__MyCaption_format_default: \__MyCaption_format:
		\l_MyCaption_label_str{}~\l_MyCaption_number_str :~#3
		\par
		\bool_if:NT \l_MyCaption_before_bool {
			\label{#2}
			#4
		}
	\group_end:
}{}

\cs_new:Nn \__MyCaption_insert_caption:n {
	\protect\__MyCaption_format_default: \protect\__MyCaption_format:
	\l_MyCaption_label_str{}~\l_MyCaption_number_str :~#1
}