/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT License.
 *
 * Code generated by Microsoft (R) AutoRest Code Generator.
 * Changes may cause incorrect behavior and will be lost if the code is regenerated.
 */
import { __asyncDelegator, __asyncGenerator, __asyncValues, __await } from "tslib";
import { setContinuationToken } from "../pagingHelper.js";
import * as coreClient from "@azure/core-client";
import * as Mappers from "../models/mappers.js";
import * as Parameters from "../models/parameters.js";
import { createHttpPoller, } from "@azure/core-lro";
import { createLroSpec } from "../lroImpl.js";
/// <reference lib="esnext.asynciterable" />
/** Class containing ApiGatewayConfigConnection operations. */
export class ApiGatewayConfigConnectionImpl {
    /**
     * Initialize a new instance of the class ApiGatewayConfigConnection class.
     * @param client Reference to the service client
     */
    constructor(client) {
        this.client = client;
    }
    /**
     * List all API Management gateway config connections within a gateway.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param gatewayName The name of the API Management gateway.
     * @param options The options parameters.
     */
    listByGateway(resourceGroupName, gatewayName, options) {
        const iter = this.listByGatewayPagingAll(resourceGroupName, gatewayName, options);
        return {
            next() {
                return iter.next();
            },
            [Symbol.asyncIterator]() {
                return this;
            },
            byPage: (settings) => {
                if (settings === null || settings === void 0 ? void 0 : settings.maxPageSize) {
                    throw new Error("maxPageSize is not supported by this operation.");
                }
                return this.listByGatewayPagingPage(resourceGroupName, gatewayName, options, settings);
            },
        };
    }
    listByGatewayPagingPage(resourceGroupName, gatewayName, options, settings) {
        return __asyncGenerator(this, arguments, function* listByGatewayPagingPage_1() {
            let result;
            let continuationToken = settings === null || settings === void 0 ? void 0 : settings.continuationToken;
            if (!continuationToken) {
                result = yield __await(this._listByGateway(resourceGroupName, gatewayName, options));
                let page = result.value || [];
                continuationToken = result.nextLink;
                setContinuationToken(page, continuationToken);
                yield yield __await(page);
            }
            while (continuationToken) {
                result = yield __await(this._listByGatewayNext(resourceGroupName, gatewayName, continuationToken, options));
                continuationToken = result.nextLink;
                let page = result.value || [];
                setContinuationToken(page, continuationToken);
                yield yield __await(page);
            }
        });
    }
    listByGatewayPagingAll(resourceGroupName, gatewayName, options) {
        return __asyncGenerator(this, arguments, function* listByGatewayPagingAll_1() {
            var _a, e_1, _b, _c;
            try {
                for (var _d = true, _e = __asyncValues(this.listByGatewayPagingPage(resourceGroupName, gatewayName, options)), _f; _f = yield __await(_e.next()), _a = _f.done, !_a; _d = true) {
                    _c = _f.value;
                    _d = false;
                    const page = _c;
                    yield __await(yield* __asyncDelegator(__asyncValues(page)));
                }
            }
            catch (e_1_1) { e_1 = { error: e_1_1 }; }
            finally {
                try {
                    if (!_d && !_a && (_b = _e.return)) yield __await(_b.call(_e));
                }
                finally { if (e_1) throw e_1.error; }
            }
        });
    }
    /**
     * List all API Management gateway config connections within a gateway.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param gatewayName The name of the API Management gateway.
     * @param options The options parameters.
     */
    _listByGateway(resourceGroupName, gatewayName, options) {
        return this.client.sendOperationRequest({ resourceGroupName, gatewayName, options }, listByGatewayOperationSpec);
    }
    /**
     * Gets an API Management gateway config connection resource description.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param gatewayName The name of the API Management gateway.
     * @param configConnectionName The name of the API Management gateway config connection.
     * @param options The options parameters.
     */
    get(resourceGroupName, gatewayName, configConnectionName, options) {
        return this.client.sendOperationRequest({ resourceGroupName, gatewayName, configConnectionName, options }, getOperationSpec);
    }
    /**
     * Creates or updates an API Management gateway config connection. This is long running operation and
     * could take several minutes to complete.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param gatewayName The name of the API Management gateway.
     * @param configConnectionName The name of the API Management gateway config connection.
     * @param parameters Parameters supplied to the CreateOrUpdate API Management gateway config connection
     *                   operation.
     * @param options The options parameters.
     */
    async beginCreateOrUpdate(resourceGroupName, gatewayName, configConnectionName, parameters, options) {
        const directSendOperation = async (args, spec) => {
            return this.client.sendOperationRequest(args, spec);
        };
        const sendOperationFn = async (args, spec) => {
            var _a;
            let currentRawResponse = undefined;
            const providedCallback = (_a = args.options) === null || _a === void 0 ? void 0 : _a.onResponse;
            const callback = (rawResponse, flatResponse) => {
                currentRawResponse = rawResponse;
                providedCallback === null || providedCallback === void 0 ? void 0 : providedCallback(rawResponse, flatResponse);
            };
            const updatedArgs = Object.assign(Object.assign({}, args), { options: Object.assign(Object.assign({}, args.options), { onResponse: callback }) });
            const flatResponse = await directSendOperation(updatedArgs, spec);
            return {
                flatResponse,
                rawResponse: {
                    statusCode: currentRawResponse.status,
                    body: currentRawResponse.parsedBody,
                    headers: currentRawResponse.headers.toJSON(),
                },
            };
        };
        const lro = createLroSpec({
            sendOperationFn,
            args: {
                resourceGroupName,
                gatewayName,
                configConnectionName,
                parameters,
                options,
            },
            spec: createOrUpdateOperationSpec,
        });
        const poller = await createHttpPoller(lro, {
            restoreFrom: options === null || options === void 0 ? void 0 : options.resumeFrom,
            intervalInMs: options === null || options === void 0 ? void 0 : options.updateIntervalInMs,
            resourceLocationConfig: "azure-async-operation",
        });
        await poller.poll();
        return poller;
    }
    /**
     * Creates or updates an API Management gateway config connection. This is long running operation and
     * could take several minutes to complete.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param gatewayName The name of the API Management gateway.
     * @param configConnectionName The name of the API Management gateway config connection.
     * @param parameters Parameters supplied to the CreateOrUpdate API Management gateway config connection
     *                   operation.
     * @param options The options parameters.
     */
    async beginCreateOrUpdateAndWait(resourceGroupName, gatewayName, configConnectionName, parameters, options) {
        const poller = await this.beginCreateOrUpdate(resourceGroupName, gatewayName, configConnectionName, parameters, options);
        return poller.pollUntilDone();
    }
    /**
     * Deletes an existing API Management gateway config connection.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param gatewayName The name of the API Management gateway.
     * @param configConnectionName The name of the API Management gateway config connection.
     * @param ifMatch ETag of the Entity. ETag should match the current entity state from the header
     *                response of the GET request or it should be * for unconditional update.
     * @param options The options parameters.
     */
    async beginDelete(resourceGroupName, gatewayName, configConnectionName, ifMatch, options) {
        const directSendOperation = async (args, spec) => {
            return this.client.sendOperationRequest(args, spec);
        };
        const sendOperationFn = async (args, spec) => {
            var _a;
            let currentRawResponse = undefined;
            const providedCallback = (_a = args.options) === null || _a === void 0 ? void 0 : _a.onResponse;
            const callback = (rawResponse, flatResponse) => {
                currentRawResponse = rawResponse;
                providedCallback === null || providedCallback === void 0 ? void 0 : providedCallback(rawResponse, flatResponse);
            };
            const updatedArgs = Object.assign(Object.assign({}, args), { options: Object.assign(Object.assign({}, args.options), { onResponse: callback }) });
            const flatResponse = await directSendOperation(updatedArgs, spec);
            return {
                flatResponse,
                rawResponse: {
                    statusCode: currentRawResponse.status,
                    body: currentRawResponse.parsedBody,
                    headers: currentRawResponse.headers.toJSON(),
                },
            };
        };
        const lro = createLroSpec({
            sendOperationFn,
            args: {
                resourceGroupName,
                gatewayName,
                configConnectionName,
                ifMatch,
                options,
            },
            spec: deleteOperationSpec,
        });
        const poller = await createHttpPoller(lro, {
            restoreFrom: options === null || options === void 0 ? void 0 : options.resumeFrom,
            intervalInMs: options === null || options === void 0 ? void 0 : options.updateIntervalInMs,
        });
        await poller.poll();
        return poller;
    }
    /**
     * Deletes an existing API Management gateway config connection.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param gatewayName The name of the API Management gateway.
     * @param configConnectionName The name of the API Management gateway config connection.
     * @param ifMatch ETag of the Entity. ETag should match the current entity state from the header
     *                response of the GET request or it should be * for unconditional update.
     * @param options The options parameters.
     */
    async beginDeleteAndWait(resourceGroupName, gatewayName, configConnectionName, ifMatch, options) {
        const poller = await this.beginDelete(resourceGroupName, gatewayName, configConnectionName, ifMatch, options);
        return poller.pollUntilDone();
    }
    /**
     * ListByGatewayNext
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param gatewayName The name of the API Management gateway.
     * @param nextLink The nextLink from the previous successful call to the ListByGateway method.
     * @param options The options parameters.
     */
    _listByGatewayNext(resourceGroupName, gatewayName, nextLink, options) {
        return this.client.sendOperationRequest({ resourceGroupName, gatewayName, nextLink, options }, listByGatewayNextOperationSpec);
    }
}
// Operation Specifications
const serializer = coreClient.createSerializer(Mappers, /* isXml */ false);
const listByGatewayOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/gateways/{gatewayName}/configConnections",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.ApiManagementGatewayConfigConnectionListResult,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    queryParameters: [Parameters.apiVersion],
    urlParameters: [
        Parameters.$host,
        Parameters.resourceGroupName,
        Parameters.gatewayName,
        Parameters.subscriptionId,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const getOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/gateways/{gatewayName}/configConnections/{configConnectionName}",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.ApiManagementGatewayConfigConnectionResource,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    queryParameters: [Parameters.apiVersion],
    urlParameters: [
        Parameters.$host,
        Parameters.resourceGroupName,
        Parameters.gatewayName,
        Parameters.subscriptionId,
        Parameters.configConnectionName,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const createOrUpdateOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/gateways/{gatewayName}/configConnections/{configConnectionName}",
    httpMethod: "PUT",
    responses: {
        200: {
            bodyMapper: Mappers.ApiManagementGatewayConfigConnectionResource,
        },
        201: {
            bodyMapper: Mappers.ApiManagementGatewayConfigConnectionResource,
        },
        202: {
            bodyMapper: Mappers.ApiManagementGatewayConfigConnectionResource,
        },
        204: {
            bodyMapper: Mappers.ApiManagementGatewayConfigConnectionResource,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    requestBody: Parameters.parameters46,
    queryParameters: [Parameters.apiVersion],
    urlParameters: [
        Parameters.$host,
        Parameters.resourceGroupName,
        Parameters.gatewayName,
        Parameters.subscriptionId,
        Parameters.configConnectionName,
    ],
    headerParameters: [Parameters.contentType, Parameters.accept],
    mediaType: "json",
    serializer,
};
const deleteOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/gateways/{gatewayName}/configConnections/{configConnectionName}",
    httpMethod: "DELETE",
    responses: {
        200: {
            headersMapper: Mappers.ApiGatewayConfigConnectionDeleteHeaders,
        },
        201: {
            headersMapper: Mappers.ApiGatewayConfigConnectionDeleteHeaders,
        },
        202: {
            headersMapper: Mappers.ApiGatewayConfigConnectionDeleteHeaders,
        },
        204: {
            headersMapper: Mappers.ApiGatewayConfigConnectionDeleteHeaders,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    queryParameters: [Parameters.apiVersion],
    urlParameters: [
        Parameters.$host,
        Parameters.resourceGroupName,
        Parameters.gatewayName,
        Parameters.subscriptionId,
        Parameters.configConnectionName,
    ],
    headerParameters: [Parameters.accept, Parameters.ifMatch1],
    serializer,
};
const listByGatewayNextOperationSpec = {
    path: "{nextLink}",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.ApiManagementGatewayConfigConnectionListResult,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    urlParameters: [
        Parameters.$host,
        Parameters.resourceGroupName,
        Parameters.gatewayName,
        Parameters.subscriptionId,
        Parameters.nextLink,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
//# sourceMappingURL=apiGatewayConfigConnection.js.map