<?php
/**
 * Autoloads classes for the deploy-sync-content plugin.
 *
 * @package EaLab\WP_Deployer
 */

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

spl_autoload_register(
	function ( $class_name ): void {
		if ( 0 === strpos( $class_name, 'EaLab\WP_Deployer\vendor\\' ) ) {
			// Do not load composer dependencies; they are autoloaded by composer.
			return;
		}

		$mapping = array(
			'EaLab\WP_Deployer\includes\Rest\\' => 'includes/rest-api',
			'EaLab\WP_Deployer\includes\\' => 'includes',
			'EaLab\WP_Deployer\\' => 'src',
		);

		foreach ( $mapping as $namespace => $dir ) {
			if ( 0 !== strpos( $class_name, $namespace ) ) {
				continue;
			}

			$src_base_dir = realpath( plugin_dir_path( __FILE__ ) ) . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR;
			$class_file   = str_replace(
				'\\',
				DIRECTORY_SEPARATOR,
				substr( $class_name, strlen( $namespace ) )
			) . '.php';
			require_once $src_base_dir . $class_file;
			break;
		}
	},
	true,
	true
);

// Vendor autoloader.
require_once __DIR__ . '/vendor/_prefixed/autoload.php';
