<?php
/**
 * FluentC WordPress URL File.
 *
 * This file contains the functions for managing URls
 *
 * @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
 */

namespace FluentC\Services;

use FluentC\Utils\Language;

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

/**
 * URL Editting Functions
 */
class Url {

	/**
	 * Site URL
	 *
	 * @var string
	 */
	protected $page_url;
	/**
	 * FluentC connection class
	 *
	 * @var object
	 */
	protected $fluentc_language;
	/**
	 * Constructor.
	 *
	 * @since 1.2
	 */
	public function __construct() {
		$this->fluentc_language = new Language();
	}
	/**
	 * Method to get Prefix from URL
	 *
	 * @param string $url URL.
	 * @param string $widgetapikey FluentC Widget key.
	 * @since 1.2
	 */
	public function get_canonical_url( $url, $widgetapikey ) {

		$prefixes = $this->fluentc_language->get_fluentc_language_url();
		$language = $this->fluentc_language->get_fluentc_language();
		$new_url  = $this->remove_matching_prefix_from_url( $url, $prefixes, $language );

		return $new_url;
	}
	/**
	 * Base URL Part
	 *
	 * @param string $url URL.
	 * @since 1.2
	 */
	public function get_base( $url ) {
		$url_parts = wp_parse_url( $url );

		// Rebuild the URL with the language code.
		$base__url = $url_parts['scheme'] . '://' . $url_parts['host'];
		return $base__url;
	}
	/**
	 * URL Query Parser
	 *
	 * @param string $url URL.
	 * @param string $widgetapikey FluentC Widget key.
	 * @return   string
	 * @since 1.2
	 */
	public function get_url_query( $url, $widgetapikey ) {

		$prefixes = $this->fluentc_language->get_fluentc_language_url();
		$language = $this->fluentc_language->get_fluentc_language();
		$new_path = $this->remove_matching_prefix_from_url_path( $url, $prefixes, $language );

		return $new_path;
	}
	/**
	 * Compare and Remove Prefix from URL
	 *
	 * @param string $url URL.
	 * @param string $prefixes  URL Prefix.
	 * @param string $language  Language if set.
	 * @return string
	 * @since 1.2
	 */
	public function remove_matching_prefix_from_url( $url, $prefixes, $language ) {
		// Parse the URL and get the path part.
		$parsed_url = wp_parse_url( $url );
		$path       = isset($parsed_url['path']) ? $parsed_url['path'] : '';
	
		// Check if $prefixes is not null and the path starts with the current prefix.
		if ( $prefixes !== null && !empty($path) && substr( $path, 0, strlen( $prefixes ) ) === $prefixes ) {
			// Remove the prefix from the path.
			$path = substr( $path, strlen( $prefixes ) );
		}
	
		// Check if $language is not null and the path starts with the language.
		if ( $language !== null && !empty($path) && substr( $path, 0, strlen( $language ) ) === $language ) {
			// Remove the language from the path.
			$path = substr( $path, strlen( $language ) );
		}
	
		// Reconstruct and return the modified URL.
		return ( isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] . '://' : '' )
			. ( isset( $parsed_url['host'] ) ? $parsed_url['host'] : '' )
			. $path
			. ( isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '' )
			. ( isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : '' );
	}

	/**
	 * Removes Prefix from URL
	 *
	 * @param string $url URL.
	 * @param string $prefix  URL Prefix.
	 * @param string $language  Language if set.
	 * @return string
	 * @since 1.2
	 */
	public function remove_matching_prefix_from_url_path( $url, $prefix, $language ) {
		// Parse the URL and get the path part.
		$parsed_url = wp_parse_url( $url );
		$path       = $parsed_url['path'];

		// Check if the path starts with the current prefix.
		// Check if $prefixes is not null and the path starts with the current prefix.
		if ( $prefix !== null && !empty($path) && substr( $path, 0, strlen( $prefix ) ) === $prefix ) {
			// Remove the prefix from the path.
			$path = substr( $path, strlen( $prefix ) );
		}
	
		// Check if $language is not null and the path starts with the language.
		if ( $language !== null && !empty($path) && substr( $path, 0, strlen( $language ) ) === $language ) {
			// Remove the language from the path.
			$path = substr( $path, strlen( $language ) );
		}

		// Reconstruct and return the modified URL.
		return $path
			. ( isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '' )
			. ( isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : '' );
	}
}
