<?php
/**
 * Formatting helpers for admin UI.
 *
 * @package MailDoctor
 */

namespace MailDoctor\Helpers;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Formatting utilities for admin UI.
 */
class Formatter {

	/**
	 * Format a datetime in site timezone.
	 *
	 * @param string $datetime_gmt Datetime string in GMT.
	 * @return string
	 */
	public static function local_time( string $datetime_gmt ): string {
		if ( empty( $datetime_gmt ) ) {
			return '';
		}

		$timestamp = get_date_from_gmt( $datetime_gmt, 'U' );
		if ( ! $timestamp ) {
			return '';
		}

		return wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $timestamp );
	}

	/**
	 * Format duration in milliseconds.
	 *
	 * @param int|null $duration Duration in ms.
	 * @return string
	 */
	public static function duration_ms( ?int $duration ): string {
		if ( empty( $duration ) || $duration <= 0 ) {
			return __( 'n/a', 'mail-doctor' );
		}

		return sprintf(
			/* translators: %d: duration in milliseconds */
			__( '%d ms', 'mail-doctor' ),
			$duration
		);
	}
}
