// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
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 JobExecutions operations. */
export class JobExecutionsImpl {
    client;
    /**
     * Initialize a new instance of the class JobExecutions class.
     * @param client Reference to the service client
     */
    constructor(client) {
        this.client = client;
    }
    /**
     * Lists all executions in a job agent.
     * @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 serverName The name of the server.
     * @param jobAgentName The name of the job agent.
     * @param options The options parameters.
     */
    listByAgent(resourceGroupName, serverName, jobAgentName, options) {
        const iter = this.listByAgentPagingAll(resourceGroupName, serverName, jobAgentName, 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.listByAgentPagingPage(resourceGroupName, serverName, jobAgentName, options, settings);
            },
        };
    }
    async *listByAgentPagingPage(resourceGroupName, serverName, jobAgentName, options, settings) {
        let result;
        let continuationToken = settings?.continuationToken;
        if (!continuationToken) {
            result = await this._listByAgent(resourceGroupName, serverName, jobAgentName, options);
            let page = result.value || [];
            continuationToken = result.nextLink;
            setContinuationToken(page, continuationToken);
            yield page;
        }
        while (continuationToken) {
            result = await this._listByAgentNext(resourceGroupName, serverName, jobAgentName, continuationToken, options);
            continuationToken = result.nextLink;
            let page = result.value || [];
            setContinuationToken(page, continuationToken);
            yield page;
        }
    }
    async *listByAgentPagingAll(resourceGroupName, serverName, jobAgentName, options) {
        for await (const page of this.listByAgentPagingPage(resourceGroupName, serverName, jobAgentName, options)) {
            yield* page;
        }
    }
    /**
     * Lists a job's executions.
     * @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 serverName The name of the server.
     * @param jobAgentName The name of the job agent.
     * @param jobName The name of the job to get.
     * @param options The options parameters.
     */
    listByJob(resourceGroupName, serverName, jobAgentName, jobName, options) {
        const iter = this.listByJobPagingAll(resourceGroupName, serverName, jobAgentName, jobName, 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.listByJobPagingPage(resourceGroupName, serverName, jobAgentName, jobName, options, settings);
            },
        };
    }
    async *listByJobPagingPage(resourceGroupName, serverName, jobAgentName, jobName, options, settings) {
        let result;
        let continuationToken = settings?.continuationToken;
        if (!continuationToken) {
            result = await this._listByJob(resourceGroupName, serverName, jobAgentName, jobName, options);
            let page = result.value || [];
            continuationToken = result.nextLink;
            setContinuationToken(page, continuationToken);
            yield page;
        }
        while (continuationToken) {
            result = await this._listByJobNext(resourceGroupName, serverName, jobAgentName, jobName, continuationToken, options);
            continuationToken = result.nextLink;
            let page = result.value || [];
            setContinuationToken(page, continuationToken);
            yield page;
        }
    }
    async *listByJobPagingAll(resourceGroupName, serverName, jobAgentName, jobName, options) {
        for await (const page of this.listByJobPagingPage(resourceGroupName, serverName, jobAgentName, jobName, options)) {
            yield* page;
        }
    }
    /**
     * Lists all executions in a job agent.
     * @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 serverName The name of the server.
     * @param jobAgentName The name of the job agent.
     * @param options The options parameters.
     */
    _listByAgent(resourceGroupName, serverName, jobAgentName, options) {
        return this.client.sendOperationRequest({ resourceGroupName, serverName, jobAgentName, options }, listByAgentOperationSpec);
    }
    /**
     * Requests cancellation of a job execution.
     * @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 serverName The name of the server.
     * @param jobAgentName The name of the job agent.
     * @param jobName The name of the job.
     * @param jobExecutionId The id of the job execution to cancel.
     * @param options The options parameters.
     */
    cancel(resourceGroupName, serverName, jobAgentName, jobName, jobExecutionId, options) {
        return this.client.sendOperationRequest({
            resourceGroupName,
            serverName,
            jobAgentName,
            jobName,
            jobExecutionId,
            options,
        }, cancelOperationSpec);
    }
    /**
     * Starts an elastic job execution.
     * @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 serverName The name of the server.
     * @param jobAgentName The name of the job agent.
     * @param jobName The name of the job to get.
     * @param options The options parameters.
     */
    async beginCreate(resourceGroupName, serverName, jobAgentName, jobName, 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 = createLroSpec({
            sendOperationFn,
            args: { resourceGroupName, serverName, jobAgentName, jobName, options },
            spec: createOperationSpec,
        });
        const poller = await createHttpPoller(lro, {
            restoreFrom: options?.resumeFrom,
            intervalInMs: options?.updateIntervalInMs,
        });
        await poller.poll();
        return poller;
    }
    /**
     * Starts an elastic job execution.
     * @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 serverName The name of the server.
     * @param jobAgentName The name of the job agent.
     * @param jobName The name of the job to get.
     * @param options The options parameters.
     */
    async beginCreateAndWait(resourceGroupName, serverName, jobAgentName, jobName, options) {
        const poller = await this.beginCreate(resourceGroupName, serverName, jobAgentName, jobName, options);
        return poller.pollUntilDone();
    }
    /**
     * Lists a job's executions.
     * @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 serverName The name of the server.
     * @param jobAgentName The name of the job agent.
     * @param jobName The name of the job to get.
     * @param options The options parameters.
     */
    _listByJob(resourceGroupName, serverName, jobAgentName, jobName, options) {
        return this.client.sendOperationRequest({ resourceGroupName, serverName, jobAgentName, jobName, options }, listByJobOperationSpec);
    }
    /**
     * Gets a job execution.
     * @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 serverName The name of the server.
     * @param jobAgentName The name of the job agent.
     * @param jobName The name of the job.
     * @param jobExecutionId The id of the job execution
     * @param options The options parameters.
     */
    get(resourceGroupName, serverName, jobAgentName, jobName, jobExecutionId, options) {
        return this.client.sendOperationRequest({
            resourceGroupName,
            serverName,
            jobAgentName,
            jobName,
            jobExecutionId,
            options,
        }, getOperationSpec);
    }
    /**
     * Creates or updates a job execution.
     * @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 serverName The name of the server.
     * @param jobAgentName The name of the job agent.
     * @param jobName The name of the job to get.
     * @param jobExecutionId The job execution id to create the job execution under.
     * @param options The options parameters.
     */
    async beginCreateOrUpdate(resourceGroupName, serverName, jobAgentName, jobName, jobExecutionId, 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 = createLroSpec({
            sendOperationFn,
            args: {
                resourceGroupName,
                serverName,
                jobAgentName,
                jobName,
                jobExecutionId,
                options,
            },
            spec: createOrUpdateOperationSpec,
        });
        const poller = await createHttpPoller(lro, {
            restoreFrom: options?.resumeFrom,
            intervalInMs: options?.updateIntervalInMs,
        });
        await poller.poll();
        return poller;
    }
    /**
     * Creates or updates a job execution.
     * @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 serverName The name of the server.
     * @param jobAgentName The name of the job agent.
     * @param jobName The name of the job to get.
     * @param jobExecutionId The job execution id to create the job execution under.
     * @param options The options parameters.
     */
    async beginCreateOrUpdateAndWait(resourceGroupName, serverName, jobAgentName, jobName, jobExecutionId, options) {
        const poller = await this.beginCreateOrUpdate(resourceGroupName, serverName, jobAgentName, jobName, jobExecutionId, options);
        return poller.pollUntilDone();
    }
    /**
     * ListByAgentNext
     * @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 serverName The name of the server.
     * @param jobAgentName The name of the job agent.
     * @param nextLink The nextLink from the previous successful call to the ListByAgent method.
     * @param options The options parameters.
     */
    _listByAgentNext(resourceGroupName, serverName, jobAgentName, nextLink, options) {
        return this.client.sendOperationRequest({ resourceGroupName, serverName, jobAgentName, nextLink, options }, listByAgentNextOperationSpec);
    }
    /**
     * ListByJobNext
     * @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 serverName The name of the server.
     * @param jobAgentName The name of the job agent.
     * @param jobName The name of the job to get.
     * @param nextLink The nextLink from the previous successful call to the ListByJob method.
     * @param options The options parameters.
     */
    _listByJobNext(resourceGroupName, serverName, jobAgentName, jobName, nextLink, options) {
        return this.client.sendOperationRequest({
            resourceGroupName,
            serverName,
            jobAgentName,
            jobName,
            nextLink,
            options,
        }, listByJobNextOperationSpec);
    }
}
// Operation Specifications
const serializer = coreClient.createSerializer(Mappers, /* isXml */ false);
const listByAgentOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/jobAgents/{jobAgentName}/executions",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.JobExecutionListResult,
        },
        default: {},
    },
    queryParameters: [
        Parameters.skip,
        Parameters.apiVersion3,
        Parameters.createTimeMin,
        Parameters.createTimeMax,
        Parameters.endTimeMin,
        Parameters.endTimeMax,
        Parameters.isActive,
        Parameters.top,
    ],
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.resourceGroupName,
        Parameters.serverName,
        Parameters.jobAgentName,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const cancelOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/jobAgents/{jobAgentName}/jobs/{jobName}/executions/{jobExecutionId}/cancel",
    httpMethod: "POST",
    responses: { 200: {}, default: {} },
    queryParameters: [Parameters.apiVersion3],
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.resourceGroupName,
        Parameters.serverName,
        Parameters.jobAgentName,
        Parameters.jobName,
        Parameters.jobExecutionId,
    ],
    serializer,
};
const createOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/jobAgents/{jobAgentName}/jobs/{jobName}/start",
    httpMethod: "POST",
    responses: {
        200: {
            bodyMapper: Mappers.JobExecution,
        },
        201: {
            bodyMapper: Mappers.JobExecution,
        },
        202: {
            bodyMapper: Mappers.JobExecution,
        },
        204: {
            bodyMapper: Mappers.JobExecution,
        },
        default: {},
    },
    queryParameters: [Parameters.apiVersion3],
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.resourceGroupName,
        Parameters.serverName,
        Parameters.jobAgentName,
        Parameters.jobName,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const listByJobOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/jobAgents/{jobAgentName}/jobs/{jobName}/executions",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.JobExecutionListResult,
        },
        default: {},
    },
    queryParameters: [
        Parameters.skip,
        Parameters.apiVersion3,
        Parameters.createTimeMin,
        Parameters.createTimeMax,
        Parameters.endTimeMin,
        Parameters.endTimeMax,
        Parameters.isActive,
        Parameters.top,
    ],
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.resourceGroupName,
        Parameters.serverName,
        Parameters.jobAgentName,
        Parameters.jobName,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const getOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/jobAgents/{jobAgentName}/jobs/{jobName}/executions/{jobExecutionId}",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.JobExecution,
        },
        default: {},
    },
    queryParameters: [Parameters.apiVersion3],
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.resourceGroupName,
        Parameters.serverName,
        Parameters.jobAgentName,
        Parameters.jobName,
        Parameters.jobExecutionId,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const createOrUpdateOperationSpec = {
    path: "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/jobAgents/{jobAgentName}/jobs/{jobName}/executions/{jobExecutionId}",
    httpMethod: "PUT",
    responses: {
        200: {
            bodyMapper: Mappers.JobExecution,
        },
        201: {
            bodyMapper: Mappers.JobExecution,
        },
        202: {
            bodyMapper: Mappers.JobExecution,
        },
        204: {
            bodyMapper: Mappers.JobExecution,
        },
        default: {},
    },
    queryParameters: [Parameters.apiVersion3],
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.resourceGroupName,
        Parameters.serverName,
        Parameters.jobAgentName,
        Parameters.jobName,
        Parameters.jobExecutionId,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const listByAgentNextOperationSpec = {
    path: "{nextLink}",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.JobExecutionListResult,
        },
        default: {},
    },
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.resourceGroupName,
        Parameters.serverName,
        Parameters.nextLink,
        Parameters.jobAgentName,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
const listByJobNextOperationSpec = {
    path: "{nextLink}",
    httpMethod: "GET",
    responses: {
        200: {
            bodyMapper: Mappers.JobExecutionListResult,
        },
        default: {},
    },
    urlParameters: [
        Parameters.$host,
        Parameters.subscriptionId,
        Parameters.resourceGroupName,
        Parameters.serverName,
        Parameters.nextLink,
        Parameters.jobAgentName,
        Parameters.jobName,
    ],
    headerParameters: [Parameters.accept],
    serializer,
};
//# sourceMappingURL=jobExecutions.js.map