<?php


namespace AutoAdmin\Bundle\Admin\Form\Type\Component;


use AutoAdmin\Bundle\Content\Repository\AbstractContentRepository;
use Doctrine\ORM\EntityRepository;
use ReflectionClass;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class RichEntityType extends AbstractHelperType
{
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        $this->addViewClass($view, 'aa-ft-rich-entity');
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        parent::configureOptions($resolver);
        $resolver->setDefault('placeholder', 'select.none');

        $resolver->setDefault(
            'query_builder',
            function (EntityRepository $repository) {
                if ((new ReflectionClass($repository))->isSubclassOf(AbstractContentRepository::class)) {
                    /** @var AbstractContentRepository $repository */
                    return $repository->createContentQueryBuilder('_');
                }

                return $repository->createQueryBuilder('_');
            }
        );
    }

    public function getParent(): string
    {
        return EntityType::class;
    }
}