"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeletedServersImpl = 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 DeletedServers operations. */
class DeletedServersImpl {
    client;
    /**
     * Initialize a new instance of the class DeletedServers class.
     * @param client Reference to the service client
     */
    constructor(client) {
        this.client = client;
    }
    /**
     * Gets a list of all deleted servers in 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?.maxPageSize) {
                    throw new Error("maxPageSize is not supported by this operation.");
                }
                return this.listPagingPage(options, settings);
            },
        };
    }
    async *listPagingPage(options, settings) {
        let result;
        let continuationToken = settings?.continuationToken;
        if (!continuationToken) {
            result = await this._list(options);
            let page = result.value || [];
            continuationToken = result.nextLink;
            (0, pagingHelper_js_1.setContinuationToken)(page, continuationToken);
            yield page;
        }
        while (continuationToken) {
            result = await this._listNext(continuationToken, options);
            continuationToken = result.nextLink;
            let page = result.value || [];
            (0, pagingHelper_js_1.setContinuationToken)(page, continuationToken);
            yield page;
        }
    }
    async *listPagingAll(options) {
        for await (const page of this.listPagingPage(options)) {
            yield* page;
        }
    }
    /**
     * Gets a list of deleted servers for a location.
     * @param locationName The name of the region where the resource is located.
     * @param options The options parameters.
     */
    listByLocation(locationName, options) {
        const iter = this.listByLocationPagingAll(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(locationName, options, settings);
            },
        };
    }
    async *listByLocationPagingPage(locationName, options, settings) {
        let result;
        let continuationToken = settings?.continuationToken;
        if (!continuationToken) {
            result = await this._listByLocation(locationName, options);
            let page = result.value || [];
            continuationToken = result.nextLink;
            (0, pagingHelper_js_1.setContinuationToken)(page, continuationToken);
            yield page;
        }
        while (continuationToken) {
            result = await this._listByLocationNext(locationName, continuationToken, options);
            continuationToken = result.nextLink;
            let page = result.value || [];
            (0, pagingHelper_js_1.setContinuationToken)(page, continuationToken);
            yield page;
        }
    }
    async *listByLocationPagingAll(locationName, options) {
        for await (const page of this.listByLocationPagingPage(locationName, options)) {
            yield* page;
        }
    }
    /**
     * Gets a list of all deleted servers in a subscription.
     * @param options The options parameters.
     */
    _list(options) {
        return this.client.sendOperationRequest({ options }, listOperationSpec);
    }
    /**
     * Gets a deleted server.
     * @param locationName The name of the region where the resource is located.
     * @param deletedServerName The name of the deleted server.
     * @param options The options parameters.
     */
    get(locationName, deletedServerName, options) {
        return this.client.sendOperationRequest({ locationName, deletedServerName, options }, getOperationSpec);
    }
    /**
     * Gets a list of deleted servers for a location.
     * @param locationName The name of the region where the resource is located.
     * @param options The options parameters.
     */
    _listByLocation(locationName, options) {
        return this.client.sendOperationRequest({ locationName, options }, listByLocationOperationSpec);
    }
    /**
     * Recovers a deleted server.
     * @param locationName The name of the region where the resource is located.
     * @param deletedServerName The name of the deleted server.
     * @param options The options parameters.
     */
    async beginRecover(locationName, deletedServerName, 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: { locationName, deletedServerName, options },
            spec: recoverOperationSpec,
        });
        const poller = await (0, core_lro_1.createHttpPoller)(lro, {
            restoreFrom: options?.resumeFrom,
            intervalInMs: options?.updateIntervalInMs,
        });
        await poller.poll();
        return poller;
    }
    /**
     * Recovers a deleted server.
     * @param locationName The name of the region where the resource is located.
     * @param deletedServerName The name of the deleted server.
     * @param options The options parameters.
     */
    async beginRecoverAndWait(locationName, deletedServerName, options) {
        const poller = await this.beginRecover(locationName, deletedServerName, options);
        return poller.pollUntilDone();
    }
    /**
     * 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);
    }
    /**
     * ListByLocationNext
     * @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(locationName, nextLink, options) {
        return this.client.sendOperationRequest({ locationName, nextLink, options }, listByLocationNextOperationSpec);
    }
}
exports.DeletedServersImpl = DeletedServersImpl;
// Operation Specifications
const serializer = coreClient.createSerializer(Mappers, /* isXml */ false);
const listOperationSpec = {
    path: "/subscriptions/{subscriptionId}/providers/Microsoft.Sql/deletedServers",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.DeletedServerListResult,
        },
        default: {},
    },
    queryParameters: [Parameters.apiVersion3],
    urlParameters: [Parameters.$host, Parameters.subscriptionId],
    headerParameters: [Parameters.accept],
    serializer,
};
const getOperationSpec = {
    path: "/subscriptions/{subscriptionId}/providers/Microsoft.Sql/locations/{locationName}/deletedServers/{deletedServerName}",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.DeletedServer,
        },
        default: {},
    },
    queryParameters: [Parameters.apiVersion3],
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.locationName,
        Parameters.deletedServerName,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const listByLocationOperationSpec = {
    path: "/subscriptions/{subscriptionId}/providers/Microsoft.Sql/locations/{locationName}/deletedServers",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.DeletedServerListResult,
        },
        default: {},
    },
    queryParameters: [Parameters.apiVersion3],
    urlParameters: [Parameters.$host, Parameters.subscriptionId, Parameters.locationName],
    headerParameters: [Parameters.accept],
    serializer,
};
const recoverOperationSpec = {
    path: "/subscriptions/{subscriptionId}/providers/Microsoft.Sql/locations/{locationName}/deletedServers/{deletedServerName}/recover",
    httpMethod: "POST",
    responses: {
        200: {
            bodyMapper: Mappers.DeletedServer,
        },
        201: {
            bodyMapper: Mappers.DeletedServer,
        },
        202: {
            bodyMapper: Mappers.DeletedServer,
        },
        204: {
            bodyMapper: Mappers.DeletedServer,
        },
        default: {},
    },
    queryParameters: [Parameters.apiVersion3],
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.locationName,
        Parameters.deletedServerName,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const listNextOperationSpec = {
    path: "{nextLink}",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.DeletedServerListResult,
        },
        default: {},
    },
    urlParameters: [Parameters.$host, Parameters.subscriptionId, Parameters.nextLink],
    headerParameters: [Parameters.accept],
    serializer,
};
const listByLocationNextOperationSpec = {
    path: "{nextLink}",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.DeletedServerListResult,
        },
        default: {},
    },
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.nextLink,
        Parameters.locationName,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
//# sourceMappingURL=deletedServers.js.map