<?php
declare(strict_types=1);

namespace App\State\Provider\Dolibarr;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\ApiResources\Dolibarr\DolibarrAccount;
use App\Service\ApiResourceBuilder\Dolibarr\DolibarrAccountBuilder;
use RuntimeException;

/**
 * Custom provider pour les DolibarrAccounts récupérés via l'api dolibarr
 * @package App\DataProvider\Utils
 */
final class DolibarrAccountProvider implements ProviderInterface
{

    public function __construct(
        private DolibarrAccountBuilder $dolibarrAccountCreator,
    )
    {}

    /**
     * @param Operation $operation
     * @param mixed[] $uriVariables
     * @param mixed[] $context
     * @return DolibarrAccount|null
     */
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?DolibarrAccount
    {
        if($operation instanceof GetCollection) {
            throw new RuntimeException('not supported', 500);
        }

        return $this->dolibarrAccountCreator->getDolibarrAccount($uriVariables['id']);
    }
}
