<?php

// autoload.php @generated by Composer
// MODIFIED by Alsendo: blacklist conflicting namespaces and append (not prepend)
// the loader so PrestaShop core's autoloader keeps precedence for shared libs
// (notably nikic/php-parser v4 used by PS's ModuleDataProvider).
// Without this, PS 8.x's AppKernel::enableComposerAutoloaderOnModules() loads
// this file directly — bypassing alsendo.php and its bootstrapAutoloader()
// — causing our v5 PhpParser to shadow PS's v4 and crash the modules manager.

if (PHP_VERSION_ID < 50600) {
    if (!headers_sent()) {
        header('HTTP/1.1 500 Internal Server Error');
    }
    $err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
    if (!ini_get('display_errors')) {
        if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
            fwrite(STDERR, $err);
        } elseif (!headers_sent()) {
            echo $err;
        }
    }
    throw new RuntimeException($err);
}

require_once __DIR__ . '/composer/autoload_real.php';

$alsendoLoader = ComposerAutoloaderInitca0a58f001e69ab8333dbb5126757e66::getLoader();

// Apply Alsendo vendor isolation as soon as this autoload.php is required —
// the loader is already registered (with prepend=true) by Composer at this
// point, so we both blacklist conflicting namespaces AND move ourselves to
// the end of the spl chain so PS's core autoloader (with PhpParser v4) wins.
if ($alsendoLoader instanceof \Composer\Autoload\ClassLoader) {
    static $alsendoBootstrapped = false;
    if (!$alsendoBootstrapped) {
        $alsendoBootstrapped = true;

        // Dev / transitive namespaces that must NOT leak into PrestaShop's
        // global autoloader. PhpParser is the primary offender: our vendor
        // ships v5 (via phpunit/php-code-coverage), PS bundles v4, and
        // PS core calls v4-only ParserFactory::create() in ModuleDataProvider.
        $alsendoBlacklist = [
            'PhpParser\\',
            'PHPUnit\\',
            'PharIo\\',
            'SebastianBergmann\\',
            'TheSeer\\',
            'Faker\\',
            'Dotenv\\',
            'PhpOption\\',
            'Doctrine\\Instantiator\\',
            'DeepCopy\\',
        ];
        foreach ($alsendoBlacklist as $alsendoPrefix) {
            $alsendoLoader->setPsr4($alsendoPrefix, []);
        }

        // setPsr4() clears PSR-4 paths; classmap entries (PHPUnit /
        // SebastianBergmann / TheSeer / PharIo) need separate cleanup.
        try {
            $alsendoClassMapRef = new \ReflectionProperty(\Composer\Autoload\ClassLoader::class, 'classMap');
            $alsendoClassMapRef->setAccessible(true);
            $alsendoClassMap = $alsendoClassMapRef->getValue($alsendoLoader);
            if (is_array($alsendoClassMap)) {
                foreach (array_keys($alsendoClassMap) as $alsendoClass) {
                    foreach ($alsendoBlacklist as $alsendoPrefix) {
                        if (strpos($alsendoClass, $alsendoPrefix) === 0) {
                            unset($alsendoClassMap[$alsendoClass]);
                            break;
                        }
                    }
                }
                $alsendoClassMapRef->setValue($alsendoLoader, $alsendoClassMap);
            }
        } catch (\ReflectionException $alsendoReflectException) {
            // Classmap clean-up is best-effort; PSR-4 blacklist already
            // resolves the primary PhpParser conflict.
        }

        // PS 1.7.x ships Guzzle 5; our wrapper requires Guzzle 7. Loading
        // Guzzle 7 globally breaks PS's CsaGuzzleBundle on 1.7.x. Wrapper
        // ApiClient handles both Guzzle versions via runtime detection.
        if (defined('_PS_VERSION_') && version_compare(_PS_VERSION_, '8.0.0', '<')) {
            $alsendoLoader->setPsr4('GuzzleHttp\\', []);
            $alsendoLoader->setPsr4('GuzzleHttp\\Psr7\\', []);
            $alsendoLoader->setPsr4('GuzzleHttp\\Promise\\', []);
        }

        // Composer 2.x registers our ClassLoader with prepend=true, which
        // puts us AHEAD of PrestaShop's core autoloader in the spl chain —
        // meaning our (blacklisted) loader is queried first for every class.
        // Re-register at the END so PS core resolves shared classes from its
        // own vendor (e.g. PhpParser\ParserFactory v4) before falling back
        // to us. Alsendo\* namespace remains unique to us so this is safe.
        spl_autoload_unregister([$alsendoLoader, 'loadClass']);
        spl_autoload_register([$alsendoLoader, 'loadClass'], true, false);
    }
}

return $alsendoLoader;
