<?php

declare(strict_types=1);

namespace Apexl\Io\Tabulator\Collections;

use Apexl\Io\Menu\Dtos\MenuMetadata;
use Apexl\Io\Menu\Interfaces\HasMenuEntryInterface;

/**
 * Menu metadata, read once per entity.
 *
 * Each entity's metadata is wanted twice — once for the data routes' permission, once for the
 * account route — and reading it can construct the entity on packages still using the
 * deprecated instance getters, which is worth avoiding a hundred and forty-six times over.
 *
 * It is an object rather than an array on the module because the module is readonly: a
 * readonly property may not be reassigned, and writing a key into an array counts as
 * reassigning it. Mutating the object a readonly property points at does not.
 */
final class MenuMetadataCache
{
    /** @var array<class-string<HasMenuEntryInterface>, MenuMetadata> */
    private array $entries = [];

    /**
     * @param class-string<HasMenuEntryInterface> $entityClass
     */
    public function for(string $entityClass): MenuMetadata
    {
        return $this->entries[$entityClass] ??= $entityClass::menuMetadata();
    }
}
