<?php

namespace Alsendo\AlsendoWrapper;

use Alsendo\AlsendoWrapper\Exception\ValidationException;
use InvalidArgumentException;
use RuntimeException;

/**
 * A wrapper class that provides a unified interface to an API client instance.
 * It manages the creation and access of an API client based on configuration.
 *
 * @throws ValidationException If the client creation fails due to invalid configuration or other validation issues.
 */
class ApiWrapper
{
    private ApiClientInterface $apiClient;


    /**
     * Initializes the object with a client name and configuration.
     *
     * @param string $name The name of the API client to create.
     * @param array $config Configuration array for the API client.
     * @param null|string $platformName Name of the platform where the wrapper is used
     * @return void
     * @throws ValidationException
     * @throws RuntimeException If the client creation fails due to configuration issues.
     * @throws InvalidArgumentException If the name is not provided or invalid.
     */
    public function __construct(string $name, array $config, ?string $platformName = null)
    {
        $this->apiClient = (new ApiClientFactory())->create($name, $config);

        if ($platformName !== null) {
            $this->apiClient->setPlatformName($platformName);
        }
    }

    /**
     * Retrieves the configured API client instance.
     *
     * @return ApiClientInterface The API client instance.
     * @throws RuntimeException If the API client is not initialized.
     */
    public function getApiClient(): ApiClientInterface
    {
        return $this->apiClient;
    }


}
