<?php
declare(strict_types=1);

namespace App\OpenApi;

use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
use ApiPlatform\OpenApi\OpenApi;
use ApiPlatform\OpenApi\Model;

/**
 * Permet de retirer des URLs dans la doc Open API
 */
final class OpenApiFactory implements OpenApiFactoryInterface
{
    const PATH_TO_EXCLUDE = [
        '/api/dolibarr_bills/{id}',
        '/api/dolibarr_contract_lines/{id}',
        '/api/dolibarr_contracts/{ref}',
    ];

    private mixed $decorated;

    public function __construct(OpenApiFactoryInterface $decorated)
    {
        $this->decorated = $decorated;
    }

    /**
     * @param mixed[] $context
     * @return OpenApi
     */
    public function __invoke(array $context = []): OpenApi
    {
        $openApi = $this->decorated->__invoke($context);

        $paths = $openApi->getPaths()->getPaths();

        $filteredPaths = new Model\Paths();
        foreach ($paths as $path => $pathItem) {
            if (in_array($path, self::PATH_TO_EXCLUDE)) {
                continue;
            }
            $filteredPaths->addPath($path, $pathItem);
        }

        return $openApi->withPaths($filteredPaths);
    }
}