<?php

declare(strict_types=1);

namespace App\Core;

final class App
{
    public function __construct(
        private Router $router,
        private View $view
    ) {}

    public function run(Request $request): void
    {
        $route = $this->router->dispatch($request);
        $result = ($route['controller'])(...array_values($route['params']));

        if ($result instanceof Response) {
            $result->send();
            return;
        }

        if (is_array($result)) {
            Response::json($result)->send();
            return;
        }

        Response::html((string) $result)->send();
    }

    public function view(): View
    {
        return $this->view;
    }
}
