<?php
/**
 * FluentC Links Model Class.
 *
 * @package FluentCPlugin
 */

namespace FluentC\Models;

use FluentC\Utils\Language;

class FluentC_Links_Model {
	public $options;

	public function __construct() {
		$this->options = $this->initialize_options();
	}


	/**
 * Adds a language code to a URL.
 *
 * @since 2.5.0
 *
 * @param string $url The URL to modify.
 * @param string $language_code Optional. The two-letter language code to add.  Defaults to the current language.
 * @param bool $add_params Optional. Whether to add the language code as a URL parameter or in the path. Defaults to false.
 * @param bool $force_url Optional. Whether to always add the language code, even if it's already present. Defaults to false.
 *
 * @return string The modified URL with the language code added, or an empty string if the input URL is empty.
 */
function add_language_to_link( $url, $language_code = null, $add_params = false, $force_url = false ) {

	if ( empty( $url ) ) {
		return '';
	}

	// Determine the language code if not provided.
	if ( null === $language_code ) {
		$language_code = fluentc_get_current_language();
	}

	// If no language code is available, return the original URL
	if ( empty( $language_code ) ) {
		return $url;
	}

	// Check if the language code is already present in the URL.
	$url_with_lang = false;

	if ( $add_params ) {
		// Check for parameter format.
		$pattern = '#\?lang=[' . $language_code . ']#i';
		if ( preg_match( $pattern, $url ) ) {
			$url_with_lang = true;
		} else {
			// Add the parameter.
			$url = add_query_arg( 'lang', $language_code, $url );
			$url_with_lang = true;
		}

	} else { // Add to path.
		// Check for path format.
		$pattern = '#/' . $language_code . '/#i';
		if ( preg_match( $pattern, $url ) ) {
			$url_with_lang = true;
		} else {
			// Add the language code to the path.
			$url = trailingslashit( $url . '/' . $language_code );
			$url_with_lang = true;
		}
	}

	if ( ! $force_url && $url_with_lang ) {
		return $url;
	} else {
		return $url;
	}

}

	/**
 * Extract language code from the current URL
 * Mimics Polylang's PLL_Links_Model::get_language_from_url() method
 *
 * @return string|false Language slug if found, false otherwise
 */
public function get_language_from_url() {
    // Use existing FluentC Language utility class
    $fluentc_language = new \FluentC\Utils\Language();
    
    // Use your existing get_fluentc_language_url_from_url method if present
    $request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
    
    // Check if we already have a method to extract language from URL
    if (method_exists($fluentc_language, 'get_fluentc_language_url_from_url')) {
        $lang_prefix = $fluentc_language->get_fluentc_language_url_from_url($request_uri);
        
        // Extract language code from the prefix (removing slashes)
        if (!empty($lang_prefix)) {
            return trim($lang_prefix, '/');
        }
    }
    
    // Fallback to standard language detection
    $current_lang = $fluentc_language->get_fluentc_language();
    
    // If no language detected in URL but we have a current language set
    // (from other sources like cookie or session), return that
    return $current_lang ?: false;
}
	/**
	 * Initialize the options property.
	 *
	 * @return array
	 */
	protected function initialize_options() {
		// Add the options initialization logic here.
		// Example options initialization:
		return array(
			'force_lang' => '1',
			'hide_default' => true
			// Add more options as needed.
		);
	}
}