const UNITS: { label: string; seconds: number }[] = [
	{ label: "y", seconds: 31536000 },
	{ label: "mo", seconds: 2592000 },
	{ label: "w", seconds: 604800 },
	{ label: "d", seconds: 86400 },
	{ label: "h", seconds: 3600 },
	{ label: "m", seconds: 60 },
	{ label: "s", seconds: 1 },
];

export function formatRelativeTime(dateStr: string): string {
	const date = new Date(dateStr);
	const now = new Date();
	const diffSeconds = Math.max(
		0,
		Math.floor((now.getTime() - date.getTime()) / 1000),
	);

	if (diffSeconds < 5) return "just now";

	for (const unit of UNITS) {
		const count = Math.floor(diffSeconds / unit.seconds);
		if (count >= 1) {
			return `${count}${unit.label} ago`;
		}
	}

	return "just now";
}
