<?php

namespace App\Modules\TypeProvider\Application\Find;

use App\Modules\TypeProvider\Domain\ExistsTypeProvider;
use App\Modules\TypeProvider\Domain\IdTypeProvider;
use App\Modules\TypeProvider\Domain\TypeProviderRepository;
use App\Modules\Shared\Domain\Transaction;

final class TypeProviderFinder
{
    public function __construct(
        private TypeProviderRepository $repository,
        private Transaction $transaction
    ) {}

    public function __invoke(
        IdTypeProvider $idTypeProvider,
        bool $avoidTransaction = false,
        bool $lockInShare = false
    ) {
        $index = $this->transaction->transactional(
            fn() => $this->repository->find(new ExistsTypeProvider($idTypeProvider, $lockInShare)),
            $avoidTransaction
        );
        return $index;
    }
}
