<?php

declare(strict_types=1);

namespace apexl\Io\factories;

use apexl\Io\interfaces\HasEntityFactory;
use app\module\newtonsTrailersDisplay\Service\EntitySpecificClassNameService;
use DI\Container;
use DI\DependencyException;
use DI\NotFoundException;
use function apexl\Io\assertInstanceOf;

final readonly class EntityFactoryFactory
{
    public function __construct(
        private Container $container,
        private EntitySpecificClassNameService $specificClassNameService,
    ) {}

    /**
     * @param class-string<HasEntityFactory> $entityClass
     * @throws DependencyException
     * @throws NotFoundException
     */
    public function make(string $entityClass): EntityFactory
    {
        assertInstanceOf($entityClass, HasEntityFactory::class);

        $factoryClass = method_exists($entityClass, 'entityFactoryClass') ?
            $entityClass::entityFactoryClass() :
            $this->specificClassNameService->class($entityClass::entityFactoryBaseClass(), $entityClass);

        return $this->container->make($factoryClass, [
            'entityClass' => $entityClass,
        ]);
    }
}