/**
 * Numeric string sorting helpers.
 *
 * The backend currently returns ascending order for years/months/days.
 * In the Explorer we want a "latest first" experience, so we sort descending.
 */

function toNumber(value) {
	const n = Number.parseInt(String(value), 10);
	return Number.isNaN(n) ? null : n;
}

export function sortNumericStringsAsc(items) {
	const arr = Array.isArray(items) ? [...items] : [];
	return arr.sort((a, b) => {
		const na = toNumber(a);
		const nb = toNumber(b);

		if (na !== null && nb !== null) return na - nb;
		return String(a).localeCompare(String(b), "de");
	});
}

export function sortNumericStringsDesc(items) {
	return sortNumericStringsAsc(items).reverse();
}

export function sortFilesByNameAsc(files) {
	const arr = Array.isArray(files) ? [...files] : [];
	return arr.sort((a, b) =>
		String(a?.name || "").localeCompare(String(b?.name || ""), "de")
	);
}
