/**
 * Shared date formatting utilities for consistent German date display.
 *
 * All views should use these functions instead of inline toLocaleDateString()
 * calls to ensure consistent formatting across the application.
 *
 * Part of Issue #175.
 */

/**
 * Format a date string (ISO 8601) as a German date (dd.mm.yyyy).
 *
 * @param {string|null|undefined} dateStr ISO date string (e.g. "2026-04-10")
 * @returns {string} Formatted date or em-dash if empty
 */
export function formatDate(dateStr) {
	if (!dateStr) return '\u2014'
	try {
		return new Date(dateStr).toLocaleDateString('de-DE', {
			day: '2-digit',
			month: '2-digit',
			year: 'numeric',
		})
	} catch {
		return dateStr
	}
}

/**
 * Format a date string (ISO 8601) as a German date+time (dd.mm.yyyy, hh:mm:ss).
 *
 * @param {string|null|undefined} dateStr ISO datetime string
 * @returns {string} Formatted datetime or em-dash if empty
 */
export function formatDateTime(dateStr) {
	if (!dateStr) return '\u2014'
	try {
		return new Date(dateStr).toLocaleString('de-DE', {
			day: '2-digit',
			month: '2-digit',
			year: 'numeric',
			hour: '2-digit',
			minute: '2-digit',
			second: '2-digit',
		})
	} catch {
		return dateStr
	}
}

/**
 * Format a Unix timestamp (seconds) as a German date+time.
 *
 * @param {number|null|undefined} timestamp Unix timestamp in seconds
 * @returns {string} Formatted datetime or em-dash if empty
 */
export function formatTimestamp(timestamp) {
	if (!timestamp) return '\u2014'
	try {
		const date = new Date(timestamp * 1000)
		return date.toLocaleDateString('de-DE', {
			day: '2-digit',
			month: '2-digit',
			year: 'numeric',
			hour: '2-digit',
			minute: '2-digit',
		})
	} catch {
		return String(timestamp)
	}
}
