<?php
defined('ABSPATH') || exit; // Prevent direct file access.

// Define the base namespace and its path
defined('SMART_GIFT_CARD_BASE_NAMESPACE') || define('SMART_GIFT_CARD_BASE_NAMESPACE', 'SGC\\eGiftCard');
defined('SMART_GIFT_CARD_BASE_NAMESPACE_PATH') || define('SMART_GIFT_CARD_BASE_NAMESPACE_PATH', __DIR__ . '/app');

// Define the classmap using the constants
$classmap = [
    SMART_GIFT_CARD_BASE_NAMESPACE => SMART_GIFT_CARD_BASE_NAMESPACE_PATH,
];

// Register the autoloader
spl_autoload_register(function ($classname) use ($classmap) {
    // Iterate over the classmap to find the matching namespace
    foreach ($classmap as $namespace => $base_dir) {
        // Check if the class uses the namespace prefix
        if (strpos($classname, $namespace) === 0) {
            // Get the relative class name
            $relative_class = substr($classname, strlen($namespace));

            // Replace namespace separators with directory separators in the relative class name, append with .php
            $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

            // If the file exists, require it
            if (file_exists($file)) {
                require $file;
            }
        }
    }
});
