<?php

namespace {{invokerPackage}}\Api\{{classname}};

use {{invokerPackage}}\AccessToken;
use {{invokerPackage}}\Configuration;
use {{invokerPackage}}\Exception\ApiException;
use {{invokerPackage}}\Exception\InvalidArgumentException;
use {{invokerPackage}}\HttpFactory;
use {{invokerPackage}}\HttpSignatureHeaders;
use {{invokerPackage}}\ObjectSerializer;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Log\LoggerInterface;

/**
 * This class was auto-generated by https://github.com/OpenAPITools/openapi-generator/.
 * Do not change it, it will be overwritten with next execution of /bin/generate.sh
 */
final {{#operations}}class {{packageName}}SDK
{
    public const API_NAME = '{{packageName}}';

    {{#operation}}
    public const OPERATION_{{#lambda.uppercase}}{{operationId}}{{/lambda.uppercase}} = '{{operationId}}';

    public const OPERATION_{{#lambda.uppercase}}{{operationId}}{{/lambda.uppercase}}_PATH = '{{path}}';
    {{/operation}}

    private ClientInterface $client;

    private HttpFactory $httpFactory;

    private Configuration $configuration;

    private LoggerInterface $logger;

    public function __construct(ClientInterface $client, HttpFactory $requestFactory, Configuration $configuration, LoggerInterface $logger)
    {
        $this->client = $client;
        $this->httpFactory = $requestFactory;
        $this->configuration = $configuration;
        $this->logger = $logger;
    }

{{#operation}}
    /**
     * Operation {{{operationId}}}
{{#summary}}
     *
     * {{.}}
{{/summary}}
     *
{{#description}}
     * {{.}}
     *
{{/description}}
{{#vendorExtensions.x-group-parameters}}
     * Note: the input parameter is an associative array with the keys listed as the parameter name below
     *
{{/vendorExtensions.x-group-parameters}}
{{#servers}}
{{#-first}}
     * This operation contains host(s) defined in the OpenAP spec. Use 'hostIndex' to select the host.
{{/-first}}
     * URL: {{{url}}}
{{#-last}}
{{/-last}}
{{/servers}}
     * @param AccessToken $accessToken
     * @param string $region
{{#allParams}}
     * @param {{{dataType}}} ${{paramName}}{{#description}} {{description}}{{/description}}{{^description}} {{paramName}}{{/description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
     *
     * @throws \{{invokerPackage}}\Exception\ApiException on non-2xx response
     * @throws \{{invokerPackage}}\Exception\InvalidArgumentException
     * @return {{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}null{{/returnType}}
     */
    public function {{operationId}}(AccessToken $accessToken, string $region{{^vendorExtensions.x-group-parameters}}{{#allParams}}, ${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}{{/-last}}{{/allParams}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}, $associative_array{{/vendorExtensions.x-group-parameters}})
    {
        $request = $this->{{operationId}}Request($accessToken, $region{{^vendorExtensions.x-group-parameters}}{{#allParams}}, ${{paramName}}{{^-last}}{{/-last}}{{/allParams}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}, $associative_array{{/vendorExtensions.x-group-parameters}});

        $this->configuration->extensions()->preRequest('{{packageName}}', '{{operationId}}', $request);

        try {
            $correlationId = \uuid_create(UUID_TYPE_RANDOM);

            if ($this->configuration->loggingEnabled('{{packageName}}', '{{operationId}}')) {

                $sanitizedRequest = $request;

                foreach ($this->configuration->loggingSkipHeaders() as $sensitiveHeader) {
                    $sanitizedRequest = $sanitizedRequest->withoutHeader($sensitiveHeader);
                }

                $this->logger->log(
                    $this->configuration->logLevel('{{packageName}}', '{{operationId}}'),
                    'Amazon Selling Partner API pre request',
                    [
                        'api' => '{{packageName}}',
                        'operation' => '{{operationId}}',
                        'request_correlation_id' => $correlationId,
                        'request_body' => (string) $sanitizedRequest->getBody(),
                        'request_headers' => $sanitizedRequest->getHeaders(),
                        'request_uri' => (string) $sanitizedRequest->getUri(),
                    ]
                );
            }

            $response = $this->client->sendRequest($request);

            $this->configuration->extensions()->postRequest('{{packageName}}', '{{operationId}}', $request, $response);

            if ($this->configuration->loggingEnabled('{{packageName}}', '{{operationId}}')) {

                $sanitizedResponse = $response;

                foreach ($this->configuration->loggingSkipHeaders() as $sensitiveHeader) {
                    $sanitizedResponse = $sanitizedResponse->withoutHeader($sensitiveHeader);
                }

                $this->logger->log(
                    $this->configuration->logLevel('{{packageName}}', '{{operationId}}'),
                    'Amazon Selling Partner API post request',
                    [
                        'api' => '{{packageName}}',
                        'operation' => '{{operationId}}',
                        'response_correlation_id' => $correlationId,
                        'response_body' => (string) $sanitizedResponse->getBody(),
                        'response_headers' => $sanitizedResponse->getHeaders(),
                        'response_status_code' => $sanitizedResponse->getStatusCode(),
                    ]
                );
            }
        } catch (ClientExceptionInterface $e) {
            throw new ApiException(
                "[{$e->getCode()}] {$e->getMessage()}",
                (int) $e->getCode(),
                null,
                null,
                $e
            );
        }

        $statusCode = $response->getStatusCode();

        if ($statusCode < 200 || $statusCode > 299) {
            throw new ApiException(
                sprintf(
                    '[%d] Error connecting to the API (%s)',
                    $statusCode,
                    (string) $request->getUri()
                ),
                $statusCode,
                $response->getHeaders(),
                (string) $response->getBody()
            );
        }
        {{#returnType}}

        return ObjectSerializer::deserialize(
            $this->configuration,
            (string) $response->getBody(),
            '{{returnType}}',
            []
        );
        {{/returnType}}
        {{^returnType}}

        return null;
        {{/returnType}}
    }

    /**
     * Create request for operation '{{{operationId}}}'
     *
{{#vendorExtensions.x-group-parameters}}
     * Note: the input parameter is an associative array with the keys listed as the parameter name below
     *
{{/vendorExtensions.x-group-parameters}}
{{#servers}}
{{#-first}}
     * This oepration contains host(s) defined in the OpenAP spec. Use 'hostIndex' to select the host.
{{/-first}}
     * URL: {{{url}}}
{{#-last}}
     *
{{/-last}}
{{/servers}}
     * @param AccessToken $accessToken
     * @param string $region
{{#allParams}}
     * @param {{{dataType}}} ${{paramName}}{{#description}} {{description}}{{/description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
     *
     * @throws \{{invokerPackage}}\Exception\InvalidArgumentException
     * @return RequestInterface
     */
    public function {{operationId}}Request(AccessToken $accessToken, string $region{{^vendorExtensions.x-group-parameters}}{{#allParams}}, ${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}{{/-last}}{{/allParams}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}, $associative_array{{/vendorExtensions.x-group-parameters}}) : RequestInterface
    {
        {{#vendorExtensions.x-group-parameters}}
        // unbox the parameters from the associative array
        {{#allParams}}
        ${{paramName}} = array_key_exists('{{paramName}}', $associative_array) ? $associative_array['{{paramName}}'] : {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}};
        {{/allParams}}

        {{/vendorExtensions.x-group-parameters}}
        {{#allParams}}
        {{#required}}
        // verify the required parameter '{{paramName}}' is set
        if (${{paramName}} === null || (is_array(${{paramName}}) && count(${{paramName}}) === 0)) {
            throw new InvalidArgumentException(
                'Missing the required parameter ${{paramName}} when calling {{operationId}}'
            );
        }
        {{/required}}
        {{#hasValidation}}
        {{#maxLength}}
        if ({{^required}}${{paramName}} !== null && {{/required}}strlen(${{paramName}}) > {{maxLength}}) {
            throw new InvalidArgumentException('invalid length for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{maxLength}}.');
        }
        {{/maxLength}}
        {{#minLength}}
        if ({{^required}}${{paramName}} !== null && {{/required}}strlen(${{paramName}}) < {{minLength}}) {
            throw new InvalidArgumentException('invalid length for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be bigger than or equal to {{minLength}}.');
        }
        {{/minLength}}
        {{#maximum}}
        if ({{^required}}${{paramName}} !== null && {{/required}}${{paramName}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}) {
            throw new InvalidArgumentException('invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.');
        }
        {{/maximum}}
        {{#minimum}}
        if ({{^required}}${{paramName}} !== null && {{/required}}${{paramName}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}) {
            throw new InvalidArgumentException('invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}.');
        }
        {{/minimum}}
        {{#pattern}}
        if ({{^required}}${{paramName}} !== null && {{/required}}!preg_match("{{{pattern}}}", ${{paramName}})) {
            throw new InvalidArgumentException("invalid value for \"{{paramName}}\" when calling {{classname}}.{{operationId}}, must conform to the pattern {{{pattern}}}.");
        }
        {{/pattern}}
        {{#maxItems}}
        if ({{^required}}${{paramName}} !== null && {{/required}}count(${{paramName}}) > {{maxItems}}) {
            throw new InvalidArgumentException('invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, number of items must be less than or equal to {{maxItems}}.');
        }
        {{/maxItems}}
        {{#minItems}}
        if ({{^required}}${{paramName}} !== null && {{/required}}count(${{paramName}}) < {{minItems}}) {
            throw new InvalidArgumentException('invalid value for "${{paramName}}" when calling {{classname}}.{{operationId}}, number of items must be greater than or equal to {{minItems}}.');
        }
        {{/minItems}}

        {{/hasValidation}}
        {{/allParams}}

        $resourcePath = '{{{path}}}';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $multipart = false;
        $query = '';

        {{#queryParams}}
        // query params
        {{#isExplode}}
        if (${{paramName}} !== null) {
        {{#style}}
            if('form' === '{{style}}' && is_array(${{paramName}})) {
                foreach(${{paramName}} as $key => $value) {
                    $queryParams[$key] = $value;
                }
            }
            else {
                $queryParams['{{baseName}}'] = ${{paramName}};
            }
        {{/style}}
        {{^style}}
            $queryParams['{{baseName}}'] = ${{paramName}};
        {{/style}}
        }
        {{/isExplode}}
        {{^isExplode}}
        if (is_array(${{paramName}})) {
            ${{paramName}} = ObjectSerializer::serializeCollection(${{paramName}}, '{{#style}}{{style}}{{/style}}{{^style}}{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}{{/style}}', true);
        }
        if (${{paramName}} !== null) {
            $queryParams['{{baseName}}'] = ObjectSerializer::toString(${{paramName}});
        }
        {{/isExplode}}
        {{/queryParams}}

        if (\count($queryParams)) {
            $query = http_build_query($queryParams);
        }

        {{#headerParams}}
        // header params
        {{#collectionFormat}}
        if (is_array(${{paramName}})) {
            ${{paramName}} = ObjectSerializer::serializeCollection(${{paramName}}, '{{collectionFormat}}');
        }
        {{/collectionFormat}}
        if (${{paramName}} !== null) {
            $headerParams['{{baseName}}'] = ObjectSerializer::toHeaderValue(${{paramName}});
        }
        {{/headerParams}}

        {{#pathParams}}
        // path params
        {{#collectionFormat}}
        if (is_array(${{paramName}})) {
            ${{paramName}} = ObjectSerializer::serializeCollection(${{paramName}}, '{{collectionFormat}}');
        }
        {{/collectionFormat}}
        if (${{paramName}} !== null) {
            $resourcePath = str_replace(
                '{' . '{{baseName}}' . '}',
                ObjectSerializer::toPathValue(${{paramName}}),
                $resourcePath
            );
        }
        {{/pathParams}}

        {{#formParams}}
        // form params
        if (${{paramName}} !== null) {
            {{#isFile}}
            $multipart = true;
            $formParams['{{baseName}}'] = [];
            $paramFiles = is_array(${{paramName}}) ? ${{paramName}} : [${{paramName}}];
            foreach ($paramFiles as $paramFile) {
                $formParams['{{baseName}}'][] = \GuzzleHttp\Psr7\try_fopen(
                    ObjectSerializer::toFormValue($paramFile),
                    'rb'
                );
            }
            {{/isFile}}
            {{^isFile}}
            $formParams['{{baseName}}'] = ObjectSerializer::toFormValue(${{paramName}});
            {{/isFile}}
        }
        {{/formParams}}

        if ($multipart) {
            $headers = [
                'accept' => ['application/json'],
                'host' => [$this->configuration->apiHost($region)],
                'user-agent' => [$this->configuration->userAgent()],
            ];
        } else {
            $headers = [
                'content-type' => ['application/json'],
                'accept' => ['application/json'],
                'host' => [$this->configuration->apiHost($region)],
                'user-agent' => [$this->configuration->userAgent()],
            ];
        }

        $request = $this->httpFactory->createRequest(
            '{{httpMethod}}',
            $this->configuration->apiURL($region) . $resourcePath . '?' . $query
        );

        // for model (json/xml)
        {{#bodyParams}}
        if (isset(${{paramName}})) {
            if ($headers['content-type'] === ['application/json']) {
                $httpBody = \json_encode(ObjectSerializer::sanitizeForSerialization(${{paramName}}));
            } else {
                $httpBody = ${{paramName}};
            }

            $request = $request->withBody($this->httpFactory->createStreamFromString($httpBody));
        } elseif (count($formParams) > 0) {
        {{/bodyParams}}
        {{^bodyParams}}
        if (count($formParams) > 0) {
        {{/bodyParams}}
            if ($multipart) {
                $multipartContents = [];
                foreach ($formParams as $formParamName => $formParamValue) {
                    $formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue];
                    foreach ($formParamValueItems as $formParamValueItem) {
                        $multipartContents[] = [
                            'name' => $formParamName,
                            'contents' => $formParamValueItem
                        ];
                    }
                }
                $request = $request->withParsedBody($multipartContents);
            } elseif ($headers['content-type'] === ['application/json']) {
                $request = $request->withBody($this->httpFactory->createStreamFromString(\json_encode($formParams)));
            } else {
                $request = $request->withParsedBody($formParams);
            }
        }

        foreach (\array_merge($headerParams, $headers) as $name => $header) {
            $request = $request->withHeader($name, $header);
        }

        return HttpSignatureHeaders::forConfig(
            $this->configuration,
            $accessToken,
            $region,
            $request
        );
    }

    {{/operation}}
}
{{/operations}}