"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.InstanceFailoverGroupsImpl = 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 InstanceFailoverGroups operations. */
class InstanceFailoverGroupsImpl {
    client;
    /**
     * Initialize a new instance of the class InstanceFailoverGroups class.
     * @param client Reference to the service client
     */
    constructor(client) {
        this.client = client;
    }
    /**
     * Lists the failover groups in a location.
     * @param resourceGroupName The name of the resource group that contains the resource. You can obtain
     *                          this value from the Azure Resource Manager API or the portal.
     * @param locationName The name of the region where the resource is located.
     * @param options The options parameters.
     */
    listByLocation(resourceGroupName, locationName, options) {
        const iter = this.listByLocationPagingAll(resourceGroupName, locationName, options);
        return {
            next() {
                return iter.next();
            },
            [Symbol.asyncIterator]() {
                return this;
            },
            byPage: (settings) => {
                if (settings?.maxPageSize) {
                    throw new Error("maxPageSize is not supported by this operation.");
                }
                return this.listByLocationPagingPage(resourceGroupName, locationName, options, settings);
            },
        };
    }
    async *listByLocationPagingPage(resourceGroupName, locationName, options, settings) {
        let result;
        let continuationToken = settings?.continuationToken;
        if (!continuationToken) {
            result = await this._listByLocation(resourceGroupName, locationName, options);
            let page = result.value || [];
            continuationToken = result.nextLink;
            (0, pagingHelper_js_1.setContinuationToken)(page, continuationToken);
            yield page;
        }
        while (continuationToken) {
            result = await this._listByLocationNext(resourceGroupName, locationName, continuationToken, options);
            continuationToken = result.nextLink;
            let page = result.value || [];
            (0, pagingHelper_js_1.setContinuationToken)(page, continuationToken);
            yield page;
        }
    }
    async *listByLocationPagingAll(resourceGroupName, locationName, options) {
        for await (const page of this.listByLocationPagingPage(resourceGroupName, locationName, options)) {
            yield* page;
        }
    }
    /**
     * Lists the failover groups in a location.
     * @param resourceGroupName The name of the resource group that contains the resource. You can obtain
     *                          this value from the Azure Resource Manager API or the portal.
     * @param locationName The name of the region where the resource is located.
     * @param options The options parameters.
     */
    _listByLocation(resourceGroupName, locationName, options) {
        return this.client.sendOperationRequest({ resourceGroupName, locationName, options }, listByLocationOperationSpec);
    }
    /**
     * Gets a failover group.
     * @param resourceGroupName The name of the resource group that contains the resource. You can obtain
     *                          this value from the Azure Resource Manager API or the portal.
     * @param locationName The name of the region where the resource is located.
     * @param failoverGroupName The name of the failover group.
     * @param options The options parameters.
     */
    get(resourceGroupName, locationName, failoverGroupName, options) {
        return this.client.sendOperationRequest({ resourceGroupName, locationName, failoverGroupName, options }, getOperationSpec);
    }
    /**
     * Creates or updates a failover group.
     * @param resourceGroupName The name of the resource group that contains the resource. You can obtain
     *                          this value from the Azure Resource Manager API or the portal.
     * @param locationName The name of the region where the resource is located.
     * @param failoverGroupName The name of the failover group.
     * @param parameters The failover group parameters.
     * @param options The options parameters.
     */
    async beginCreateOrUpdate(resourceGroupName, locationName, failoverGroupName, parameters, options) {
        const directSendOperation = async (args, spec) => {
            return this.client.sendOperationRequest(args, spec);
        };
        const sendOperationFn = async (args, spec) => {
            let currentRawResponse = undefined;
            const providedCallback = args.options?.onResponse;
            const callback = (rawResponse, flatResponse) => {
                currentRawResponse = rawResponse;
                providedCallback?.(rawResponse, flatResponse);
            };
            const updatedArgs = {
                ...args,
                options: {
                    ...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,
                locationName,
                failoverGroupName,
                parameters,
                options,
            },
            spec: createOrUpdateOperationSpec,
        });
        const poller = await (0, core_lro_1.createHttpPoller)(lro, {
            restoreFrom: options?.resumeFrom,
            intervalInMs: options?.updateIntervalInMs,
        });
        await poller.poll();
        return poller;
    }
    /**
     * Creates or updates a failover group.
     * @param resourceGroupName The name of the resource group that contains the resource. You can obtain
     *                          this value from the Azure Resource Manager API or the portal.
     * @param locationName The name of the region where the resource is located.
     * @param failoverGroupName The name of the failover group.
     * @param parameters The failover group parameters.
     * @param options The options parameters.
     */
    async beginCreateOrUpdateAndWait(resourceGroupName, locationName, failoverGroupName, parameters, options) {
        const poller = await this.beginCreateOrUpdate(resourceGroupName, locationName, failoverGroupName, parameters, options);
        return poller.pollUntilDone();
    }
    /**
     * Deletes a failover group.
     * @param resourceGroupName The name of the resource group that contains the resource. You can obtain
     *                          this value from the Azure Resource Manager API or the portal.
     * @param locationName The name of the region where the resource is located.
     * @param failoverGroupName The name of the failover group.
     * @param options The options parameters.
     */
    async beginDelete(resourceGroupName, locationName, failoverGroupName, options) {
        const directSendOperation = async (args, spec) => {
            return this.client.sendOperationRequest(args, spec);
        };
        const sendOperationFn = async (args, spec) => {
            let currentRawResponse = undefined;
            const providedCallback = args.options?.onResponse;
            const callback = (rawResponse, flatResponse) => {
                currentRawResponse = rawResponse;
                providedCallback?.(rawResponse, flatResponse);
            };
            const updatedArgs = {
                ...args,
                options: {
                    ...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, locationName, failoverGroupName, options },
            spec: deleteOperationSpec,
        });
        const poller = await (0, core_lro_1.createHttpPoller)(lro, {
            restoreFrom: options?.resumeFrom,
            intervalInMs: options?.updateIntervalInMs,
        });
        await poller.poll();
        return poller;
    }
    /**
     * Deletes a failover group.
     * @param resourceGroupName The name of the resource group that contains the resource. You can obtain
     *                          this value from the Azure Resource Manager API or the portal.
     * @param locationName The name of the region where the resource is located.
     * @param failoverGroupName The name of the failover group.
     * @param options The options parameters.
     */
    async beginDeleteAndWait(resourceGroupName, locationName, failoverGroupName, options) {
        const poller = await this.beginDelete(resourceGroupName, locationName, failoverGroupName, options);
        return poller.pollUntilDone();
    }
    /**
     * Fails over from the current primary managed instance to this managed instance.
     * @param resourceGroupName The name of the resource group that contains the resource. You can obtain
     *                          this value from the Azure Resource Manager API or the portal.
     * @param locationName The name of the region where the resource is located.
     * @param failoverGroupName The name of the failover group.
     * @param options The options parameters.
     */
    async beginFailover(resourceGroupName, locationName, failoverGroupName, options) {
        const directSendOperation = async (args, spec) => {
            return this.client.sendOperationRequest(args, spec);
        };
        const sendOperationFn = async (args, spec) => {
            let currentRawResponse = undefined;
            const providedCallback = args.options?.onResponse;
            const callback = (rawResponse, flatResponse) => {
                currentRawResponse = rawResponse;
                providedCallback?.(rawResponse, flatResponse);
            };
            const updatedArgs = {
                ...args,
                options: {
                    ...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, locationName, failoverGroupName, options },
            spec: failoverOperationSpec,
        });
        const poller = await (0, core_lro_1.createHttpPoller)(lro, {
            restoreFrom: options?.resumeFrom,
            intervalInMs: options?.updateIntervalInMs,
        });
        await poller.poll();
        return poller;
    }
    /**
     * Fails over from the current primary managed instance to this managed instance.
     * @param resourceGroupName The name of the resource group that contains the resource. You can obtain
     *                          this value from the Azure Resource Manager API or the portal.
     * @param locationName The name of the region where the resource is located.
     * @param failoverGroupName The name of the failover group.
     * @param options The options parameters.
     */
    async beginFailoverAndWait(resourceGroupName, locationName, failoverGroupName, options) {
        const poller = await this.beginFailover(resourceGroupName, locationName, failoverGroupName, options);
        return poller.pollUntilDone();
    }
    /**
     * Fails over from the current primary managed instance to this managed instance. This operation might
     * result in data loss.
     * @param resourceGroupName The name of the resource group that contains the resource. You can obtain
     *                          this value from the Azure Resource Manager API or the portal.
     * @param locationName The name of the region where the resource is located.
     * @param failoverGroupName The name of the failover group.
     * @param options The options parameters.
     */
    async beginForceFailoverAllowDataLoss(resourceGroupName, locationName, failoverGroupName, options) {
        const directSendOperation = async (args, spec) => {
            return this.client.sendOperationRequest(args, spec);
        };
        const sendOperationFn = async (args, spec) => {
            let currentRawResponse = undefined;
            const providedCallback = args.options?.onResponse;
            const callback = (rawResponse, flatResponse) => {
                currentRawResponse = rawResponse;
                providedCallback?.(rawResponse, flatResponse);
            };
            const updatedArgs = {
                ...args,
                options: {
                    ...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, locationName, failoverGroupName, options },
            spec: forceFailoverAllowDataLossOperationSpec,
        });
        const poller = await (0, core_lro_1.createHttpPoller)(lro, {
            restoreFrom: options?.resumeFrom,
            intervalInMs: options?.updateIntervalInMs,
        });
        await poller.poll();
        return poller;
    }
    /**
     * Fails over from the current primary managed instance to this managed instance. This operation might
     * result in data loss.
     * @param resourceGroupName The name of the resource group that contains the resource. You can obtain
     *                          this value from the Azure Resource Manager API or the portal.
     * @param locationName The name of the region where the resource is located.
     * @param failoverGroupName The name of the failover group.
     * @param options The options parameters.
     */
    async beginForceFailoverAllowDataLossAndWait(resourceGroupName, locationName, failoverGroupName, options) {
        const poller = await this.beginForceFailoverAllowDataLoss(resourceGroupName, locationName, failoverGroupName, options);
        return poller.pollUntilDone();
    }
    /**
     * ListByLocationNext
     * @param resourceGroupName The name of the resource group that contains the resource. You can obtain
     *                          this value from the Azure Resource Manager API or the portal.
     * @param locationName The name of the region where the resource is located.
     * @param nextLink The nextLink from the previous successful call to the ListByLocation method.
     * @param options The options parameters.
     */
    _listByLocationNext(resourceGroupName, locationName, nextLink, options) {
        return this.client.sendOperationRequest({ resourceGroupName, locationName, nextLink, options }, listByLocationNextOperationSpec);
    }
}
exports.InstanceFailoverGroupsImpl = InstanceFailoverGroupsImpl;
// Operation Specifications
const serializer = coreClient.createSerializer(Mappers, /* isXml */ false);
const listByLocationOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/locations/{locationName}/instanceFailoverGroups",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.InstanceFailoverGroupListResult,
        },
        default: {},
    },
    queryParameters: [Parameters.apiVersion9],
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.resourceGroupName,
        Parameters.locationName,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const getOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/locations/{locationName}/instanceFailoverGroups/{failoverGroupName}",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.InstanceFailoverGroup,
        },
        default: {},
    },
    queryParameters: [Parameters.apiVersion9],
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.resourceGroupName,
        Parameters.locationName,
        Parameters.failoverGroupName,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const createOrUpdateOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/locations/{locationName}/instanceFailoverGroups/{failoverGroupName}",
    httpMethod: "PUT",
    responses: {
        200: {
            bodyMapper: Mappers.InstanceFailoverGroup,
        },
        201: {
            bodyMapper: Mappers.InstanceFailoverGroup,
        },
        202: {
            bodyMapper: Mappers.InstanceFailoverGroup,
        },
        204: {
            bodyMapper: Mappers.InstanceFailoverGroup,
        },
        default: {},
    },
    requestBody: Parameters.parameters79,
    queryParameters: [Parameters.apiVersion9],
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.resourceGroupName,
        Parameters.locationName,
        Parameters.failoverGroupName,
    ],
    headerParameters: [Parameters.contentType, Parameters.accept],
    mediaType: "json",
    serializer,
};
const deleteOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/locations/{locationName}/instanceFailoverGroups/{failoverGroupName}",
    httpMethod: "DELETE",
    responses: { 200: {}, 201: {}, 202: {}, 204: {}, default: {} },
    queryParameters: [Parameters.apiVersion9],
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.resourceGroupName,
        Parameters.locationName,
        Parameters.failoverGroupName,
    ],
    serializer,
};
const failoverOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/locations/{locationName}/instanceFailoverGroups/{failoverGroupName}/failover",
    httpMethod: "POST",
    responses: {
        200: {
            bodyMapper: Mappers.InstanceFailoverGroup,
        },
        201: {
            bodyMapper: Mappers.InstanceFailoverGroup,
        },
        202: {
            bodyMapper: Mappers.InstanceFailoverGroup,
        },
        204: {
            bodyMapper: Mappers.InstanceFailoverGroup,
        },
        default: {},
    },
    queryParameters: [Parameters.apiVersion9],
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.resourceGroupName,
        Parameters.locationName,
        Parameters.failoverGroupName,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const forceFailoverAllowDataLossOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/locations/{locationName}/instanceFailoverGroups/{failoverGroupName}/forceFailoverAllowDataLoss",
    httpMethod: "POST",
    responses: {
        200: {
            bodyMapper: Mappers.InstanceFailoverGroup,
        },
        201: {
            bodyMapper: Mappers.InstanceFailoverGroup,
        },
        202: {
            bodyMapper: Mappers.InstanceFailoverGroup,
        },
        204: {
            bodyMapper: Mappers.InstanceFailoverGroup,
        },
        default: {},
    },
    queryParameters: [Parameters.apiVersion9],
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.resourceGroupName,
        Parameters.locationName,
        Parameters.failoverGroupName,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const listByLocationNextOperationSpec = {
    path: "{nextLink}",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.InstanceFailoverGroupListResult,
        },
        default: {},
    },
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.resourceGroupName,
        Parameters.nextLink,
        Parameters.locationName,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
//# sourceMappingURL=instanceFailoverGroups.js.map