// tslint:disable
{{>licenseInfo}}
import { Observable } from 'rxjs';
import { BaseAPI, RequiredError, HttpHeaders, HttpQuery, COLLECTION_FORMATS } from '../runtime';
{{#imports.0}}
import {
    {{#imports}}
    {{className}},
    {{/imports}}
} from '../models';
{{/imports.0}}

{{#operations}}
{{#operation}}
{{#allParams.0}}
export interface {{operationIdCamelCase}}Request {
    {{#allParams}}
    {{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}};
    {{/allParams}}
}

{{/allParams.0}}
{{/operation}}
{{/operations}}
{{#operations}}
/**
 * {{#description}}{{{description}}}{{/description}}{{^description}}no description{{/description}}
 */
export class {{classname}} extends BaseAPI {

    {{#operation}}
    /**
     {{#notes}}
     * {{&notes}}
     {{/notes}}
     {{#summary}}
     * {{&summary}}
     {{/summary}}
     */
    {{nickname}}({{#allParams.0}}requestParameters: {{operationIdCamelCase}}Request{{/allParams.0}}): Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {
        {{#allParams}}
        {{#required}}
        if (requestParameters.{{paramName}} === null || requestParameters.{{paramName}} === undefined) {
            throw new RequiredError('{{paramName}}','Required parameter requestParameters.{{paramName}} was null or undefined when calling {{nickname}}.');
        }

        {{/required}}
        {{/allParams}}
        const queryParameters: HttpQuery = {};

        {{#queryParams}}
        {{#isListContainer}}
        if (requestParameters.{{paramName}}) {
            {{#isCollectionFormatMulti}}
            queryParameters['{{baseName}}'] = requestParameters.{{paramName}};
            {{/isCollectionFormatMulti}}
            {{^isCollectionFormatMulti}}
            queryParameters['{{baseName}}'] = requestParameters.{{paramName}}.join(COLLECTION_FORMATS["{{collectionFormat}}"]);
            {{/isCollectionFormatMulti}}
        }

        {{/isListContainer}}
        {{^isListContainer}}
        if (requestParameters.{{paramName}} !== undefined && requestParameters.{{paramName}} !== null) {
            {{#isDateTime}}
            queryParameters['{{baseName}}'] = (requestParameters.{{paramName}} as any).toISOString();
            {{/isDateTime}}
            {{^isDateTime}}
            {{#isDate}}
            queryParameters['{{baseName}}'] = (requestParameters.{{paramName}} as any).toISOString();
            {{/isDate}}
            {{^isDate}}
            queryParameters['{{baseName}}'] = requestParameters.{{paramName}};
            {{/isDate}}
            {{/isDateTime}}
        }

        {{/isListContainer}}
        {{/queryParams}}
        const headerParameters: HttpHeaders = {};

        {{#bodyParam}}
        {{^consumes}}
        headerParameters['Content-Type'] = 'application/json';

        {{/consumes}}
        {{#consumes.0}}
        headerParameters['Content-Type'] = '{{{mediaType}}}';

        {{/consumes.0}}
        {{/bodyParam}}
        {{#headerParams}}
        {{#isListContainer}}
        if (requestParameters.{{paramName}}) {
            headerParameters['{{baseName}}'] = requestParameters.{{paramName}}.join(COLLECTION_FORMATS["{{collectionFormat}}"]));
        }

        {{/isListContainer}}
        {{^isListContainer}}
        if (requestParameters.{{paramName}} !== undefined && requestParameters.{{paramName}} !== null) {
            headerParameters['{{baseName}}'] = String(requestParameters.{{paramName}});
        }

        {{/isListContainer}}
        {{/headerParams}}
        {{#authMethods}}
        {{#isBasic}}
        if (this.configuration && (this.configuration.username !== undefined || this.configuration.password !== undefined)) {
            headerParameters["Authorization"] = "Basic " + btoa(this.configuration.username + ":" + this.configuration.password);
        }

        {{/isBasic}}
        {{#isApiKey}}
        {{#isKeyInHeader}}
        if (this.configuration && this.configuration.apiKey) {
            headerParameters["{{keyParamName}}"] = this.configuration.apiKey("{{keyParamName}}"); // {{name}} authentication
        }

        {{/isKeyInHeader}}
        {{#isKeyInQuery}}
        if (this.configuration && this.configuration.apiKey) {
            queryParameters["{{keyParamName}}"] = this.configuration.apiKey("{{keyParamName}}"); // {{name}} authentication
        }

        {{/isKeyInQuery}}
        {{/isApiKey}}
        {{#isOAuth}}
        if (this.configuration && this.configuration.accessToken) {
            // oauth required
            if (typeof this.configuration.accessToken === 'function') {
                headerParameters["Authorization"] = this.configuration.accessToken("{{name}}", [{{#scopes}}"{{{scope}}}"{{^-last}}, {{/-last}}{{/scopes}}]);
            } else {
                headerParameters["Authorization"] = this.configuration.accessToken;
            }
        }

        {{/isOAuth}}
        {{/authMethods}}
        {{#hasFormParams}}
        const formData = new FormData();
        {{/hasFormParams}}
        {{#formParams}}
        {{#isListContainer}}
        if (requestParameters.{{paramName}}) {
            {{#isCollectionFormatMulti}}
            requestParameters.{{paramName}}.forEach((element) => {
                formData.append('{{baseName}}', element as any);
            })
            {{/isCollectionFormatMulti}}
            {{^isCollectionFormatMulti}}
            formData.append('{{baseName}}', requestParameters.{{paramName}}.join(COLLECTION_FORMATS["{{collectionFormat}}"]));
            {{/isCollectionFormatMulti}}
        }

        {{/isListContainer}}
        {{^isListContainer}}
        if (requestParameters.{{paramName}} !== undefined) {
            formData.append('{{baseName}}', requestParameters.{{paramName}} as any);
        }

        {{/isListContainer}}
        {{/formParams}}
        return this.request<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}>({
            path: `{{{path}}}`{{#pathParams}}.replace(`{${"{{baseName}}"}}`, encodeURIComponent(String(requestParameters.{{paramName}}))){{/pathParams}},
            method: '{{httpMethod}}',
            headers: headerParameters,
            query: queryParameters,
            {{#hasBodyParam}}
            {{#bodyParam}}
            {{#isContainer}}
            body: requestParameters.{{paramName}},
            {{/isContainer}}
            {{^isContainer}}
            {{^isPrimitiveType}}
            body: requestParameters.{{paramName}},
            {{/isPrimitiveType}}
            {{#isPrimitiveType}}
            body: requestParameters.{{paramName}} as any,
            {{/isPrimitiveType}}
            {{/isContainer}}
            {{/bodyParam}}
            {{/hasBodyParam}}
            {{#hasFormParams}}
            body: formData,
            {{/hasFormParams}}
        });
    }

    {{/operation}}
}
{{/operations}}
{{#hasEnums}}

{{#operations}}
{{#operation}}
{{#allParams}}
{{#isEnum}}
/**
    * @export
    * @enum {string}
    */
export enum {{operationIdCamelCase}}{{enumName}} {
{{#allowableValues}}
    {{#enumVars}}
    {{{name}}} = {{{value}}}{{^-last}},{{/-last}}
    {{/enumVars}}
{{/allowableValues}}
}
{{/isEnum}}
{{/allParams}}
{{/operation}}
{{/operations}}
{{/hasEnums}}
