<?php
namespace WPDRMS\ASP;

if ( !defined('ABSPATH') ) {
	die('-1');
}

// Short-circuit in normal operation: Composer's vendor/autoload.php (loaded at
// ajax-search-pro.php:48) already handles these namespaces via PSR-4.
if ( class_exists('WPDRMS\\PluginCore\\ORM\\Model') ) {
	return;
}

// Registers a path-based fallback for the three shared-package namespaces.
// Needed when an old autoloader (≤4.28.4) is still in PHP memory during a plugin
// update: the old vendor/autoload.php was already require_once'd at boot, so
// re-requiring it is a no-op. Registering a fresh spl_autoload function sidesteps
// that — this file is new, so require_once of its path is never a no-op.
spl_autoload_register(function ( $class ) {
	static $map = null;
	if ( $map === null ) {
		$map = array(
			'WPDRMS\\PluginCore\\' => ASP_PATH . 'vendor/wpdreams/plugin-core/src/',
			'WPDRMS\\AdminUI\\'    => ASP_PATH . 'vendor/wpdreams/admin-ui/src/',
			'WPDRMS\\Utils\\'      => ASP_PATH . 'vendor/wpdreams/php-utils/src/',
		);
	}
	foreach ( $map as $prefix => $base ) {
		$len = strlen($prefix);
		if ( strncmp($prefix, $class, $len) !== 0 ) {
			continue;
		}
		$file = $base . str_replace('\\', '/', substr($class, $len)) . '.php';
		if ( file_exists($file) ) {
			require $file;
		}
		return;
	}
} );
