<?php

namespace App\Modules\DefinedService\Route\Application\Index;

use App\Modules\Shared\Domain\Transaction;
use App\Modules\DefinedService\Route\Domain\IdRoute;
use App\Modules\DefinedService\Route\Domain\IndexRoute;
use App\Modules\DefinedService\Route\Domain\RouteRepository;
use App\Modules\Shared\Domain\QueryBuilder;

final class RouteIndexer
{
    public function __construct(
        private RouteRepository $repository,
        private Transaction $transaction
    ) {}

    public function __invoke(IdRoute $idRoute, IndexRoutePopulate $populate, bool $lockInShare = false)
    {
        $this->transaction->begin();
        $index = $this->repository->find(new IndexRoute($idRoute, $populate, $lockInShare));
        $this->transaction->commit();
        return $index;
    }
}
