<?php

declare(strict_types=1);

namespace apexl\Io\modules\tabulator\ComponentFactory;

use apexl\Io\modules\component\Factories\GenericComponentFactory;
use apexl\Io\modules\tabulator\Components\EntityTabulator;
use apexl\Io\modules\tabulator\Interfaces\HasTabulatorViewInterface;
use apexl\Io\services\EntitySpecificClassNameService;
use DI\DependencyException;
use DI\NotFoundException;
use function apexl\Io\assertInstanceOf;

final readonly class EntityTabulatorFactory
{
    public function __construct(
        private GenericComponentFactory $componentFactory,
        private EntitySpecificClassNameService $entitySpecificClassNameService
    ) {}

    /**
     * @param class-string<HasTabulatorViewInterface> $entityClass
     * @throws DependencyException
     * @throws NotFoundException
     */
    public function make(string $entityClass, ?string $subEntityKey = null): EntityTabulator
    {
        assertInstanceOf($entityClass, HasTabulatorViewInterface::class);
        /** @var class-string<EntityTabulator> $class */
        $class = $this->entitySpecificClassNameService->class(EntityTabulator::class, $entityClass);

        /** @var EntityTabulator $component */
        $component = $this->componentFactory->make(
            class: $class,
            initParams: [
                
            ],
            constructorParams: [
                'entityClass' => $entityClass,
                'subEntityKey' => $subEntityKey,
            ]
        );

        return $component;
    }
}