<?php
/**
 * PYS_PRO_GLOBAL Namespace Autoloader
 * This autoloader handles PYS_PRO_GLOBAL namespace for prefixed vendor packages
 * It works alongside Composer's autoloader
 */

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

// Only register if not already registered
if ( ! function_exists( 'orderflow_load_pys_global_autoloader' ) ) {
    
    function orderflow_load_pys_global_autoloader() {
        $vendorPath = dirname( __FILE__ );
        
        spl_autoload_register( function ( $class_name ) use ( $vendorPath ) {
            
            // Only handle PYS_PRO_GLOBAL namespace
            if ( 0 !== strpos( $class_name, 'PYS_PRO_GLOBAL\\' ) ) {
                return;
            }

            // Remove PYS_PRO_GLOBAL\ prefix
            $relative_class = substr( $class_name, strlen( 'PYS_PRO_GLOBAL\\' ) );
            
            // Map namespaces to directories in vendor folder
            $namespace_map = array(
                'FacebookAds\\' => 'facebook/php-business-sdk/src/FacebookAds/',
                'GuzzleHttp\\' => 'guzzlehttp/guzzle/src/',
                'Psr\\' => 'psr/',
            );
            
            $file_path = null;
            
            // Check each namespace mapping
            foreach ( $namespace_map as $namespace => $directory ) {
                if ( 0 === strpos( $relative_class, $namespace ) ) {
                    $relative_path = substr( $relative_class, strlen( $namespace ) );
                    $file_path = $vendorPath . '/' . $directory . str_replace( '\\', '/', $relative_path ) . '.php';
                    break;
                }
            }
            
            // Handle special cases
            if ( ! $file_path ) {
                // For ralouphie/getallheaders
                if ( $relative_class === 'getallheaders' ) {
                    $file_path = $vendorPath . '/ralouphie/getallheaders/src/getallheaders.php';
                }
            }
            
            // Load file if exists
            if ( $file_path && file_exists( $file_path ) ) {
                require_once $file_path;
                return;
            }
            
        }, true, true ); // Prepend to autoload stack and throw on error
        
    }
    
    orderflow_load_pys_global_autoloader();
}



