<?php
/**
 * SpectatorTextPreparer helper.
 *
 * Prepares translated spectator view text with proper pluralization.
 *
 * @package Ilabs\Firevert\Modules\SpectatorViews\Services\Helper
 */

declare( strict_types=1 );

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

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

/**
 * Class SpectatorTextPreparer
 *
 * Generates translated and pluralized text for the Spectator Views widget,
 * returning fully prepared sentences based on the number of spectators
 * and the current active language (PL or EN).
 */
class SpectatorTextPreparer {

	/**
	 * Returns full spectator text with plural and correct verb based on locale.
	 *
	 * @param int $count Number of spectators.
	 *
	 * @return string Full translated spectator text.
	 */
	public static function get_spectator_text( int $count ): string {
		$locale = LanguageHelper::get_active_region_code() ?? get_locale();

		if ( 'pl_PL' === $locale ) {
			if ( 1 === $count ) {
				$label = 'osoba';
				$verb  = 'ogląda';
			} elseif ( $count % 10 >= 2 && $count % 10 <= 4 && ( $count % 100 < 10 || $count % 100 >= 20 ) ) {
				$label = 'osoby';
				$verb  = 'oglądają';
			} else {
				$label = 'osób';
				$verb  = 'ogląda';
			}

			return sprintf(
				/* translators: %1$d: number of people, %2$s: noun (osoba/osoby/osób), %3$s: verb */
				'Aktualnie <span>%1$d</span> %2$s %3$s ten produkt',
				$count,
				$label,
				$verb
			);
		}

		if ( 1 === $count ) {
			$label = 'person';
			$verb  = 'is watching';
		} else {
			$label = 'people';
			$verb  = 'are watching';
		}

		return sprintf(
			/* translators: %1$d: number of people, %2$s: noun (person/people), %3$s: verb */
			'Currently <span>%1$d</span> %2$s %3$s this product',
			$count,
			$label,
			$verb
		);
	}
}
