"use strict";
/*
 * 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.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiGatewayImpl = void 0;
const tslib_1 = require("tslib");
const pagingHelper_js_1 = require("../pagingHelper.js");
const coreClient = tslib_1.__importStar(require("@azure/core-client"));
const Mappers = tslib_1.__importStar(require("../models/mappers.js"));
const Parameters = tslib_1.__importStar(require("../models/parameters.js"));
const core_lro_1 = require("@azure/core-lro");
const lroImpl_js_1 = require("../lroImpl.js");
/// <reference lib="esnext.asynciterable" />
/** Class containing ApiGateway operations. */
class ApiGatewayImpl {
    /**
     * Initialize a new instance of the class ApiGateway class.
     * @param client Reference to the service client
     */
    constructor(client) {
        this.client = client;
    }
    /**
     * List all API Management gateways within a resource group.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param options The options parameters.
     */
    listByResourceGroup(resourceGroupName, options) {
        const iter = this.listByResourceGroupPagingAll(resourceGroupName, 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.listByResourceGroupPagingPage(resourceGroupName, options, settings);
            },
        };
    }
    listByResourceGroupPagingPage(resourceGroupName, options, settings) {
        return tslib_1.__asyncGenerator(this, arguments, function* listByResourceGroupPagingPage_1() {
            let result;
            let continuationToken = settings === null || settings === void 0 ? void 0 : settings.continuationToken;
            if (!continuationToken) {
                result = yield tslib_1.__await(this._listByResourceGroup(resourceGroupName, options));
                let page = result.value || [];
                continuationToken = result.nextLink;
                (0, pagingHelper_js_1.setContinuationToken)(page, continuationToken);
                yield yield tslib_1.__await(page);
            }
            while (continuationToken) {
                result = yield tslib_1.__await(this._listByResourceGroupNext(resourceGroupName, continuationToken, options));
                continuationToken = result.nextLink;
                let page = result.value || [];
                (0, pagingHelper_js_1.setContinuationToken)(page, continuationToken);
                yield yield tslib_1.__await(page);
            }
        });
    }
    listByResourceGroupPagingAll(resourceGroupName, options) {
        return tslib_1.__asyncGenerator(this, arguments, function* listByResourceGroupPagingAll_1() {
            var _a, e_1, _b, _c;
            try {
                for (var _d = true, _e = tslib_1.__asyncValues(this.listByResourceGroupPagingPage(resourceGroupName, options)), _f; _f = yield tslib_1.__await(_e.next()), _a = _f.done, !_a; _d = true) {
                    _c = _f.value;
                    _d = false;
                    const page = _c;
                    yield tslib_1.__await(yield* tslib_1.__asyncDelegator(tslib_1.__asyncValues(page)));
                }
            }
            catch (e_1_1) { e_1 = { error: e_1_1 }; }
            finally {
                try {
                    if (!_d && !_a && (_b = _e.return)) yield tslib_1.__await(_b.call(_e));
                }
                finally { if (e_1) throw e_1.error; }
            }
        });
    }
    /**
     * List all API Management gateways within a subscription.
     * @param options The options parameters.
     */
    list(options) {
        const iter = this.listPagingAll(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.listPagingPage(options, settings);
            },
        };
    }
    listPagingPage(options, settings) {
        return tslib_1.__asyncGenerator(this, arguments, function* listPagingPage_1() {
            let result;
            let continuationToken = settings === null || settings === void 0 ? void 0 : settings.continuationToken;
            if (!continuationToken) {
                result = yield tslib_1.__await(this._list(options));
                let page = result.value || [];
                continuationToken = result.nextLink;
                (0, pagingHelper_js_1.setContinuationToken)(page, continuationToken);
                yield yield tslib_1.__await(page);
            }
            while (continuationToken) {
                result = yield tslib_1.__await(this._listNext(continuationToken, options));
                continuationToken = result.nextLink;
                let page = result.value || [];
                (0, pagingHelper_js_1.setContinuationToken)(page, continuationToken);
                yield yield tslib_1.__await(page);
            }
        });
    }
    listPagingAll(options) {
        return tslib_1.__asyncGenerator(this, arguments, function* listPagingAll_1() {
            var _a, e_2, _b, _c;
            try {
                for (var _d = true, _e = tslib_1.__asyncValues(this.listPagingPage(options)), _f; _f = yield tslib_1.__await(_e.next()), _a = _f.done, !_a; _d = true) {
                    _c = _f.value;
                    _d = false;
                    const page = _c;
                    yield tslib_1.__await(yield* tslib_1.__asyncDelegator(tslib_1.__asyncValues(page)));
                }
            }
            catch (e_2_1) { e_2 = { error: e_2_1 }; }
            finally {
                try {
                    if (!_d && !_a && (_b = _e.return)) yield tslib_1.__await(_b.call(_e));
                }
                finally { if (e_2) throw e_2.error; }
            }
        });
    }
    /**
     * Creates or updates an API Management gateway. 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 parameters Parameters supplied to the CreateOrUpdate API Management gateway operation.
     * @param options The options parameters.
     */
    async beginCreateOrUpdate(resourceGroupName, gatewayName, 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 = (0, lroImpl_js_1.createLroSpec)({
            sendOperationFn,
            args: { resourceGroupName, gatewayName, parameters, options },
            spec: createOrUpdateOperationSpec,
        });
        const poller = await (0, core_lro_1.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;
    }
    /**
     * Creates or updates an API Management gateway. 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 parameters Parameters supplied to the CreateOrUpdate API Management gateway operation.
     * @param options The options parameters.
     */
    async beginCreateOrUpdateAndWait(resourceGroupName, gatewayName, parameters, options) {
        const poller = await this.beginCreateOrUpdate(resourceGroupName, gatewayName, parameters, options);
        return poller.pollUntilDone();
    }
    /**
     * Updates an existing API Management gateway.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param gatewayName The name of the API Management gateway.
     * @param parameters Parameters supplied to the CreateOrUpdate API Management gateway operation.
     * @param options The options parameters.
     */
    async beginUpdate(resourceGroupName, gatewayName, 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 = (0, lroImpl_js_1.createLroSpec)({
            sendOperationFn,
            args: { resourceGroupName, gatewayName, parameters, options },
            spec: updateOperationSpec,
        });
        const poller = await (0, core_lro_1.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;
    }
    /**
     * Updates an existing API Management gateway.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param gatewayName The name of the API Management gateway.
     * @param parameters Parameters supplied to the CreateOrUpdate API Management gateway operation.
     * @param options The options parameters.
     */
    async beginUpdateAndWait(resourceGroupName, gatewayName, parameters, options) {
        const poller = await this.beginUpdate(resourceGroupName, gatewayName, parameters, options);
        return poller.pollUntilDone();
    }
    /**
     * Gets an API Management gateway 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 options The options parameters.
     */
    get(resourceGroupName, gatewayName, options) {
        return this.client.sendOperationRequest({ resourceGroupName, gatewayName, options }, getOperationSpec);
    }
    /**
     * Deletes an existing API Management 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.
     */
    async beginDelete(resourceGroupName, gatewayName, 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 = (0, lroImpl_js_1.createLroSpec)({
            sendOperationFn,
            args: { resourceGroupName, gatewayName, options },
            spec: deleteOperationSpec,
        });
        const poller = await (0, core_lro_1.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.
     * @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.
     */
    async beginDeleteAndWait(resourceGroupName, gatewayName, options) {
        const poller = await this.beginDelete(resourceGroupName, gatewayName, options);
        return poller.pollUntilDone();
    }
    /**
     * List all API Management gateways within a resource group.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param options The options parameters.
     */
    _listByResourceGroup(resourceGroupName, options) {
        return this.client.sendOperationRequest({ resourceGroupName, options }, listByResourceGroupOperationSpec);
    }
    /**
     * List all API Management gateways within a subscription.
     * @param options The options parameters.
     */
    _list(options) {
        return this.client.sendOperationRequest({ options }, listOperationSpec);
    }
    /**
     * ListByResourceGroupNext
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param nextLink The nextLink from the previous successful call to the ListByResourceGroup method.
     * @param options The options parameters.
     */
    _listByResourceGroupNext(resourceGroupName, nextLink, options) {
        return this.client.sendOperationRequest({ resourceGroupName, nextLink, options }, listByResourceGroupNextOperationSpec);
    }
    /**
     * ListNext
     * @param nextLink The nextLink from the previous successful call to the List method.
     * @param options The options parameters.
     */
    _listNext(nextLink, options) {
        return this.client.sendOperationRequest({ nextLink, options }, listNextOperationSpec);
    }
}
exports.ApiGatewayImpl = ApiGatewayImpl;
// Operation Specifications
const serializer = coreClient.createSerializer(Mappers, /* isXml */ false);
const createOrUpdateOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/gateways/{gatewayName}",
    httpMethod: "PUT",
    responses: {
        200: {
            bodyMapper: Mappers.ApiManagementGatewayResource,
        },
        201: {
            bodyMapper: Mappers.ApiManagementGatewayResource,
        },
        202: {
            bodyMapper: Mappers.ApiManagementGatewayResource,
        },
        204: {
            bodyMapper: Mappers.ApiManagementGatewayResource,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    requestBody: Parameters.parameters,
    queryParameters: [Parameters.apiVersion],
    urlParameters: [
        Parameters.$host,
        Parameters.resourceGroupName,
        Parameters.gatewayName,
        Parameters.subscriptionId,
    ],
    headerParameters: [Parameters.contentType, Parameters.accept],
    mediaType: "json",
    serializer,
};
const updateOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/gateways/{gatewayName}",
    httpMethod: "PATCH",
    responses: {
        200: {
            bodyMapper: Mappers.ApiManagementGatewayResource,
        },
        201: {
            bodyMapper: Mappers.ApiManagementGatewayResource,
        },
        202: {
            bodyMapper: Mappers.ApiManagementGatewayResource,
        },
        204: {
            bodyMapper: Mappers.ApiManagementGatewayResource,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    requestBody: Parameters.parameters1,
    queryParameters: [Parameters.apiVersion],
    urlParameters: [
        Parameters.$host,
        Parameters.resourceGroupName,
        Parameters.gatewayName,
        Parameters.subscriptionId,
    ],
    headerParameters: [Parameters.contentType, Parameters.accept],
    mediaType: "json",
    serializer,
};
const getOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/gateways/{gatewayName}",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.ApiManagementGatewayResource,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    queryParameters: [Parameters.apiVersion],
    urlParameters: [
        Parameters.$host,
        Parameters.resourceGroupName,
        Parameters.gatewayName,
        Parameters.subscriptionId,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const deleteOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/gateways/{gatewayName}",
    httpMethod: "DELETE",
    responses: {
        200: {
            bodyMapper: Mappers.ApiManagementGatewayResource,
            headersMapper: Mappers.ApiGatewayDeleteHeaders,
        },
        201: {
            bodyMapper: Mappers.ApiManagementGatewayResource,
            headersMapper: Mappers.ApiGatewayDeleteHeaders,
        },
        202: {
            bodyMapper: Mappers.ApiManagementGatewayResource,
            headersMapper: Mappers.ApiGatewayDeleteHeaders,
        },
        204: {
            bodyMapper: Mappers.ApiManagementGatewayResource,
            headersMapper: Mappers.ApiGatewayDeleteHeaders,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    queryParameters: [Parameters.apiVersion],
    urlParameters: [
        Parameters.$host,
        Parameters.resourceGroupName,
        Parameters.gatewayName,
        Parameters.subscriptionId,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const listByResourceGroupOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/gateways",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.ApiManagementGatewayListResult,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    queryParameters: [Parameters.apiVersion],
    urlParameters: [
        Parameters.$host,
        Parameters.resourceGroupName,
        Parameters.subscriptionId,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const listOperationSpec = {
    path: "/subscriptions/{subscriptionId}/providers/Microsoft.ApiManagement/gateways",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.ApiManagementGatewayListResult,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    queryParameters: [Parameters.apiVersion],
    urlParameters: [Parameters.$host, Parameters.subscriptionId],
    headerParameters: [Parameters.accept],
    serializer,
};
const listByResourceGroupNextOperationSpec = {
    path: "{nextLink}",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.ApiManagementGatewayListResult,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    urlParameters: [
        Parameters.$host,
        Parameters.resourceGroupName,
        Parameters.subscriptionId,
        Parameters.nextLink,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const listNextOperationSpec = {
    path: "{nextLink}",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.ApiManagementGatewayListResult,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.nextLink,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
//# sourceMappingURL=apiGateway.js.map