export function getTimeInMins(timeInSeconds: number) {
	const totalMinutes = Math.floor(timeInSeconds / 60);
	const sec = timeInSeconds % 60;
	const hours = Math.floor(totalMinutes / 60);
	const min = totalMinutes % 60;

	// Compact time format: show seconds if < 1min, minutes if < 1hr, hours only if >= 1hr
	const compactTime =
		timeInSeconds < 60
			? `${Math.floor(timeInSeconds)}s`
			: timeInSeconds < 3600 // less than 1 hour
				? (() => {
						const roundedTotalMinutes = Math.round(timeInSeconds / 60);
						return `${roundedTotalMinutes}m`;
					})()
				: (() => {
						const roundedTotalHours = Math.round(timeInSeconds / 3600);
						return `${roundedTotalHours}h`;
					})();

	return {
		hours,
		min,
		sec: Math.floor(sec),

		full: `${timeInHoursMins(hours, min, false) ?? ""} ${sec > 0 ? `${Math.floor(sec)} sec` : ""}`.trim(),

		fullShort:
			`${timeInHoursMins(hours, min, true) ?? ""} ${sec > 0 ? `${Math.floor(sec)}s` : ""}`.trim(),

		compactTime,

		numbers: `${String(min).padStart(2, "0")}:${String(Math.floor(sec)).padStart(2, "0")}`, // no hours, only mins and seconds here
	};
}

function timeInHoursMins(hours: number, mins: number, short: boolean) {
	const parts = [];

	if (hours > 0)
		parts.push(
			short ? `${hours}h` : `${hours} ${hours > 1 ? "hours" : "hour"}`,
		);

	if (mins > 0)
		parts.push(short ? `${mins}m` : `${mins} ${mins > 1 ? "mins" : "min"}`);
	if (parts.length === 0) return null;
	return parts.join(" ");
}
