/*
 * 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 ApiSchema operations. */
export class ApiSchemaImpl {
    /**
     * Initialize a new instance of the class ApiSchema class.
     * @param client Reference to the service client
     */
    constructor(client) {
        this.client = client;
    }
    /**
     * Get the schema configuration at the API level.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceName The name of the API Management service.
     * @param apiId API revision identifier. Must be unique in the current API Management service instance.
     *              Non-current revision has ;rev=n as a suffix where n is the revision number.
     * @param options The options parameters.
     */
    listByApi(resourceGroupName, serviceName, apiId, options) {
        const iter = this.listByApiPagingAll(resourceGroupName, serviceName, apiId, 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.listByApiPagingPage(resourceGroupName, serviceName, apiId, options, settings);
            },
        };
    }
    listByApiPagingPage(resourceGroupName, serviceName, apiId, options, settings) {
        return __asyncGenerator(this, arguments, function* listByApiPagingPage_1() {
            let result;
            let continuationToken = settings === null || settings === void 0 ? void 0 : settings.continuationToken;
            if (!continuationToken) {
                result = yield __await(this._listByApi(resourceGroupName, serviceName, apiId, options));
                let page = result.value || [];
                continuationToken = result.nextLink;
                setContinuationToken(page, continuationToken);
                yield yield __await(page);
            }
            while (continuationToken) {
                result = yield __await(this._listByApiNext(resourceGroupName, serviceName, apiId, continuationToken, options));
                continuationToken = result.nextLink;
                let page = result.value || [];
                setContinuationToken(page, continuationToken);
                yield yield __await(page);
            }
        });
    }
    listByApiPagingAll(resourceGroupName, serviceName, apiId, options) {
        return __asyncGenerator(this, arguments, function* listByApiPagingAll_1() {
            var _a, e_1, _b, _c;
            try {
                for (var _d = true, _e = __asyncValues(this.listByApiPagingPage(resourceGroupName, serviceName, apiId, 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; }
            }
        });
    }
    /**
     * Get the schema configuration at the API level.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceName The name of the API Management service.
     * @param apiId API revision identifier. Must be unique in the current API Management service instance.
     *              Non-current revision has ;rev=n as a suffix where n is the revision number.
     * @param options The options parameters.
     */
    _listByApi(resourceGroupName, serviceName, apiId, options) {
        return this.client.sendOperationRequest({ resourceGroupName, serviceName, apiId, options }, listByApiOperationSpec);
    }
    /**
     * Gets the entity state (Etag) version of the schema specified by its identifier.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceName The name of the API Management service.
     * @param apiId API revision identifier. Must be unique in the current API Management service instance.
     *              Non-current revision has ;rev=n as a suffix where n is the revision number.
     * @param schemaId Schema id identifier. Must be unique in the current API Management service instance.
     * @param options The options parameters.
     */
    getEntityTag(resourceGroupName, serviceName, apiId, schemaId, options) {
        return this.client.sendOperationRequest({ resourceGroupName, serviceName, apiId, schemaId, options }, getEntityTagOperationSpec);
    }
    /**
     * Get the schema configuration at the API level.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceName The name of the API Management service.
     * @param apiId API revision identifier. Must be unique in the current API Management service instance.
     *              Non-current revision has ;rev=n as a suffix where n is the revision number.
     * @param schemaId Schema id identifier. Must be unique in the current API Management service instance.
     * @param options The options parameters.
     */
    get(resourceGroupName, serviceName, apiId, schemaId, options) {
        return this.client.sendOperationRequest({ resourceGroupName, serviceName, apiId, schemaId, options }, getOperationSpec);
    }
    /**
     * Creates or updates schema configuration for the API.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceName The name of the API Management service.
     * @param apiId API revision identifier. Must be unique in the current API Management service instance.
     *              Non-current revision has ;rev=n as a suffix where n is the revision number.
     * @param schemaId Schema id identifier. Must be unique in the current API Management service instance.
     * @param parameters The schema contents to apply.
     * @param options The options parameters.
     */
    async beginCreateOrUpdate(resourceGroupName, serviceName, apiId, schemaId, 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,
                serviceName,
                apiId,
                schemaId,
                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: "location",
        });
        await poller.poll();
        return poller;
    }
    /**
     * Creates or updates schema configuration for the API.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceName The name of the API Management service.
     * @param apiId API revision identifier. Must be unique in the current API Management service instance.
     *              Non-current revision has ;rev=n as a suffix where n is the revision number.
     * @param schemaId Schema id identifier. Must be unique in the current API Management service instance.
     * @param parameters The schema contents to apply.
     * @param options The options parameters.
     */
    async beginCreateOrUpdateAndWait(resourceGroupName, serviceName, apiId, schemaId, parameters, options) {
        const poller = await this.beginCreateOrUpdate(resourceGroupName, serviceName, apiId, schemaId, parameters, options);
        return poller.pollUntilDone();
    }
    /**
     * Deletes the schema configuration at the Api.
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceName The name of the API Management service.
     * @param apiId API revision identifier. Must be unique in the current API Management service instance.
     *              Non-current revision has ;rev=n as a suffix where n is the revision number.
     * @param schemaId Schema id identifier. Must be unique in the current API Management service instance.
     * @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.
     */
    delete(resourceGroupName, serviceName, apiId, schemaId, ifMatch, options) {
        return this.client.sendOperationRequest({ resourceGroupName, serviceName, apiId, schemaId, ifMatch, options }, deleteOperationSpec);
    }
    /**
     * ListByApiNext
     * @param resourceGroupName The name of the resource group. The name is case insensitive.
     * @param serviceName The name of the API Management service.
     * @param apiId API revision identifier. Must be unique in the current API Management service instance.
     *              Non-current revision has ;rev=n as a suffix where n is the revision number.
     * @param nextLink The nextLink from the previous successful call to the ListByApi method.
     * @param options The options parameters.
     */
    _listByApiNext(resourceGroupName, serviceName, apiId, nextLink, options) {
        return this.client.sendOperationRequest({ resourceGroupName, serviceName, apiId, nextLink, options }, listByApiNextOperationSpec);
    }
}
// Operation Specifications
const serializer = coreClient.createSerializer(Mappers, /* isXml */ false);
const listByApiOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/schemas",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.SchemaCollection,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    queryParameters: [
        Parameters.apiVersion,
        Parameters.filter,
        Parameters.top,
        Parameters.skip,
    ],
    urlParameters: [
        Parameters.$host,
        Parameters.resourceGroupName,
        Parameters.subscriptionId,
        Parameters.serviceName,
        Parameters.apiId,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const getEntityTagOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/schemas/{schemaId}",
    httpMethod: "HEAD",
    responses: {
        200: {
            headersMapper: Mappers.ApiSchemaGetEntityTagHeaders,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    queryParameters: [Parameters.apiVersion],
    urlParameters: [
        Parameters.$host,
        Parameters.resourceGroupName,
        Parameters.subscriptionId,
        Parameters.serviceName,
        Parameters.apiId,
        Parameters.schemaId,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const getOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/schemas/{schemaId}",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.SchemaContract,
            headersMapper: Mappers.ApiSchemaGetHeaders,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    queryParameters: [Parameters.apiVersion],
    urlParameters: [
        Parameters.$host,
        Parameters.resourceGroupName,
        Parameters.subscriptionId,
        Parameters.serviceName,
        Parameters.apiId,
        Parameters.schemaId,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const createOrUpdateOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/schemas/{schemaId}",
    httpMethod: "PUT",
    responses: {
        200: {
            bodyMapper: Mappers.SchemaContract,
            headersMapper: Mappers.ApiSchemaCreateOrUpdateHeaders,
        },
        201: {
            bodyMapper: Mappers.SchemaContract,
            headersMapper: Mappers.ApiSchemaCreateOrUpdateHeaders,
        },
        202: {
            bodyMapper: Mappers.SchemaContract,
            headersMapper: Mappers.ApiSchemaCreateOrUpdateHeaders,
        },
        204: {
            bodyMapper: Mappers.SchemaContract,
            headersMapper: Mappers.ApiSchemaCreateOrUpdateHeaders,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    requestBody: Parameters.parameters11,
    queryParameters: [Parameters.apiVersion],
    urlParameters: [
        Parameters.$host,
        Parameters.resourceGroupName,
        Parameters.subscriptionId,
        Parameters.serviceName,
        Parameters.apiId,
        Parameters.schemaId,
    ],
    headerParameters: [
        Parameters.contentType,
        Parameters.accept,
        Parameters.ifMatch,
    ],
    mediaType: "json",
    serializer,
};
const deleteOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/apis/{apiId}/schemas/{schemaId}",
    httpMethod: "DELETE",
    responses: {
        200: {},
        204: {},
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    queryParameters: [Parameters.apiVersion, Parameters.force],
    urlParameters: [
        Parameters.$host,
        Parameters.resourceGroupName,
        Parameters.subscriptionId,
        Parameters.serviceName,
        Parameters.apiId,
        Parameters.schemaId,
    ],
    headerParameters: [Parameters.accept, Parameters.ifMatch1],
    serializer,
};
const listByApiNextOperationSpec = {
    path: "{nextLink}",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.SchemaCollection,
        },
        default: {
            bodyMapper: Mappers.ErrorResponse,
        },
    },
    urlParameters: [
        Parameters.$host,
        Parameters.resourceGroupName,
        Parameters.subscriptionId,
        Parameters.nextLink,
        Parameters.serviceName,
        Parameters.apiId,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
//# sourceMappingURL=apiSchema.js.map