<?php

declare(strict_types=1);

namespace App\Framework\HttpClient;

use App\Framework\Async\AsyncService;
use App\Framework\DI\Initializer;

final readonly class HttpClientInitializer
{
    public function __construct(
        private ?AsyncService $asyncService = null,
        private bool $enableAsync = true
    ) {
    }

    #[Initializer]
    public function __invoke(): HttpClient
    {
        $httpClient = new DefaultHttpClient();

        // Automatically wrap with AsyncAwareHttpClient if AsyncService is available
        if ($this->enableAsync && $this->asyncService !== null) {
            return new AsyncAwareHttpClient($httpClient, $this->asyncService);
        }

        return $httpClient;
    }
}
