<?php

namespace App\Framework\EventBus;

use App\Framework\DI\Container;
use App\Framework\DI\Initializer;
use App\Framework\Discovery\Results\DiscoveryResults;

final readonly class EventBusInitializer
{
    public function __construct(
        private Container        $container,
        private DiscoveryResults $results
    ){}

    #[Initializer]
    public function __invoke(): EventBus
    {
        $handlers = $this->results->get(EventHandler::class);

        $eventHandlers = [];
        foreach ($handlers as $handler) {
            $eventHandlers[$handler['event_class']] = $handler;
        }

        return new DefaultEventBus($eventHandlers, $this->container);
    }
}
