<?php

declare(strict_types=1);

namespace App\Core;

final class App
{
    /** @var array<string, mixed> */
    private static array $config = [];

    public static function boot(): void
    {
        self::$config = require dirname(__DIR__, 2) . '/config.php';
        date_default_timezone_set(self::config('app')['timezone']);

        if (self::config('app')['debug']) {
            ini_set('display_errors', '1');
            error_reporting(E_ALL);
        } else {
            ini_set('display_errors', '0');
        }

        require __DIR__ . '/polyfills.php';

        // PSR-4 style autoloader for the App\ namespace.
        spl_autoload_register(function (string $class): void {
            if (!str_starts_with($class, 'App\\')) {
                return;
            }
            $path = dirname(__DIR__) . '/' . str_replace('\\', '/', substr($class, 4)) . '.php';
            if (file_exists($path)) {
                require $path;
            }
        });
    }

    /** @return array<string, mixed> */
    public static function config(string $key): array
    {
        return self::$config[$key] ?? [];
    }
}
