<?php
namespace Misuzu;

use Index\Http\Routing\{HttpRouter,IRouter,IRouteHandler};
use Misuzu\URLs\{IURLSource,URLInfo,URLRegistry};

class RoutingContext {
    private URLRegistry $urls;
    private HttpRouter $router;

    public function __construct() {
        $this->urls = new URLRegistry;
        $this->router = new HttpRouter(errorHandler: new RoutingErrorHandler);
        $this->router->use('/', fn($resp) => $resp->setPoweredBy('Misuzu'));
    }

    public function getURLs(): URLRegistry {
        return $this->urls;
    }

    public function getRouter(): IRouter {
        return $this->router;
    }

    public function register(IRouteHandler|IURLSource $handler): void {
        if($handler instanceof IRouteHandler)
            $this->router->register($handler);
        if($handler instanceof IURLSource)
            $handler->registerURLs($this->urls);
        URLInfo::handleAttributes($this->urls, $handler);
    }

    public function dispatch(...$args): void {
        $this->router->dispatch(...$args);
    }
}
