<?php

namespace App\Modules\CategoryProvider\Application\Index;

use App\Modules\CategoryProvider\Domain\ExistsCategoryProvider;
use App\Modules\CategoryProvider\Domain\IdCategoryProvider;
use App\Modules\CategoryProvider\Domain\CategoryProviderRepository;
use App\Modules\Shared\Domain\Transaction;

final class CategoryProviderIndexer
{
    public function __construct(
        private CategoryProviderRepository $repository,
        private Transaction $transaction
    ) {}

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