<?php
declare(strict_types=1);

namespace Help4BuilderSuite;

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

final class Autoloader
{
    private const PREFIX = 'Help4BuilderSuite\\';

    public static function register(string $baseDir): void
    {
        $baseDir = rtrim($baseDir, '/\\') . DIRECTORY_SEPARATOR;

        spl_autoload_register(
            static function (string $class) use ($baseDir): void {
                if (strncmp(self::PREFIX, $class, strlen(self::PREFIX)) !== 0) {
                    return;
                }

                $relativeClass = substr($class, strlen(self::PREFIX));
                if ($relativeClass === false) {
                    return;
                }

                $file = $baseDir . str_replace('\\', DIRECTORY_SEPARATOR, $relativeClass) . '.php';
                if (is_readable($file)) {
                    require_once $file;
                }
            }
        );
    }
}
