<?php
/**
 * PromoTimeTextPreparer helper.
 *
 * Prepares translated singular/plural labels for the number of days
 * between two dates, based on the current locale.
 *
 * @package Ilabs\Firevert\Modules\PromoTimeWidget\Services\Helper
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Modules\PromoTimeWidget\Services\Helper;

use DateTimeImmutable;
use Ilabs\Firevert\Core\Services\Helper\Integration\LanguageHelper;

/**
 * Class PromoTimeTextPreparer
 *
 * Generates localized singular/plural day label ("day(s)", "dzień/dni")
 * based on the number of days between two given dates.
 */
class PromoTimeTextPreparer {

	/**
	 * Returns the correct pluralized form of "day"/"dzień"
	 * depending on the number of days between two dates and locale.
	 *
	 * @param string $start_date_string Start date (parsable by DateTimeImmutable).
	 * @param string $end_date_string   End date (parsable by DateTimeImmutable).
	 *
	 * @return string Translated and pluralized day label.
	 */
	public static function get_day_label_from_range( string $start_date_string, string $end_date_string ): string {
		try {
			$start = new DateTimeImmutable( $start_date_string );
			$end   = new DateTimeImmutable( $end_date_string );
		} catch ( \Exception $e ) {
			// Fallback to English plural if invalid date.
			return 'days';
		}

		$days   = (int) $start->diff( $end )->format( '%a' );
		$locale = LanguageHelper::get_active_region_code() ?? get_locale();

		if ( 'pl_PL' === $locale ) {
			return ( 1 === $days ) ? 'dzień' : 'dni';
		}

		return ( 1 === $days ) ? 'day' : 'days';
	}
}
