<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * Core Functions
 *
 * @author RadiusTheme
 * @package RadiusBooking
 * @since 1.0.0
 */

use RadiusTheme\RadiusBooking\Helpers\SettingsHelper;
/**
 * Load a specified template file with optional arguments.
 *
 * This function locates a template file, applies filters for customization,
 * validates its existence, and allows passing arguments to the template scope.
 * It also triggers actions before and after the template file inclusion.
 *
 * @param string $template_name The name of the template to be loaded, with or without `.php` extension.
 * @param array $args Optional. An associative array of arguments to pass to the template. Default empty array.
 * @param string $template_path Optional. custom template path. Default ''.
 * @param string $default_path Optional. Default template path to use if template not found. Default ''.
 *
 * @return void
 */
function rtrb_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {

	if ( false === strpos( $template_name, '.php' ) ) {
		$template_name .= '.php';
	}

	$template = rtrb_locate_template( $template_name, $template_path, $default_path );

	// Allow 3rd party plugin filter template file from their plugin.
	$filter_template = apply_filters( 'radius_booking_get_template', $template, $template_name, $args, $template_path, $default_path );

	if ( $filter_template !== $template ) {
		if ( ! file_exists( $filter_template ) ) {
			/* translators: %s template */
			_doing_it_wrong(
				__FUNCTION__,
				sprintf(
				/* translators: %s template */
					esc_html__( '%s does not exist.', 'radius-booking' ),
					'<code>' . esc_html( $filter_template ) . '</code>'
				),
				'1.0'
			);
			return;
		}
		$template = $filter_template;
	}

	$action_args = array(
		'template_name' => $template_name,
		'template_path' => $template_path,
		'located'       => $template,
		'args'          => $args,
	);

	if ( ! empty( $args ) && is_array( $args ) ) {
		if ( isset( $args['action_args'] ) ) {
			_doing_it_wrong(
				__FUNCTION__,
				esc_html__( 'action_args should not be overwritten when calling rtrb_get_template.', 'radius-booking' ),
				'1.0.0'
			);
			unset( $args['action_args'] );
		}
		extract( $args ); // @codingStandardsIgnoreLine
	}

	do_action( 'radius_booking_before_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] );

	include $action_args['located'];

	do_action( 'radius_booking_after_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] );
}


/**
 * Locate a template file.
 *
 * @param string $template_name The name of the template file to locate.
 * @param string $template_path Optional. The path to search within. Default is determined by the 'radius_booking_template_path' filter.
 * @param string $default_path Optional. The default path to search if the template is not found in the given path. Default is determined by the 'rtrb_template_path' filter and 'RADIUS_BOOKING_PATH'.
 *
 * @return string The full path to the located template file.
 */
function rtrb_locate_template( $template_name, $template_path = '', $default_path = '' ) {
	if ( ! $template_path ) {
		$template_path = apply_filters( 'radius_booking_template_path', 'radius-booking/' );
	}

	if ( ! $default_path ) {
		$default_path = apply_filters( 'rtrb_template_path', RADIUS_BOOKING_PATH ) . '/templates/';
	}

	if ( empty( $template ) ) {
		$template = locate_template(
			array(
				trailingslashit( $template_path ) . $template_name,
				$template_name,
			)
		);
	}

	if ( ! isset( $template ) || ! $template ) {
		$template = trailingslashit( $default_path ) . $template_name;
	}

	// Return what we found.
	return apply_filters( 'rtrb_locate_template', $template, $template_name, $template_path );
}


/**
 * Locate and load a template part.
 *
 * This function attempts to locate a template part by its slug and optionally by name.
 * It searches in the child theme, parent theme, and a plugin-defined path as a fallback.
 * If a template is found, it is loaded.
 *
 * @param string $slug The slug name for the generic template.
 * @param string $name Optional. The name of the specialized template. Default is an empty string.
 *
 * @return void
 */
function rtrb_locate_template_part( $slug, $name = '' ) {
	if ( $name ) {
		$template = locate_template(
			array(
				"{$slug}-{$name}.php",
				rtrb_template_path() . "{$slug}-{$name}.php",
			)
		);
		if ( ! $template ) {
			$fallback = RADIUS_BOOKING_PATH . "/templates/{$slug}-{$name}.php";
			$template = file_exists( $fallback ) ? $fallback : '';
		}
	}
	$template = apply_filters( 'rtrb_get_template_part', $template, $slug, $name );

	if ( $template ) {
		load_template( $template, false );
	}
}


/**
 * Set a cookie with the given parameters if headers have not been sent.
 *
 * @param string $name The name of the cookie.
 * @param string $value The value of the cookie.
 * @param int $expire The time the cookie expires. Defaults to 0 (session cookie).
 * @param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection. Defaults to false.
 * @param bool $httponly Whether the cookie is accessible only through the HTTP protocol. Defaults to false.
 *
 * @return void
 */
function rtrb_setcookie( $name, $value, $expire = 0, $secure = false, $httponly = false ) {
	if ( ! headers_sent() ) {
		$options = array(
			'expires'  => $expire,
			'secure'   => $secure,
			'path'     => COOKIEPATH ? COOKIEPATH : '/',
			'domain'   => COOKIE_DOMAIN,
			'httponly' => $httponly,
		);
		setcookie( $name, $value, $options );
	}
}


/**
 * Check if the site URL is using HTTPS.
 *
 * @return bool
 */
function rtrb_site_is_https() {
	return false !== strstr( get_option( 'home' ), 'https:' );
}


/**
 * Generate a help tip element with provided text.
 *
 * @param string $tip The help tip text to be displayed.
 *
 * @return string The generated HTML for the help tip element.
 */
function rtrb_help_tip( $tip ) {
	$sanitized_tip = esc_attr( $tip );
	return apply_filters( 'radius_booking_help_tip', '<span class="rtrb-help-tip" tabindex="0" aria-label="' . $sanitized_tip . '" data-tip="' . $sanitized_tip . '"></span>', $sanitized_tip, $tip );
}


/**
 * Retrieve the template path for the Radius Booking plugin.
 *
 * This function applies a filter to allow customization of the template path.
 *
 * @return string The template path for the Radius Booking plugin.
 */
function rtrb_template_path() {
	return apply_filters( 'radius_booking_template_path', 'radius-booking/' );
}

/**
 * Retrieve the currency setting for the booking system.
 *
 * @return string The currency code used in the booking system.
 */
function get_rtrb_currency() {
	return apply_filters( 'radius_booking_currency', SettingsHelper::get_setting( 'currency' ) );
}
function rtrb_phone_setting() {
	return apply_filters( 'radius_booking_location_setting', SettingsHelper::get_setting( 'phone' ) );
}
function rtrb_get_business_setting() {
	return apply_filters( 'radius_booking_business_setting', SettingsHelper::get_setting( 'business' ) );
}
/**
 * Get full list of currency codes.
 *
 * Currency symbols and names should follow the Unicode CLDR recommendation (https://cldr.unicode.org/translation/currency-names-and-symbols)
 *
 * @return array
 */
function get_rtrb_currencies() {
	static $currencies;

	if ( ! isset( $currencies ) ) {
		$currencies = array_unique(
			apply_filters(
				'radius_booking_currencies',
				array(
					'AED' => __( 'United Arab Emirates dirham', 'radius-booking' ),
					'AFN' => __( 'Afghan afghani', 'radius-booking' ),
					'ALL' => __( 'Albanian lek', 'radius-booking' ),
					'AMD' => __( 'Armenian dram', 'radius-booking' ),
					'ANG' => __( 'Netherlands Antillean guilder', 'radius-booking' ),
					'AOA' => __( 'Angolan kwanza', 'radius-booking' ),
					'ARS' => __( 'Argentine peso', 'radius-booking' ),
					'AUD' => __( 'Australian dollar', 'radius-booking' ),
					'AWG' => __( 'Aruban florin', 'radius-booking' ),
					'AZN' => __( 'Azerbaijani manat', 'radius-booking' ),
					'BAM' => __( 'Bosnia and Herzegovina convertible mark', 'radius-booking' ),
					'BBD' => __( 'Barbadian dollar', 'radius-booking' ),
					'BDT' => __( 'Bangladeshi taka', 'radius-booking' ),
					'BGN' => __( 'Bulgarian lev', 'radius-booking' ),
					'BHD' => __( 'Bahraini dinar', 'radius-booking' ),
					'BIF' => __( 'Burundian franc', 'radius-booking' ),
					'BMD' => __( 'Bermudian dollar', 'radius-booking' ),
					'BND' => __( 'Brunei dollar', 'radius-booking' ),
					'BOB' => __( 'Bolivian boliviano', 'radius-booking' ),
					'BRL' => __( 'Brazilian real', 'radius-booking' ),
					'BSD' => __( 'Bahamian dollar', 'radius-booking' ),
					'BTC' => __( 'Bitcoin', 'radius-booking' ),
					'BTN' => __( 'Bhutanese ngultrum', 'radius-booking' ),
					'BWP' => __( 'Botswana pula', 'radius-booking' ),
					'BYR' => __( 'Belarusian ruble (old)', 'radius-booking' ),
					'BYN' => __( 'Belarusian ruble', 'radius-booking' ),
					'BZD' => __( 'Belize dollar', 'radius-booking' ),
					'CAD' => __( 'Canadian dollar', 'radius-booking' ),
					'CDF' => __( 'Congolese franc', 'radius-booking' ),
					'CHF' => __( 'Swiss franc', 'radius-booking' ),
					'CLP' => __( 'Chilean peso', 'radius-booking' ),
					'CNY' => __( 'Chinese yuan', 'radius-booking' ),
					'COP' => __( 'Colombian peso', 'radius-booking' ),
					'CRC' => __( 'Costa Rican col&oacute;n', 'radius-booking' ),
					'CUC' => __( 'Cuban convertible peso', 'radius-booking' ),
					'CUP' => __( 'Cuban peso', 'radius-booking' ),
					'CVE' => __( 'Cape Verdean escudo', 'radius-booking' ),
					'CZK' => __( 'Czech koruna', 'radius-booking' ),
					'DJF' => __( 'Djiboutian franc', 'radius-booking' ),
					'DKK' => __( 'Danish krone', 'radius-booking' ),
					'DOP' => __( 'Dominican peso', 'radius-booking' ),
					'DZD' => __( 'Algerian dinar', 'radius-booking' ),
					'EGP' => __( 'Egyptian pound', 'radius-booking' ),
					'ERN' => __( 'Eritrean nakfa', 'radius-booking' ),
					'ETB' => __( 'Ethiopian birr', 'radius-booking' ),
					'EUR' => __( 'Euro', 'radius-booking' ),
					'FJD' => __( 'Fijian dollar', 'radius-booking' ),
					'FKP' => __( 'Falkland Islands pound', 'radius-booking' ),
					'GBP' => __( 'Pound sterling', 'radius-booking' ),
					'GEL' => __( 'Georgian lari', 'radius-booking' ),
					'GGP' => __( 'Guernsey pound', 'radius-booking' ),
					'GHS' => __( 'Ghana cedi', 'radius-booking' ),
					'GIP' => __( 'Gibraltar pound', 'radius-booking' ),
					'GMD' => __( 'Gambian dalasi', 'radius-booking' ),
					'GNF' => __( 'Guinean franc', 'radius-booking' ),
					'GTQ' => __( 'Guatemalan quetzal', 'radius-booking' ),
					'GYD' => __( 'Guyanese dollar', 'radius-booking' ),
					'HKD' => __( 'Hong Kong dollar', 'radius-booking' ),
					'HNL' => __( 'Honduran lempira', 'radius-booking' ),
					'HRK' => __( 'Croatian kuna', 'radius-booking' ),
					'HTG' => __( 'Haitian gourde', 'radius-booking' ),
					'HUF' => __( 'Hungarian forint', 'radius-booking' ),
					'IDR' => __( 'Indonesian rupiah', 'radius-booking' ),
					'ILS' => __( 'Israeli new shekel', 'radius-booking' ),
					'IMP' => __( 'Manx pound', 'radius-booking' ),
					'INR' => __( 'Indian rupee', 'radius-booking' ),
					'IQD' => __( 'Iraqi dinar', 'radius-booking' ),
					'IRR' => __( 'Iranian rial', 'radius-booking' ),
					'IRT' => __( 'Iranian toman', 'radius-booking' ),
					'ISK' => __( 'Icelandic kr&oacute;na', 'radius-booking' ),
					'JEP' => __( 'Jersey pound', 'radius-booking' ),
					'JMD' => __( 'Jamaican dollar', 'radius-booking' ),
					'JOD' => __( 'Jordanian dinar', 'radius-booking' ),
					'JPY' => __( 'Japanese yen', 'radius-booking' ),
					'KES' => __( 'Kenyan shilling', 'radius-booking' ),
					'KGS' => __( 'Kyrgyzstani som', 'radius-booking' ),
					'KHR' => __( 'Cambodian riel', 'radius-booking' ),
					'KMF' => __( 'Comorian franc', 'radius-booking' ),
					'KPW' => __( 'North Korean won', 'radius-booking' ),
					'KRW' => __( 'South Korean won', 'radius-booking' ),
					'KWD' => __( 'Kuwaiti dinar', 'radius-booking' ),
					'KYD' => __( 'Cayman Islands dollar', 'radius-booking' ),
					'KZT' => __( 'Kazakhstani tenge', 'radius-booking' ),
					'LAK' => __( 'Lao kip', 'radius-booking' ),
					'LBP' => __( 'Lebanese pound', 'radius-booking' ),
					'LKR' => __( 'Sri Lankan rupee', 'radius-booking' ),
					'LRD' => __( 'Liberian dollar', 'radius-booking' ),
					'LSL' => __( 'Lesotho loti', 'radius-booking' ),
					'LYD' => __( 'Libyan dinar', 'radius-booking' ),
					'MAD' => __( 'Moroccan dirham', 'radius-booking' ),
					'MDL' => __( 'Moldovan leu', 'radius-booking' ),
					'MGA' => __( 'Malagasy ariary', 'radius-booking' ),
					'MKD' => __( 'Macedonian denar', 'radius-booking' ),
					'MMK' => __( 'Burmese kyat', 'radius-booking' ),
					'MNT' => __( 'Mongolian t&ouml;gr&ouml;g', 'radius-booking' ),
					'MOP' => __( 'Macanese pataca', 'radius-booking' ),
					'MRU' => __( 'Mauritanian ouguiya', 'radius-booking' ),
					'MUR' => __( 'Mauritian rupee', 'radius-booking' ),
					'MVR' => __( 'Maldivian rufiyaa', 'radius-booking' ),
					'MWK' => __( 'Malawian kwacha', 'radius-booking' ),
					'MXN' => __( 'Mexican peso', 'radius-booking' ),
					'MYR' => __( 'Malaysian ringgit', 'radius-booking' ),
					'MZN' => __( 'Mozambican metical', 'radius-booking' ),
					'NAD' => __( 'Namibian dollar', 'radius-booking' ),
					'NGN' => __( 'Nigerian naira', 'radius-booking' ),
					'NIO' => __( 'Nicaraguan c&oacute;rdoba', 'radius-booking' ),
					'NOK' => __( 'Norwegian krone', 'radius-booking' ),
					'NPR' => __( 'Nepalese rupee', 'radius-booking' ),
					'NZD' => __( 'New Zealand dollar', 'radius-booking' ),
					'OMR' => __( 'Omani rial', 'radius-booking' ),
					'PAB' => __( 'Panamanian balboa', 'radius-booking' ),
					'PEN' => __( 'Sol', 'radius-booking' ),
					'PGK' => __( 'Papua New Guinean kina', 'radius-booking' ),
					'PHP' => __( 'Philippine peso', 'radius-booking' ),
					'PKR' => __( 'Pakistani rupee', 'radius-booking' ),
					'PLN' => __( 'Polish z&#x142;oty', 'radius-booking' ),
					'PRB' => __( 'Transnistrian ruble', 'radius-booking' ),
					'PYG' => __( 'Paraguayan guaran&iacute;', 'radius-booking' ),
					'QAR' => __( 'Qatari riyal', 'radius-booking' ),
					'RON' => __( 'Romanian leu', 'radius-booking' ),
					'RSD' => __( 'Serbian dinar', 'radius-booking' ),
					'RUB' => __( 'Russian ruble', 'radius-booking' ),
					'RWF' => __( 'Rwandan franc', 'radius-booking' ),
					'SAR' => __( 'Saudi riyal', 'radius-booking' ),
					'SBD' => __( 'Solomon Islands dollar', 'radius-booking' ),
					'SCR' => __( 'Seychellois rupee', 'radius-booking' ),
					'SDG' => __( 'Sudanese pound', 'radius-booking' ),
					'SEK' => __( 'Swedish krona', 'radius-booking' ),
					'SGD' => __( 'Singapore dollar', 'radius-booking' ),
					'SHP' => __( 'Saint Helena pound', 'radius-booking' ),
					'SLL' => __( 'Sierra Leonean leone', 'radius-booking' ),
					'SOS' => __( 'Somali shilling', 'radius-booking' ),
					'SRD' => __( 'Surinamese dollar', 'radius-booking' ),
					'SSP' => __( 'South Sudanese pound', 'radius-booking' ),
					'STN' => __( 'S&atilde;o Tom&eacute; and Pr&iacute;ncipe dobra', 'radius-booking' ),
					'SYP' => __( 'Syrian pound', 'radius-booking' ),
					'SZL' => __( 'Swazi lilangeni', 'radius-booking' ),
					'THB' => __( 'Thai baht', 'radius-booking' ),
					'TJS' => __( 'Tajikistani somoni', 'radius-booking' ),
					'TMT' => __( 'Turkmenistan manat', 'radius-booking' ),
					'TND' => __( 'Tunisian dinar', 'radius-booking' ),
					'TOP' => __( 'Tongan pa&#x2bb;anga', 'radius-booking' ),
					'TRY' => __( 'Turkish lira', 'radius-booking' ),
					'TTD' => __( 'Trinidad and Tobago dollar', 'radius-booking' ),
					'TWD' => __( 'New Taiwan dollar', 'radius-booking' ),
					'TZS' => __( 'Tanzanian shilling', 'radius-booking' ),
					'UAH' => __( 'Ukrainian hryvnia', 'radius-booking' ),
					'UGX' => __( 'Ugandan shilling', 'radius-booking' ),
					'USD' => __( 'United States (US) dollar', 'radius-booking' ),
					'UYU' => __( 'Uruguayan peso', 'radius-booking' ),
					'UZS' => __( 'Uzbekistani som', 'radius-booking' ),
					'VEF' => __( 'Venezuelan bol&iacute;var (2008–2018)', 'radius-booking' ),
					'VES' => __( 'Venezuelan bol&iacute;var', 'radius-booking' ),
					'VND' => __( 'Vietnamese &#x111;&#x1ed3;ng', 'radius-booking' ),
					'VUV' => __( 'Vanuatu vatu', 'radius-booking' ),
					'WST' => __( 'Samoan t&#x101;l&#x101;', 'radius-booking' ),
					'XAF' => __( 'Central African CFA franc', 'radius-booking' ),
					'XCD' => __( 'East Caribbean dollar', 'radius-booking' ),
					'XOF' => __( 'West African CFA franc', 'radius-booking' ),
					'XPF' => __( 'CFP franc', 'radius-booking' ),
					'YER' => __( 'Yemeni rial', 'radius-booking' ),
					'ZAR' => __( 'South African rand', 'radius-booking' ),
					'ZMW' => __( 'Zambian kwacha', 'radius-booking' ),
				)
			)
		);
	}

	return $currencies;
}

/**
 * Get all available Currency symbols.
 *
 * Currency symbols and names should follow the Unicode CLDR recommendation (https://cldr.unicode.org/translation/currency-names-and-symbols)
 *
 * @since 4.1.0
 * @return array
 */
function get_rtrb_currency_symbols() {

	$symbols = apply_filters(
		'radius_booking_currency_symbols',
		array(
			'AED' => '&#x62f;.&#x625;',
			'AFN' => '&#x60b;',
			'ALL' => 'L',
			'AMD' => 'AMD',
			'ANG' => '&fnof;',
			'AOA' => 'Kz',
			'ARS' => '&#36;',
			'AUD' => '&#36;',
			'AWG' => 'Afl.',
			'AZN' => '&#8380;',
			'BAM' => 'KM',
			'BBD' => '&#36;',
			'BDT' => '&#2547;&nbsp;',
			'BGN' => '&#1083;&#1074;.',
			'BHD' => '.&#x62f;.&#x628;',
			'BIF' => 'Fr',
			'BMD' => '&#36;',
			'BND' => '&#36;',
			'BOB' => 'Bs.',
			'BRL' => '&#82;&#36;',
			'BSD' => '&#36;',
			'BTC' => '&#3647;',
			'BTN' => 'Nu.',
			'BWP' => 'P',
			'BYR' => 'Br',
			'BYN' => 'Br',
			'BZD' => '&#36;',
			'CAD' => '&#36;',
			'CDF' => 'Fr',
			'CHF' => '&#67;&#72;&#70;',
			'CLP' => '&#36;',
			'CNY' => '&yen;',
			'COP' => '&#36;',
			'CRC' => '&#x20a1;',
			'CUC' => '&#36;',
			'CUP' => '&#36;',
			'CVE' => '&#36;',
			'CZK' => '&#75;&#269;',
			'DJF' => 'Fr',
			'DKK' => 'kr.',
			'DOP' => 'RD&#36;',
			'DZD' => '&#x62f;.&#x62c;',
			'EGP' => 'EGP',
			'ERN' => 'Nfk',
			'ETB' => 'Br',
			'EUR' => '&euro;',
			'FJD' => '&#36;',
			'FKP' => '&pound;',
			'GBP' => '&pound;',
			'GEL' => '&#x20be;',
			'GGP' => '&pound;',
			'GHS' => '&#x20b5;',
			'GIP' => '&pound;',
			'GMD' => 'D',
			'GNF' => 'Fr',
			'GTQ' => 'Q',
			'GYD' => '&#36;',
			'HKD' => '&#36;',
			'HNL' => 'L',
			'HRK' => 'kn',
			'HTG' => 'G',
			'HUF' => '&#70;&#116;',
			'IDR' => 'Rp',
			'ILS' => '&#8362;',
			'IMP' => '&pound;',
			'INR' => '&#8377;',
			'IQD' => '&#x62f;.&#x639;',
			'IRR' => '&#xfdfc;',
			'IRT' => '&#x062A;&#x0648;&#x0645;&#x0627;&#x0646;',
			'ISK' => 'kr.',
			'JEP' => '&pound;',
			'JMD' => '&#36;',
			'JOD' => '&#x62f;.&#x627;',
			'JPY' => '&yen;',
			'KES' => 'KSh',
			'KGS' => '&#x441;&#x43e;&#x43c;',
			'KHR' => '&#x17db;',
			'KMF' => 'Fr',
			'KPW' => '&#x20a9;',
			'KRW' => '&#8361;',
			'KWD' => '&#x62f;.&#x643;',
			'KYD' => '&#36;',
			'KZT' => '&#8376;',
			'LAK' => '&#8365;',
			'LBP' => '&#x644;.&#x644;',
			'LKR' => '&#xdbb;&#xdd4;',
			'LRD' => '&#36;',
			'LSL' => 'L',
			'LYD' => '&#x62f;.&#x644;',
			'MAD' => '&#x62f;.&#x645;.',
			'MDL' => 'MDL',
			'MGA' => 'Ar',
			'MKD' => '&#x434;&#x435;&#x43d;',
			'MMK' => 'Ks',
			'MNT' => '&#x20ae;',
			'MOP' => 'P',
			'MRU' => 'UM',
			'MUR' => '&#x20a8;',
			'MVR' => '.&#x783;',
			'MWK' => 'MK',
			'MXN' => '&#36;',
			'MYR' => '&#82;&#77;',
			'MZN' => 'MT',
			'NAD' => 'N&#36;',
			'NGN' => '&#8358;',
			'NIO' => 'C&#36;',
			'NOK' => '&#107;&#114;',
			'NPR' => '&#8360;',
			'NZD' => '&#36;',
			'OMR' => '&#x631;.&#x639;.',
			'PAB' => 'B/.',
			'PEN' => 'S/',
			'PGK' => 'K',
			'PHP' => '&#8369;',
			'PKR' => '&#8360;',
			'PLN' => '&#122;&#322;',
			'PRB' => '&#x440;.',
			'PYG' => '&#8370;',
			'QAR' => '&#x631;.&#x642;',
			'RMB' => '&yen;',
			'RON' => 'lei',
			'RSD' => '&#1088;&#1089;&#1076;',
			'RUB' => '&#8381;',
			'RWF' => 'Fr',
			'SAR' => '&#x631;.&#x633;',
			'SBD' => '&#36;',
			'SCR' => '&#x20a8;',
			'SDG' => '&#x62c;.&#x633;.',
			'SEK' => '&#107;&#114;',
			'SGD' => '&#36;',
			'SHP' => '&pound;',
			'SLL' => 'Le',
			'SOS' => 'Sh',
			'SRD' => '&#36;',
			'SSP' => '&pound;',
			'STN' => 'Db',
			'SYP' => '&#x644;.&#x633;',
			'SZL' => 'E',
			'THB' => '&#3647;',
			'TJS' => '&#x405;&#x41c;',
			'TMT' => 'm',
			'TND' => '&#x62f;.&#x62a;',
			'TOP' => 'T&#36;',
			'TRY' => '&#8378;',
			'TTD' => '&#36;',
			'TWD' => '&#78;&#84;&#36;',
			'TZS' => 'Sh',
			'UAH' => '&#8372;',
			'UGX' => 'UGX',
			'USD' => '&#36;',
			'UYU' => '&#36;',
			'UZS' => 'UZS',
			'VEF' => 'Bs F',
			'VES' => 'Bs.',
			'VND' => '&#8363;',
			'VUV' => 'Vt',
			'WST' => 'T',
			'XAF' => 'CFA',
			'XCD' => '&#36;',
			'XOF' => 'CFA',
			'XPF' => 'Fr',
			'YER' => '&#xfdfc;',
			'ZAR' => '&#82;',
			'ZMW' => 'ZK',
		)
	);

	return $symbols;
}


/**
 * Get Currency symbol.
 *
 * Currency symbols and names should follow the Unicode CLDR recommendation (https://cldr.unicode.org/translation/currency-names-and-symbols)
 *
 * @param string $currency Currency. (default: '').
 * @return string
 */
function get_rtrb_currency_symbol( $currency = '' ) {
	if ( ! $currency ) {
		$currency = get_rtrb_currency();
	}
	$symbols = get_rtrb_currency_symbols();

	$currency_symbol = isset( $symbols[ $currency['symbol'] ] ) ? $symbols[ $currency['symbol'] ] : '';
	$currency_symbol = html_entity_decode( $currency_symbol );
	return apply_filters( 'radius_booking_currency_symbol', $currency_symbol, $currency );
}

/**
 * Decodes Unicode escape sequences in a string to their corresponding UTF-8 characters.
 *
 * @param string $str The input string containing Unicode escape sequences (e.g., \uXXXX).
 *
 * @return string The decoded string with Unicode sequences replaced by their UTF-8 characters.
 */
function rtrb_decode_unicode_sequences( $str ) {
	return preg_replace_callback(
		'/\\\\u([0-9a-fA-F]{4})/',
		function ( $matches ) {
			return mb_convert_encoding( pack( 'H*', $matches[1] ), 'UTF-8', 'UCS-2BE' );
		},
		$str
	);
}

/**
 * Check if theme support is available.
 *
 * @since 1.0.0
 * @return bool
 */
function rtrb_theme_support_available_for_booking() {
	$supported_theme = array(
		'twentytwentyone',
		'twentytwentytwo',
		'astra',
		'blocksy',
		'generatepress',
		'kadence',
		'oceanwp',
		'storefront',
		'twentynineteen',
		'twentyseventeen',
		'twentysixteen',
		'twentytwenty',
		'helloelementor',
	);
	return in_array( get_template(), $supported_theme, true );
}

/**
 * Formats a duration array into a human-readable string with hours, minutes, and seconds.
 *
 * @param array $duration An associative array containing the duration components.
 *                        Possible keys are 'hour', 'min', and 'sec', with their respective numeric values.
 *
 * @return string The formatted duration string in the format 'X hr Y min Z sec'.
 */
function radius_booking_format_duration( $duration ) {
	$formatted_duration = '';
	if ( isset( $duration['hour'] ) ) {
		$formatted_duration .= "{$duration['hour']} hr ";
	}
	if ( isset( $duration['min'] ) ) {
		$formatted_duration .= "{$duration['min']} min ";
	}
	if ( isset( $duration['sec'] ) ) {
		$formatted_duration .= "{$duration['sec']} sec";
	}
	return $formatted_duration;
}

/**
 * Determines the current country location based on various fallback methods, such as GeoIP detection,
 * WooCommerce geolocation, Cloudflare headers, proxy headers, WooCommerce billing address, or WordPress locale.
 *
 * The function returns a two-letter country ISO code. If no specific location is determined, it defaults to 'US'.
 *
 * @return string The two-letter ISO country code for the current location, filtered through the 'radius_booking_current_country' filter.
 */
function get_rtrb_current_location() {
	$default  = 'US';
	$location = $default;

	// 1) GeoIP Detect plugin (geoip-detect)
	if ( function_exists( 'geoip_detect2_get_info_from_current_ip' ) ) {
		try {
			$geo = geoip_detect2_get_info_from_current_ip();
			if ( ! empty( $geo->country->isoCode ) ) {
				$location = strtoupper( (string) $geo->country->isoCode );
				return apply_filters( 'radius_booking_current_country', $location );
			}
		} catch ( Exception $e ) {
			// ignore and continue to other fallbacks
		}
	}

	// 2) WooCommerce geolocation (correct check)
	if ( class_exists( 'WC_Geolocation' ) && is_callable( array( 'WC_Geolocation', 'geolocate_ip' ) ) ) {
		$geo = WC_Geolocation::geolocate_ip();
		if ( is_array( $geo ) && ! empty( $geo['country'] ) ) {
			$location = strtoupper( (string) $geo['country'] );
			return apply_filters( 'radius_booking_current_country', $location );
		}
	}

	// 3) Cloudflare header (very common when behind Cloudflare)
	if ( ! empty( $_SERVER['HTTP_CF_IPCOUNTRY'] ) ) {
		$country = strtoupper( sanitize_text_field( wp_unslash( $_SERVER['HTTP_CF_IPCOUNTRY'] ) ) );
		if ( strlen( $country ) === 2 ) {
			$location = $country;
			return apply_filters( 'radius_booking_current_country', $location );
		}
	}

	// 4) Other possible proxy headers (adjust to your infra if needed)
	$proxy_headers = array( 'HTTP_X_COUNTRY_CODE', 'HTTP_X_COUNTRY' );
	foreach ( $proxy_headers as $h ) {
		if ( ! empty( $_SERVER[ $h ] ) ) {
			$country = strtoupper( sanitize_text_field( wp_unslash( $_SERVER[ $h ] ) ) );
			if ( strlen( $country ) === 2 ) {
				$location = $country;
				return apply_filters( 'radius_booking_current_country', $location );
			}
		}
	}

	// 5) If WooCommerce is active, try customer billing country (if available)
	if ( function_exists( 'WC' ) && isset( WC()->customer ) ) {
		$billing = WC()->customer->get_billing_country();
		if ( ! empty( $billing ) ) {
			$location = strtoupper( (string) $billing );
			return apply_filters( 'radius_booking_current_country', $location );
		}
	}

	// 6) Try deriving from WP locale (en_US => US)
	$locale = get_locale();
	if ( $locale && preg_match( '/[_-]([A-Za-z]{2})$/', $locale, $m ) ) {
		$location = strtoupper( $m[1] );
		return apply_filters( 'radius_booking_current_country', $location );
	}

	// Final fallback
	return apply_filters( 'radius_booking_current_country', $location );
}

function rtrb_get_current_url() {
	global $wp;

	// Try to get from referer first (more reliable for AJAX).
	if ( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
		return esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) );
	}

	// Fallback to home URL with current request.
	return home_url( add_query_arg( array(), $wp->request ) );
}

/**
 * Checks if the Radius Booking Pro plugin is active by verifying if its version constant is defined.
 *
 * @return bool True if the RADIUS_BOOKING_PRO_VERSION constant is defined, indicating the plugin is active. False otherwise.
 */
function rtrb_pro_active() {
//    return true;
	if ( defined( 'RADIUS_BOOKING_PRO_VERSION' ) ) {
		return true;
	}
	return false;
}

/**
 * Checks if a booking module is active.
 *
 * Service is always active. Meeting and Event are controlled via Settings > Modules.
 *
 * @since 1.2.0
 *
 * @param string $module The module name: 'service', 'meeting', or 'event'.
 *
 * @return bool True if the module is active, false otherwise.
 */
function rtrb_is_module_active( string $module ): bool {
	if ( 'service' === $module ) {
		return true;
	}
	$modules = \RadiusTheme\RadiusBooking\Helpers\SettingsHelper::get_setting( 'modules' );
	return ! empty( $modules[ $module ] );
}
