<?php


namespace AutoAdmin\Bundle\Menu\Repository;

use AutoAdmin\Bundle\Content\Repository\AbstractTranslatableContentRepository;
use AutoAdmin\Bundle\Menu\Entity\MenuItem;
use Doctrine\ORM\QueryBuilder;

/**
 * @method MenuItem|null find($id, $lockMode = null, $lockVersion = null)
 * @method MenuItem|null findOneBy(array $criteria, array $orderBy = null)
 * @method MenuItem[]    findAll()
 * @method MenuItem[]    findBy(array $criteria, array $orderBy = null, ?int $limit = null, ?int $offset = null)
 */
class MenuItemRepository extends AbstractTranslatableContentRepository
{
    public function contentFindOneBySlug(string $slug)
    {
        $qb = $this->createConfiguredQueryBuilder();
//        $qb->addSelect('_tr');
        $query = $qb->getQuery();
//        $query
//            ->setFetchMode(MenuItem::class, 'translations', ClassMetadataInfo::FETCH_EAGER)
//            ->setFetchMode(MenuItem::class, 'children', ClassMetadataInfo::FETCH_EAGER)

//        $query
//            ->setCacheable(true)
//            ->enableResultCache();

        return (($res = $query->execute()) && !empty($res[0])) ? $res[0] : null;
    }

    protected function createConfiguredQueryBuilder(): QueryBuilder
    {
        $qb = $this->createContentQueryBuilder();
        //        $qb->andWhere('_.slug = :slug')->setParameter('slug', $slug);
        $qb->addSelect('_');
        $qb->addSelect('_tr');

        return $qb;
    }
}
