<?php

use WP_science\Buffer\Cache;
use WP_science\Buffer\Config;
use WP_science\Buffer\Tests;

defined( 'ABSPATH' ) || exit;

define( 'WP_science_ADVANCED_CACHE', true );

$science_path        = '{{WP_science_PATH}}';
$science_config_path = '{{WP_science_CONFIG_PATH}}';
$science_cache_path  = '{{WP_science_CACHE_PATH}}';

if (
	version_compare( phpversion(), '{{WP_science_PHP_VERSION}}', '<' )
	|| ! file_exists( $science_path )
	|| ! file_exists( $science_config_path )
	|| ! file_exists( $science_cache_path )
) {
	define( 'WP_science_ADVANCED_CACHE_PROBLEM', true );
	return;
}

'{{MOBILE_CACHE}}';
if ( file_exists( '{{WP_science_PATH}}inc/classes/dependencies/mobiledetect/mobiledetectlib/Mobile_Detect.php' ) && ! class_exists( 'WP_science_Mobile_Detect' ) ) {
	include_once '{{WP_science_PATH}}inc/classes/dependencies/mobiledetect/mobiledetectlib/Mobile_Detect.php';
}
'{{/MOBILE_CACHE}}';

spl_autoload_register(
	function ( $class ) use ( $science_path ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.classFound
		$science_classes = [
			'WP_science\\Buffer\\Abstract_Buffer' => [ $science_path . 'inc/classes/Buffer/class-abstract-buffer.php' ],
			'WP_science\\Buffer\\Cache'           => [ $science_path . 'inc/classes/Buffer/class-cache.php' ],
			'WP_science\\Buffer\\Tests'           => [ $science_path . 'inc/classes/Buffer/class-tests.php' ],
			'WP_science\\Buffer\\Config'          => [ $science_path . 'inc/classes/Buffer/class-config.php' ],
			'WP_science\\Logger\\HTMLFormatter'   => [
				$science_path . 'inc/Logger/HTMLFormatter.php',
				$science_path . 'inc/classes/logger/class-html-formatter.php',
			],
			'WP_science\\Logger\\Logger'          => [
				$science_path . 'inc/Logger/Logger.php',
				$science_path . 'inc/classes/logger/class-logger.php',
			],
			'WP_science\\Logger\\StreamHandler'   => [
				$science_path . 'inc/Logger/StreamHandler.php',
				$science_path . 'inc/classes/logger/class-stream-handler.php',
			],
			'WP_science\\Traits\\Memoize'         => [ $science_path . 'inc/classes/traits/trait-memoize.php' ],
		];

		if ( isset( $science_classes[ $class ] ) ) {
			$file_options = $science_classes[ $class ];
			$file         = '';

			foreach ( $file_options as $file_option ) {
				if ( file_exists( $file_option ) ) {
					$file = $file_option;
					break;
				}
			}
		} elseif ( strpos( $class, 'WP_science\\Dependencies\\Monolog\\' ) === 0 ) {
			$file = $science_path . 'inc/Dependencies/Monolog/' . str_replace( '\\', '/', $class ) . '.php';
			if ( ! file_exists( $file ) ) {
				$file = $science_path . 'vendor/monolog/monolog/src/' . str_replace( '\\', '/', $class ) . '.php';
			}
		} elseif ( strpos( $class, 'WP_science\\Dependencies\\Psr\\Log\\' ) === 0 ) {
			$file = $science_path . 'inc/Dependencies/Psr/Log/' . str_replace( '\\', '/', $class ) . '.php';
			if ( ! file_exists( $file ) ) {
				$file = $science_path . 'vendor/psr/log/' . str_replace( '\\', '/', $class ) . '.php';
			}
		} else {
			return;
		}

		if ( file_exists( $file ) ) {
			require $file;
		}
	}
);

if ( ! class_exists( '\WP_science\Buffer\Cache' ) ) {
	if ( ! defined( 'DONOTscienceOPTIMIZE' ) ) {
		define( 'DONOTscienceOPTIMIZE', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound
	}
	return;
}

$science_config_class = new Config(
	[
		'config_dir_path' => $science_config_path,
	]
);

( new Cache(
	new Tests(
		$science_config_class
	),
	$science_config_class,
	[
		'cache_dir_path' => $science_cache_path,
	]
) )->maybe_init_process();
