var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var pagingHelpers_exports = {};
__export(pagingHelpers_exports, {
  buildPagedAsyncIterator: () => buildPagedAsyncIterator
});
module.exports = __toCommonJS(pagingHelpers_exports);
var import_core_client = require("@azure-rest/core-client");
var import_core_rest_pipeline = require("@azure/core-rest-pipeline");
function buildPagedAsyncIterator(client, getInitialResponse, processResponseBody, expectedStatuses, options = {}) {
  const itemName = options.itemName ?? "value";
  const nextLinkName = options.nextLinkName ?? "nextLink";
  const pagedResult = {
    getPage: async (pageLink) => {
      const result = pageLink === void 0 ? await getInitialResponse() : await client.pathUnchecked(pageLink).get();
      checkPagingRequest(result, expectedStatuses);
      const results = await processResponseBody(result);
      const nextLink = getNextLink(results, nextLinkName);
      const values = getElements(results, itemName);
      return {
        page: values,
        nextPageLink: nextLink
      };
    },
    byPage: (settings) => {
      const { continuationToken } = settings ?? {};
      return getPageAsyncIterator(pagedResult, {
        pageLink: continuationToken
      });
    }
  };
  return getPagedAsyncIterator(pagedResult);
}
function getPagedAsyncIterator(pagedResult) {
  const iter = getItemAsyncIterator(pagedResult);
  return {
    next() {
      return iter.next();
    },
    [Symbol.asyncIterator]() {
      return this;
    },
    byPage: pagedResult?.byPage ?? ((settings) => {
      const { continuationToken } = settings ?? {};
      return getPageAsyncIterator(pagedResult, {
        pageLink: continuationToken
      });
    })
  };
}
async function* getItemAsyncIterator(pagedResult) {
  const pages = getPageAsyncIterator(pagedResult);
  for await (const page of pages) {
    yield* page;
  }
}
async function* getPageAsyncIterator(pagedResult, options = {}) {
  const { pageLink } = options;
  let response = await pagedResult.getPage(pageLink ?? pagedResult.firstPageLink);
  if (!response) {
    return;
  }
  let result = response.page;
  result.continuationToken = response.nextPageLink;
  yield result;
  while (response.nextPageLink) {
    response = await pagedResult.getPage(response.nextPageLink);
    if (!response) {
      return;
    }
    result = response.page;
    result.continuationToken = response.nextPageLink;
    yield result;
  }
}
function getNextLink(body, nextLinkName) {
  if (!nextLinkName) {
    return void 0;
  }
  const nextLink = body[nextLinkName];
  if (typeof nextLink !== "string" && typeof nextLink !== "undefined" && nextLink !== null) {
    throw new import_core_rest_pipeline.RestError(
      `Body Property ${nextLinkName} should be a string or undefined or null but got ${typeof nextLink}`
    );
  }
  if (nextLink === null) {
    return void 0;
  }
  return nextLink;
}
function getElements(body, itemName) {
  const value = body[itemName];
  if (!Array.isArray(value)) {
    throw new import_core_rest_pipeline.RestError(
      `Couldn't paginate response
 Body doesn't contain an array property with name: ${itemName}`
    );
  }
  return value ?? [];
}
function checkPagingRequest(response, expectedStatuses) {
  if (!expectedStatuses.includes(response.status)) {
    throw (0, import_core_client.createRestError)(
      `Pagination failed with unexpected statusCode ${response.status}`,
      response
    );
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  buildPagedAsyncIterator
});
//# sourceMappingURL=pagingHelpers.js.map
