<?php
/**
 * FluentC support for The Polylang public API.
 * PLL_Translate_Slugs_Model
 *
 * @package FluentCPlugin
 */

use FluentC\Services\Connect;
use FluentC\Utils\Language;


/**
 * Returns the current language on frontend.
 * Returns the language set in admin language filter on backend (false if set to all languages).
 *
 * @api
 * @since 0.8.1
 * @since 3.4 Accepts composite values.
 *
 * @param string $field Optional, the language field to return (@see PLL_Language), defaults to `'slug'`.
 *                      Pass `\OBJECT` constant to get the language object. A composite value can be used for language
 *                      term property values, in the form of `{language_taxonomy_name}:{property_name}` (see
 *                      {@see PLL_Language::get_tax_prop()} for the possible values). Ex: `term_language:term_taxonomy_id`.
 * @return string|int|bool|string[]|PLL_Language The requested field or object for the current language, `false` if the field isn't set or if current language doesn't exist yet.
 *
 * @phpstan-return (
 *     $field is \OBJECT ? PLL_Language : (
 *         $field is 'slug' ? non-empty-string : string|int|bool|list<non-empty-string>
 *     )
 * )|false
 */
function pll_current_language( $field = 'slug' ) {
	$fluentc_language = new Language();
	
	$lang = $fluentc_language->get_fluentc_language();

	if ( empty( $lang ) || \OBJECT === $field ) {
		return $lang;
	}

	return $lang;
}

/**
 * Returns the default language.
 *
 * @api
 * @since 1.0
 * @since 3.4 Accepts composite values.
 *
 * @param string $field Optional, the language field to return (@see PLL_Language), defaults to `'slug'`.
 *                      Pass `\OBJECT` constant to get the language object. A composite value can be used for language
 *                      term property values, in the form of `{language_taxonomy_name}:{property_name}` (see
 *                      {@see PLL_Language::get_tax_prop()} for the possible values). Ex: `term_language:term_taxonomy_id`.
 * @return string|int|bool|string[]|PLL_Language The requested field or object for the default language, `false` if the field isn't set or if default language doesn't exist yet.
 *
 * @phpstan-return (
 *     $field is \OBJECT ? PLL_Language : (
 *         $field is 'slug' ? non-empty-string : string|int|bool|list<non-empty-string>
 *     )
 * )|false
 */
function pll_default_language( $field = 'slug' ) {
	$fluentc_language = new Language();
	
	$lang = $fluentc_language->fluentc_site_language();

	return $lang;
}
 
/**
 * Among the post and its translations, returns the ID of the post which is in the language represented by $lang.
 *
 * @api
 * @since 0.5
 * @since 3.4 Returns 0 instead of false.
 * @since 3.4 $lang accepts PLL_Language or string.
 *
 * @param int                 $post_id Post ID.
 * @param PLL_Language|string $lang    Optional language (object or slug), defaults to the current language.
 * @return int|false The translation post ID if exists, otherwise the passed ID. False if the passed object has no language or if the language doesn't exist.
 *
 * @phpstan-return int<0, max>|false
 */
function pll_get_post( $post_id, $lang = '' ) {
	
	return $post_id;
}

/**
 * Returns the list of available languages.
 *
 * @api
 * @since 1.5
 *
 * @param array $args {
 *   Optional array of arguments.
 *
 *   @type bool   $hide_empty Hides languages with no posts if set to true ( defaults to false ).
 *   @type string $fields     Return only that field if set ( @see PLL_Language for a list of fields ), defaults to 'slug'.
 * }
 * @return string[]
 */
function pll_languages_list( $args = array() ) {
    $fluentc_connect = new Connect();
    
    // Always set 'fields' to 'slug' to ensure we get an array of slugs
    $args['fields'] = 'slug';
    
    $result = $fluentc_connect->get_pll_fluentc_languages_list( $args );
    
    // Ensure we always return an array of strings (slugs)
    if ( !is_array($result) || empty($result) ) {
        return array();
    }
    
    // If any elements are still objects, extract the slug
    return array_map(function($item) {
        return is_object($item) ? $item->slug : $item;
    }, $result);
}
/**
 * Gets post translations (FluentC stub function)
 * 
 * @param int $post_id Post ID
 * @return array
 */
function pll_get_post_translations($post_id) {
    // Return empty array to prevent errors - FluentC doesn't handle translations
    return array();
}

/**
 * Gets term translations (FluentC stub function)
 * 
 * @param int $term_id Term ID
 * @return array  
 */
function pll_get_term_translations($term_id) {
    // Return empty array to prevent errors
    return array();
}

/**
 * Save post translations (FluentC stub function)
 * 
 * @param array $translations Array of translations
 * @return bool
 */
function pll_save_post_translations($translations) {
    // Stub function - does nothing but prevents errors
    return true;
}

/**
 * Save term translations (FluentC stub function)
 * 
 * @param array $translations Array of translations  
 * @return bool
 */
function pll_save_term_translations($translations) {
    // Stub function - does nothing but prevents errors
    return true;
}

/**
 * Count posts in a language (FluentC stub function)
 * 
 * @param string $lang Language code
 * @param array $args Additional arguments
 * @return int
 */
function pll_count_posts($lang, $args = array()) {
    // Return 0 to prevent errors
    return 0;
}

/**
 * Gets the home URL for a language (FluentC stub function)
 * 
 * @param string $lang Language code
 * @return string
 */
function pll_home_url($lang = '') {
    // Return regular home URL
    return home_url();
}
/**
 * Returns the post language. Used by FluentC to get the Language from the URL
 *
 * @api
 * @since 1.5.4
 * @since 3.4 Accepts composite values for `$field`.
 *
 * @param int    $post_id Post ID.
 * @param string $field Optional, the language field to return (@see PLL_Language), defaults to `'slug'`.
 *                      Pass `\OBJECT` constant to get the language object. A composite value can be used for language
 *                      term property values, in the form of `{language_taxonomy_name}:{property_name}` (see
 *                      {@see PLL_Language::get_tax_prop()} for the possible values). Ex: `term_language:term_taxonomy_id`.
 * @return string|int|bool|string[]|PLL_Language The requested field or object for the post language, `false` if no language is associated to that post.
 *
 * @phpstan-return (
 *     $field is \OBJECT ? PLL_Language : (
 *         $field is 'slug' ? non-empty-string : string|int|bool|list<non-empty-string>
 *     )
 * )|false
 */
function pll_get_post_language( $post_id, $field = 'slug' ) {
	$fluentc_language = new Language();
	
	$lang = $fluentc_language->get_fluentc_language();

	if ( empty( $lang ) || \OBJECT === $field ) {
		return $lang;
	}

	return $lang;
}

/**
 * Returns the term language which for FluentC is set in the URL
 *
 * @api
 * @since 1.5.4
 * @since 3.4 Accepts composite values for `$field`.
 *
 * @param int    $term_id Term ID.
 * @param string $field Optional, the language field to return (@see PLL_Language), defaults to `'slug'`.
 *                      Pass `\OBJECT` constant to get the language object. A composite value can be used for language
 *                      term property values, in the form of `{language_taxonomy_name}:{property_name}` (see
 *                      {@see PLL_Language::get_tax_prop()} for the possible values). Ex: `term_language:term_taxonomy_id`.
 * @return string|int|bool|string[]|PLL_Language The requested field or object for the post language, `false` if no language is associated to that term.
 *
 * @phpstan-return (
 *     $field is \OBJECT ? PLL_Language : (
 *         $field is 'slug' ? non-empty-string : string|int|bool|list<non-empty-string>
 *     )
 * )|false
 */
function pll_get_term_language( $term_id, $field = 'slug' ) {
	$fluentc_language = new Language();
	
	$lang = $fluentc_language->get_fluentc_language();

	if ( empty( $lang ) || \OBJECT === $field ) {
		return $lang;
	}

	return $lang;
}

/**
 * Allows to access the Polylang instance.
 * However, it is always preferable to use API functions
 * as internal methods may be changed without prior notice.
 *
 * @since 1.8
 *
 * @return PLL_Frontend|PLL_Admin|PLL_Settings|PLL_REST_Request
 */
function PLL() {
    if (!isset($GLOBALS['polylang'])) {
        $GLOBALS['polylang'] = new FluentC\Polylang();
    }
    return $GLOBALS['polylang'];
}

/**
 * Registers a string for translation (FluentC stub function)
 * 
 * This is a compatibility function for plugins like JetEngine that expect
 * pll_register_string() to exist. FluentC doesn't implement string translations,
 * so this function does nothing but prevents fatal errors.
 *
 * @api
 * @since 1.0
 *
 * @param string $name       Name provided for sorting convenience (ex: 'myplugin')
 * @param string $string     The string to translate
 * @param string $group      Optional. The group in which the string is registered, defaults to 'polylang'
 * @param bool   $multiline  Optional. If set to true, the translation text field will be multiline, defaults to false
 * @return void
 */
function pll_register_string( $name, $string, $group = 'polylang', $multiline = false ) {
    // FluentC stub function - does nothing but prevents fatal errors
    // FluentC doesn't implement string translations functionality
    return;
}

/**
 * Translates a string previously registered with pll_register_string (FluentC stub function)
 * 
 * @api
 * @since 1.0
 *
 * @param string $string The string to translate
 * @return string The original string (untranslated in FluentC)
 */
function pll__( $string ) {
    // FluentC stub function - returns original string untranslated
    return $string;
}

/**
 * Echoes a translated string previously registered with pll_register_string (FluentC stub function)
 * 
 * @api
 * @since 1.0
 *
 * @param string $string The string to translate and echo
 * @return void
 */
function pll_e( $string ) {
    // FluentC stub function - echoes original string untranslated
    echo $string;
}

/**
 * Translates a string previously registered with pll_register_string in a given language (FluentC stub function)
 * 
 * @api
 * @since 1.0
 *
 * @param string $string The string to translate
 * @param string $lang   The language code of the desired translation
 * @return string The original string (untranslated in FluentC)
 */
function pll_translate_string( $string, $lang ) {
    // FluentC stub function - returns original string untranslated
    return $string;
}

/**
 * Sets the language of a post (FluentC stub function)
 * 
 * @param int $post_id Post ID
 * @param string $lang Language code
 * @return bool
 */
function pll_set_post_language($post_id, $lang) {
    // Stub function - does nothing but prevents errors
    return true;
}

/**
 * Sets the language of a term (FluentC stub function)
 * 
 * @param int $term_id Term ID
 * @param string $lang Language code
 * @return bool
 */
function pll_set_term_language($term_id, $lang) {
    // Stub function - does nothing but prevents errors
    return true;
}

/**
 * Checks if a post type is translated (FluentC stub function)
 * 
 * @param string $post_type Post type name
 * @return bool
 */
function pll_is_translated_post_type($post_type) {
    // Return false - FluentC doesn't handle post type translations
    return false;
}

/**
 * Checks if a taxonomy is translated (FluentC stub function)
 * 
 * @param string $taxonomy Taxonomy name
 * @return bool
 */
function pll_is_translated_taxonomy($taxonomy) {
    // Return false - FluentC doesn't handle taxonomy translations
    return false;
}
