/*
 * 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 { GetUsagesInLocation } 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 {
  CsmUsageQuota,
  GetUsagesInLocationListNextOptionalParams,
  GetUsagesInLocationListOptionalParams,
  GetUsagesInLocationListResponse,
  GetUsagesInLocationListNextResponse
} from "../models";

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

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

  /**
   * List usages in cores for all skus used by a subscription in a given location, for a specific quota
   * type.
   * @param location The name of the Azure region.
   * @param options The options parameters.
   */
  public list(
    location: string,
    options?: GetUsagesInLocationListOptionalParams
  ): PagedAsyncIterableIterator<CsmUsageQuota> {
    const iter = this.listPagingAll(location, 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.listPagingPage(location, options, settings);
      }
    };
  }

  private async *listPagingPage(
    location: string,
    options?: GetUsagesInLocationListOptionalParams,
    settings?: PageSettings
  ): AsyncIterableIterator<CsmUsageQuota[]> {
    let result: GetUsagesInLocationListResponse;
    let continuationToken = settings?.continuationToken;
    if (!continuationToken) {
      result = await this._list(location, options);
      let page = result.value || [];
      continuationToken = result.nextLink;
      setContinuationToken(page, continuationToken);
      yield page;
    }
    while (continuationToken) {
      result = await this._listNext(location, continuationToken, options);
      continuationToken = result.nextLink;
      let page = result.value || [];
      setContinuationToken(page, continuationToken);
      yield page;
    }
  }

  private async *listPagingAll(
    location: string,
    options?: GetUsagesInLocationListOptionalParams
  ): AsyncIterableIterator<CsmUsageQuota> {
    for await (const page of this.listPagingPage(location, options)) {
      yield* page;
    }
  }

  /**
   * List usages in cores for all skus used by a subscription in a given location, for a specific quota
   * type.
   * @param location The name of the Azure region.
   * @param options The options parameters.
   */
  private _list(
    location: string,
    options?: GetUsagesInLocationListOptionalParams
  ): Promise<GetUsagesInLocationListResponse> {
    return this.client.sendOperationRequest(
      { location, options },
      listOperationSpec
    );
  }

  /**
   * ListNext
   * @param location The name of the Azure region.
   * @param nextLink The nextLink from the previous successful call to the List method.
   * @param options The options parameters.
   */
  private _listNext(
    location: string,
    nextLink: string,
    options?: GetUsagesInLocationListNextOptionalParams
  ): Promise<GetUsagesInLocationListNextResponse> {
    return this.client.sendOperationRequest(
      { location, nextLink, options },
      listNextOperationSpec
    );
  }
}
// Operation Specifications
const serializer = coreClient.createSerializer(Mappers, /* isXml */ false);

const listOperationSpec: coreClient.OperationSpec = {
  path:
    "/subscriptions/{subscriptionId}/providers/Microsoft.Web/locations/{location}/usages",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper: Mappers.CsmUsageQuotaCollection
    },
    default: {
      bodyMapper: Mappers.DefaultErrorResponse
    }
  },
  queryParameters: [Parameters.apiVersion],
  urlParameters: [
    Parameters.$host,
    Parameters.subscriptionId,
    Parameters.location1
  ],
  headerParameters: [Parameters.accept],
  serializer
};
const listNextOperationSpec: coreClient.OperationSpec = {
  path: "{nextLink}",
  httpMethod: "GET",
  responses: {
    200: {
      bodyMapper: Mappers.CsmUsageQuotaCollection
    },
    default: {
      bodyMapper: Mappers.DefaultErrorResponse
    }
  },
  urlParameters: [
    Parameters.$host,
    Parameters.subscriptionId,
    Parameters.nextLink,
    Parameters.location1
  ],
  headerParameters: [Parameters.accept],
  serializer
};
