<?php

namespace SugarCalendarBookings\Support\Helpers;

/**
 * Class WP provides WordPress shortcuts.
 *
 * @since 1.0.0
 */
class WP {

	/**
	 * Get the postfix for assets files - ".min" or empty.
	 * ".min" if in production mode.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public static function asset_min() {

		$min = '.min';

		if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
			$min = '';
		}

		return $min;
	}

	/**
	 * Wrapper for the WP `admin_url` method that should be used in the plugin.
	 *
	 * We can filter into it, to maybe call `network_admin_url` for multisite support.
	 *
	 * @since 1.0.0
	 *
	 * @param string $path   Optional path relative to the admin URL.
	 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
	 *                       'http' or 'https' can be passed to force those schemes.
	 *
	 * @return string Admin URL link with optional path appended.
	 */
	public static function admin_url( $path = '', $scheme = 'admin' ) {

		/**
		 * Filter the admin url.
		 *
		 * @since 1.0.0
		 *
		 * @param string $url    Admin url.
		 * @param string $path   Optional path relative to the admin URL.
		 * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
		 *                       'http' or 'https' can be passed to force those schemes.
		 */
		return apply_filters( 'sugar_calendar_bookings_admin_url', admin_url( $path, $scheme ), $path, $scheme ); // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
	}

	/**
	 * Builds an AJAX url.
	 *
	 * @since 1.0.0
	 *
	 * @return string
	 */
	public static function ajax_url() {

		return add_query_arg(
			[
				'_wpnonce' => wp_create_nonce( 'sugar_calendar_bookings' ),
				'action'   => 'sugar_calendar_bookings',
			],
			self::admin_url( 'admin-ajax.php' )
		);
	}

	/**
	 * Redirect with a dashboard notice.
	 *
	 * @since 1.0.0
	 *
	 * @param string $url         Redirect URL.
	 * @param string $notice      Notice slug.
	 * @param string $notice_data Notice data.
	 */
	public static function redirect_with_notice( $url, $notice, $notice_data = [] ) {

		$query = [
			'scbookings-notice' => $notice,
		];

		if ( ! empty( $notice_data ) ) {
			$query['scbookings-notice-data'] = $notice_data;
		}

		wp_safe_redirect( add_query_arg( $query, $url ) );
		exit();
	}

	/**
	 * Check if current environment is local.
	 *
	 * @since 1.2.0
	 *
	 * @return bool
	 */
	public static function is_local_environment() { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh

		$site_url = get_site_url();
		$host     = wp_parse_url( $site_url, PHP_URL_HOST );
		$is_local = false;

		// Check for common local development indicators.
		$local_hosts = [
			'localhost',
			'127.0.0.1',
			'::1',
		];

		if ( in_array( $host, $local_hosts, true ) ) {
			$is_local = true;
		}

		// Check for common local TLDs.
		$local_tlds = [
			'.local',
			'.test',
			'.localhost',
			'.invalid',
			'.example',
			'.dev',
		];

		foreach ( $local_tlds as $tld ) {
			if ( substr( $host, -strlen( $tld ) ) === $tld ) {
				$is_local = true;
			}
		}

		// Check WordPress environment type.
		if ( function_exists( 'wp_get_environment_type' ) ) {
			$env = wp_get_environment_type();

			if ( in_array( $env, [ 'local', 'development' ], true ) ) {
				$is_local = true;
			}
		}

		/**
		 * Filter whether the site is a local installation.
		 *
		 * @since 1.2.0
		 *
		 * @param bool $is_local Whether the environment is local.
		 */
		return apply_filters( 'sugar_calendar_bookings_is_local_environment', $is_local ); // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
	}
}
