<?php

/**

 * Language detection. Spanish is the default. A single setting controls the

 * language for every administrator viewing the dashboard.

 *

 * @package SuiteMkt3k

 */



namespace SuiteMkt3k\Utils;



defined( 'ABSPATH' ) || exit;



final class I18n {



	private static ?string $cached_locale = null;



	private static ?string $loaded_for = null;



	public static function register(): void {

		add_filter( 'plugin_locale', array( __CLASS__, 'filter_plugin_locale' ), 1, 2 );

		add_filter( 'gettext', array( __CLASS__, 'filter_gettext' ), 1, 3 );

		add_filter( 'gettext_with_context', array( __CLASS__, 'filter_gettext_with_context' ), 1, 4 );

		add_filter( 'ngettext', array( __CLASS__, 'filter_ngettext' ), 1, 5 );

		add_filter( 'load_script_translation_file', array( __CLASS__, 'filter_script_translation_file' ), 1, 3 );

		add_action( 'plugins_loaded', array( __CLASS__, 'load_textdomain' ), 1 );

		add_action( 'init', array( __CLASS__, 'load_textdomain' ), 1 );

	}



	public static function available_locales(): array {

		return array(

			'es_ES' => 'Español',

			'en_US' => 'English',

		);

	}



	public static function get_default_locale(): string {

		$settings = (array) get_option( 'suitemkt3k_settings', array() );

		$value    = $settings['language']['default'] ?? 'es_ES';

		return self::is_supported( (string) $value ) ? (string) $value : 'es_ES';

	}



	public static function get_active_locale(): string {

		if ( null === self::$cached_locale ) {

			self::$cached_locale = self::get_default_locale();

		}

		return self::$cached_locale;

	}



	public static function is_supported( string $locale ): bool {

		return array_key_exists( $locale, self::available_locales() );

	}



	public static function locale_to_short( string $locale ): string {

		return 'es_ES' === $locale ? 'es' : 'en';

	}



	public static function filter_plugin_locale( string $locale, string $domain ): string {

		if ( 'suitemkt3k' === $domain ) {

			return self::get_active_locale();

		}

		return $locale;

	}



	/**

	 * English uses source strings; Spanish uses the MO file.

	 */

	private static function uses_source_strings(): bool {

		return 'en_US' === self::get_active_locale();

	}



	public static function filter_gettext( $translation, $text, $domain ) {

		if ( 'suitemkt3k' !== $domain || ! self::uses_source_strings() ) {

			return $translation;

		}

		return $text;

	}



	public static function filter_gettext_with_context( $translation, $text, $context, $domain ) {

		if ( 'suitemkt3k' !== $domain || ! self::uses_source_strings() ) {

			return $translation;

		}

		return $text;

	}



	public static function filter_ngettext( $translation, $single, $plural, $number, $domain ) {

		if ( 'suitemkt3k' !== $domain || ! self::uses_source_strings() ) {

			return $translation;

		}

		return 1 === (int) $number ? $single : $plural;

	}



	/**

	 * Skip React/JS translation JSON when English is active (use msgid strings).

	 *

	 * @param string|false $file   Path to JSON, or false.

	 * @param string       $handle Script handle.

	 * @param string       $domain Text domain.

	 * @return string|false

	 */

	public static function filter_script_translation_file( $file, $handle, $domain ) {

		if ( 'suitemkt3k' !== $domain || ! self::uses_source_strings() ) {

			return $file;

		}

		return false;

	}



	/**

	 * Load the correct MO file for the plugin language setting.

	 */

	public static function load_textdomain(): void {

		$locale = self::get_active_locale();



		if ( self::$loaded_for === $locale ) {

			return;

		}



		unload_textdomain( 'suitemkt3k' );



		if ( 'es_ES' === $locale ) {

			$mofile = SUITEMKT3K_PATH . 'languages/suitemkt3k-es_ES.mo';

			if ( is_readable( $mofile ) ) {

				load_textdomain( 'suitemkt3k', $mofile );

			}

		}



		self::$loaded_for = $locale;

	}



	/**

	 * Ensure translations are loaded (REST/public emails).

	 */

	public static function ensure_textdomain(): void {

		self::load_textdomain();

	}



	/**

	 * Clear per-request caches after the language setting changes.

	 */

	public static function reset_runtime_cache(): void {

		self::$cached_locale = null;

		self::$loaded_for    = null;

	}

}

