<?php

namespace apexl\Io\modules\BasicApi\Services;

class BasicModuleEntityStore
{
    /** @var array<string, array<class-string, class-string>> */
    protected array $entities = [];

    /**
     * @param class-string|class-string[] $entities
     */
    public function add(string $module, string|array $entities): void
    {
        if (is_array($entities)) {
            foreach ($entities as $entity) {
                $this->add($module, $entity);
            }

            return;
        }

        $this->entities[$module][$entities] ??= $entities;
    }

    /**
     * @param class-string|null $type
     * @return class-string[]
     */
    public function all(string $module, ?string $type = null): array
    {
        $entities = $this->entities[$module] ?? [];

        if ($type) {
            $entities = array_filter(
                $entities,
                fn(string $entity): bool => is_a($entity, $type, true)
            );
        }

        return $entities;
    }

}
