<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Modules;

final class ModuleCollection
{
    /** @var array<string, ModuleRecord> */
    private array $items = [];

    public function add(ModuleRecord $module): void
    {
        $this->items[$module->id()] = $module;
    }

    public function has(string $id): bool { return isset($this->items[$id]); }
    public function get(string $id): ?ModuleRecord { return $this->items[$id] ?? null; }
    public function all(): array { return array_values($this->items); }

    public function enabled(): array
    {
        return array_values(array_filter($this->items, static fn (ModuleRecord $m): bool => $m->enabled()));
    }

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

    public function toArray(): array
    {
        return array_map(static fn (ModuleRecord $module): array => $module->toArray(), $this->all());
    }
}
