<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Listener;

final class ListenerPriorityQueue
{
    private array $listeners = [];

    public function add(EventListenerDefinition $definition): void
    {
        $this->listeners[] = $definition;
        usort(
            $this->listeners,
            static fn (EventListenerDefinition $a, EventListenerDefinition $b): int => $b->priority() <=> $a->priority()
        );
    }

    public function all(): array
    {
        return $this->listeners;
    }

    public function count(): int
    {
        return count($this->listeners);
    }

    public function toArray(): array
    {
        return array_map(static fn (EventListenerDefinition $definition): array => $definition->toArray(), $this->listeners);
    }
}
