<?php
/**
 * WPML language provider implementation.
 *
 * @package Ilabs\Firevert\Core\Integration\Language
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Core\Integration\Language;

/**
 * Class WpmlLanguageProvider
 *
 * Implements the language provider interface for WPML plugin.
 *
 * @since 1.0.0
 */
class WpmlLanguageProvider implements LanguageProviderInterface {

	/**
	 * Returns the readable name of the source plugin.
	 *
	 * @return string Plugin name.
	 * @since 1.0.0
	 */
	public function get_plugin_name(): string {
		return 'WPML Multilingual CMS';
	}

	/**
	 * Returns the readable name of the source plugin creator.
	 *
	 * @return string Plugin creator.
	 * @since 1.0.0
	 */
	public function get_plugin_creator(): string {
		return 'OnTheGoSystems';
	}

	/**
	 * Checks if WPML is active and available.
	 *
	 * @return bool Whether WPML is active.
	 * @since 1.0.0
	 */
	public function is_active(): bool {
		return defined( 'ICL_SITEPRESS_VERSION' ) && function_exists( 'wpml_get_current_language' );
	}

	/**
	 * Alternative method to get current slug for backward compatibility.
	 *
	 * @return string|null Current language slug or null if not available.
	 * @since 1.0.0
	 */
	public function is_current_slug(): ?string {
		return $this->is_active() ? wpml_get_current_language() : null;
	}

	/**
	 * Returns current language slug.
	 *
	 * @return string|null Current language slug (e.g., 'pl', 'en') or null if not available.
	 * @since 1.0.0
	 */
	public function get_current_slug(): ?string {
		return $this->is_current_slug();
	}

	/**
	 * Returns default language slug.
	 *
	 * @return string|null Default language slug (e.g., 'pl', 'en') or null if not available.
	 * @since 1.0.0
	 */
	public function get_default_slug(): ?string {
		return function_exists( 'wpml_get_default_language' )
			? wpml_get_default_language()
			: null;
	}

	/**
	 * Returns all available language slugs.
	 *
	 * @return array List of available language slugs (e.g., ['pl', 'en']).
	 * @since 1.0.0
	 */
	public function get_available_slugs(): array {
		if ( ! $this->is_active() ) {
			return array();
		}

		$languages = apply_filters( 'wpml_active_languages', null, array( 'skip_missing' => 0 ) );

		return is_array( $languages ) ? array_keys( $languages ) : array();
	}

	/**
	 * Returns current locale.
	 *
	 * @return string|null Current locale (e.g., 'pl_PL') or null if not available.
	 * @since 1.0.0
	 */
	public function get_current_locale(): ?string {
		if ( ! $this->is_active() ) {
			return null;
		}

		$languages = apply_filters( 'wpml_active_languages', null );
		if ( is_array( $languages ) ) {
			foreach ( $languages as $lang ) {
				if ( isset( $lang['active'], $lang['default_locale'] ) && $lang['active'] ) {
					return $lang['default_locale'];
				}
			}
		}

		return null;
	}

	/**
	 * Returns all available locales.
	 *
	 * @return array List of available locales (e.g., ['pl_PL', 'en_GB']).
	 * @since 1.0.0
	 */
	public function get_available_locales(): array {
		if ( ! $this->is_active() ) {
			return array();
		}

		$languages = apply_filters( 'wpml_active_languages', null, array( 'skip_missing' => 0 ) );

		if ( ! is_array( $languages ) ) {
			return array();
		}

		return array_values(
			array_filter(
				array_map(
					static function ( $lang ) {
						return $lang['default_locale'] ?? null;
					},
					$languages
				)
			)
		);
	}

	/**
	 * Returns mapping between language slugs and locales.
	 *
	 * @return array Associative array mapping slugs to locales (e.g., ['pl' => 'pl_PL']).
	 * @since 1.0.0
	 */
	public function get_slug_to_locale_map(): array {
		if ( ! $this->is_active() ) {
			return array();
		}

		$languages = apply_filters( 'wpml_active_languages', null, array( 'skip_missing' => 0 ) );
		if ( ! is_array( $languages ) ) {
			return array();
		}

		$map = array();
		foreach ( $languages as $slug => $data ) {
			if ( isset( $data['default_locale'] ) ) {
				$map[ $slug ] = $data['default_locale'];
			}
		}

		return $map;
	}
}
