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

declare( strict_types=1 );

namespace Ilabs\Firevert\Core\Integration\Language;

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

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

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

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

	/**
	 * 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 function_exists( 'pll_current_language' ) ? pll_current_language() : null;
	}

	/**
	 * 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( 'pll_default_language' ) ? pll_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 {
		return function_exists( 'pll_languages_list' ) ? pll_languages_list() : array();
	}

	/**
	 * 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 {
		return function_exists( 'pll_languages_list' )
			? pll_languages_list( array( 'fields' => 'locale' ) )
			: array();
	}

	/**
	 * 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 ( ! function_exists( 'pll_languages_list' ) ) {
			return array();
		}

		$slugs   = pll_languages_list( array( 'fields' => 'slug' ) );
		$locales = pll_languages_list( array( 'fields' => 'locale' ) );

		if ( ! is_array( $slugs ) || ! is_array( $locales ) || count( $slugs ) !== count( $locales ) ) {
			return array();
		}

		return array_combine( $slugs, $locales );
	}

	/**
	 * 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 ( ! function_exists( 'pll_current_language' ) || ! function_exists( 'pll_languages_list' ) ) {
			return null;
		}

		$current_slug = pll_current_language();
		$slugs        = pll_languages_list( array( 'fields' => 'slug' ) );
		$locales      = pll_languages_list( array( 'fields' => 'locale' ) );

		if ( ! is_array( $slugs ) || ! is_array( $locales ) || count( $slugs ) !== count( $locales ) ) {
			return null;
		}

		$map = array_combine( $slugs, $locales );

		return $map[ $current_slug ] ?? null;
	}

	/**
	 * 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 function_exists( 'pll_current_language' ) ? pll_current_language() : null;
	}
}
