<?php
/**
 * FluentC Global Language Functions
 *
 * This file provides global helper functions to access language functionality
 *
 * @category Translation
 * @package  FluentCPlugin
 * @author   FluentC <wp@fluentc.io>
 * @license  http://www.gnu.org/copyleft/gpl.html GNU General Public License
 * @link     https://www.fluentc.ai
 */

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Get the FluentC Language instance
 *
 * @return FluentC\Utils\Language
 */
function fluentc_language() {
    static $instance = null;
    
    if (null === $instance) {
        $instance = new \FluentC\Utils\Language();
    }
    
    return $instance;
}

/**
 * Get the current language code
 *
 * @return string|null
 */
function fluentc_get_current_language() {
    static $current_language = null;
    
    if ($current_language === null) {
        // Check API key first
        $api_key = get_option('fluentc_api_key');
        if (empty($api_key)) {
            return null;
        }
        
        // Use the language class to determine language
        $current_language = fluentc_language()->get_fluentc_language();
    }
    
    return $current_language;
}

/**
 * Set the current language code
 *
 * @param string $language_code The language code to set
 * @return void
 */
function fluentc_set_current_language($language_code) {
    fluentc_language()->set_current_language($language_code);
}

/**
 * Get the site's default language
 *
 * @return string
 */
function fluentc_get_default_language() {
    return fluentc_language()->fluentc_site_language();
}

/**
 * Get all available languages
 *
 * @return array List of language codes
 */
function fluentc_get_available_languages() {
    return fluentc_language()->get_available_languages();
}

/**
 * Check if a language is available
 *
 * @param string $language_code The language code to check
 * @return bool
 */
function fluentc_is_language_available($language_code) {
    return fluentc_language()->is_language_available($language_code);
}

/**
 * Get the language URL prefix
 *
 * @return string
 */
function fluentc_get_language_url() {
    return fluentc_language()->get_fluentc_language_url();
}

/**
 * Add language code to URL
 *
 * @param string $url The URL to modify
 * @param string $language_code Optional language code (defaults to current language)
 * @return string The URL with language code
 */
function fluentc_add_language_to_url($url, $language_code = null) {
    if (null === $language_code) {
        $language_code = fluentc_get_current_language();
    }
    
    // Skip if no language code
    if (!$language_code) {
        return $url;
    }
    
    // Get the default language
    $default_language = fluentc_get_default_language();
    
    // Don't add language code if it's the default language
    if ($language_code === $default_language) {
        return $url;
    }
    
    $url_parts = parse_url($url);
    
    // Build URL with language code
    $scheme = isset($url_parts['scheme']) ? $url_parts['scheme'] . '://' : '';
    $host = isset($url_parts['host']) ? $url_parts['host'] : '';
    $path = isset($url_parts['path']) ? $url_parts['path'] : '';
    
    // Check if path already has language code
    if (preg_match('|^/' . $language_code . '/|', $path)) {
        return $url;
    }
    
    // Add language code to path
    $path = '/' . $language_code . $path;
    
    // Rebuild URL
    $new_url = $scheme . $host . $path;
    
    // Add query string
    if (isset($url_parts['query'])) {
        $new_url .= '?' . $url_parts['query'];
    }
    
    // Add fragment
    if (isset($url_parts['fragment'])) {
        $new_url .= '#' . $url_parts['fragment'];
    }
    
    return $new_url;
}