/*
 * 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 { PagedAsyncIterableIterator, PageSettings } from "@azure/core-paging";
import { setContinuationToken } from "../pagingHelper";
import { CertificateOrdersDiagnostics } from "../operationsInterfaces";
import * as coreClient from "@azure/core-client";
import * as Mappers from "../models/mappers";
import * as Parameters from "../models/parameters";
import { WebSiteManagementClient } from "../webSiteManagementClient";
import {
  DetectorResponse,
  CertificateOrdersDiagnosticsListAppServiceCertificateOrderDetectorResponseNextOptionalParams,
  CertificateOrdersDiagnosticsListAppServiceCertificateOrderDetectorResponseOptionalParams,
  CertificateOrdersDiagnosticsListAppServiceCertificateOrderDetectorResponseResponse,
  CertificateOrdersDiagnosticsGetAppServiceCertificateOrderDetectorResponseOptionalParams,
  CertificateOrdersDiagnosticsGetAppServiceCertificateOrderDetectorResponseResponse,
  CertificateOrdersDiagnosticsListAppServiceCertificateOrderDetectorResponseNextResponse
} from "../models";

/// <reference lib="esnext.asynciterable" />
/** Class containing CertificateOrdersDiagnostics operations. */
export class CertificateOrdersDiagnosticsImpl
  implements CertificateOrdersDiagnostics {
  private readonly client: WebSiteManagementClient;

  /**
   * Initialize a new instance of the class CertificateOrdersDiagnostics class.
   * @param client Reference to the service client
   */
  constructor(client: WebSiteManagementClient) {
    this.client = client;
  }

  /**
   * Description for Microsoft.CertificateRegistration to get the list of detectors for this RP.
   * @param resourceGroupName Name of the resource group to which the resource belongs.
   * @param certificateOrderName The certificate order name for which the response is needed.
   * @param options The options parameters.
   */
  public listAppServiceCertificateOrderDetectorResponse(
    resourceGroupName: string,
    certificateOrderName: string,
    options?: CertificateOrdersDiagnosticsListAppServiceCertificateOrderDetectorResponseOptionalParams
  ): PagedAsyncIterableIterator<DetectorResponse> {
    const iter = this.listAppServiceCertificateOrderDetectorResponsePagingAll(
      resourceGroupName,
      certificateOrderName,
      options
    );
    return {
      next() {
        return iter.next();
      },
      [Symbol.asyncIterator]() {
        return this;
      },
      byPage: (settings?: PageSettings) => {
        if (settings?.maxPageSize) {
          throw new Error("maxPageSize is not supported by this operation.");
        }
        return this.listAppServiceCertificateOrderDetectorResponsePagingPage(
          resourceGroupName,
          certificateOrderName,
          options,
          settings
        );
      }
    };
  }

  private async *listAppServiceCertificateOrderDetectorResponsePagingPage(
    resourceGroupName: string,
    certificateOrderName: string,
    options?: CertificateOrdersDiagnosticsListAppServiceCertificateOrderDetectorResponseOptionalParams,
    settings?: PageSettings
  ): AsyncIterableIterator<DetectorResponse[]> {
    let result: CertificateOrdersDiagnosticsListAppServiceCertificateOrderDetectorResponseResponse;
    let continuationToken = settings?.continuationToken;
    if (!continuationToken) {
      result = await this._listAppServiceCertificateOrderDetectorResponse(
        resourceGroupName,
        certificateOrderName,
        options
      );
      let page = result.value || [];
      continuationToken = result.nextLink;
      setContinuationToken(page, continuationToken);
      yield page;
    }
    while (continuationToken) {
      result = await this._listAppServiceCertificateOrderDetectorResponseNext(
        resourceGroupName,
        certificateOrderName,
        continuationToken,
        options
      );
      continuationToken = result.nextLink;
      let page = result.value || [];
      setContinuationToken(page, continuationToken);
      yield page;
    }
  }

  private async *listAppServiceCertificateOrderDetectorResponsePagingAll(
    resourceGroupName: string,
    certificateOrderName: string,
    options?: CertificateOrdersDiagnosticsListAppServiceCertificateOrderDetectorResponseOptionalParams
  ): AsyncIterableIterator<DetectorResponse> {
    for await (const page of this.listAppServiceCertificateOrderDetectorResponsePagingPage(
      resourceGroupName,
      certificateOrderName,
      options
    )) {
      yield* page;
    }
  }

  /**
   * Description for Microsoft.CertificateRegistration to get the list of detectors for this RP.
   * @param resourceGroupName Name of the resource group to which the resource belongs.
   * @param certificateOrderName The certificate order name for which the response is needed.
   * @param options The options parameters.
   */
  private _listAppServiceCertificateOrderDetectorResponse(
    resourceGroupName: string,
    certificateOrderName: string,
    options?: CertificateOrdersDiagnosticsListAppServiceCertificateOrderDetectorResponseOptionalParams
  ): Promise<
    CertificateOrdersDiagnosticsListAppServiceCertificateOrderDetectorResponseResponse
  > {
    return this.client.sendOperationRequest(
      { resourceGroupName, certificateOrderName, options },
      listAppServiceCertificateOrderDetectorResponseOperationSpec
    );
  }

  /**
   * Description for Microsoft.CertificateRegistration call to get a detector response from App Lens.
   * @param resourceGroupName Name of the resource group to which the resource belongs.
   * @param certificateOrderName The certificate order name for which the response is needed.
   * @param detectorName The detector name which needs to be run.
   * @param options The options parameters.
   */
  getAppServiceCertificateOrderDetectorResponse(
    resourceGroupName: string,
    certificateOrderName: string,
    detectorName: string,
    options?: CertificateOrdersDiagnosticsGetAppServiceCertificateOrderDetectorResponseOptionalParams
  ): Promise<
    CertificateOrdersDiagnosticsGetAppServiceCertificateOrderDetectorResponseResponse
  > {
    return this.client.sendOperationRequest(
      { resourceGroupName, certificateOrderName, detectorName, options },
      getAppServiceCertificateOrderDetectorResponseOperationSpec
    );
  }

  /**
   * ListAppServiceCertificateOrderDetectorResponseNext
   * @param resourceGroupName Name of the resource group to which the resource belongs.
   * @param certificateOrderName The certificate order name for which the response is needed.
   * @param nextLink The nextLink from the previous successful call to the
   *                 ListAppServiceCertificateOrderDetectorResponse method.
   * @param options The options parameters.
   */
  private _listAppServiceCertificateOrderDetectorResponseNext(
    resourceGroupName: string,
    certificateOrderName: string,
    nextLink: string,
    options?: CertificateOrdersDiagnosticsListAppServiceCertificateOrderDetectorResponseNextOptionalParams
  ): Promise<
    CertificateOrdersDiagnosticsListAppServiceCertificateOrderDetectorResponseNextResponse
  > {
    return this.client.sendOperationRequest(
      { resourceGroupName, certificateOrderName, nextLink, options },
      listAppServiceCertificateOrderDetectorResponseNextOperationSpec
    );
  }
}
// Operation Specifications
const serializer = coreClient.createSerializer(Mappers, /* isXml */ false);

const listAppServiceCertificateOrderDetectorResponseOperationSpec: coreClient.OperationSpec = {
  path:
    "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.CertificateRegistration/certificateOrders/{certificateOrderName}/detectors",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper: Mappers.DetectorResponseCollection
    },
    default: {
      bodyMapper: Mappers.DefaultErrorResponse
    }
  },
  queryParameters: [Parameters.apiVersion],
  urlParameters: [
    Parameters.$host,
    Parameters.subscriptionId,
    Parameters.resourceGroupName,
    Parameters.certificateOrderName
  ],
  headerParameters: [Parameters.accept],
  serializer
};
const getAppServiceCertificateOrderDetectorResponseOperationSpec: coreClient.OperationSpec = {
  path:
    "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.CertificateRegistration/certificateOrders/{certificateOrderName}/detectors/{detectorName}",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper: Mappers.DetectorResponse
    },
    default: {
      bodyMapper: Mappers.DefaultErrorResponse
    }
  },
  queryParameters: [
    Parameters.apiVersion,
    Parameters.startTime,
    Parameters.endTime,
    Parameters.timeGrain
  ],
  urlParameters: [
    Parameters.$host,
    Parameters.subscriptionId,
    Parameters.resourceGroupName,
    Parameters.certificateOrderName,
    Parameters.detectorName
  ],
  headerParameters: [Parameters.accept],
  serializer
};
const listAppServiceCertificateOrderDetectorResponseNextOperationSpec: coreClient.OperationSpec = {
  path: "{nextLink}",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper: Mappers.DetectorResponseCollection
    },
    default: {
      bodyMapper: Mappers.DefaultErrorResponse
    }
  },
  urlParameters: [
    Parameters.$host,
    Parameters.subscriptionId,
    Parameters.resourceGroupName,
    Parameters.certificateOrderName,
    Parameters.nextLink
  ],
  headerParameters: [Parameters.accept],
  serializer
};
