"use strict";
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 pagination_exports = {};
__export(pagination_exports, {
  toIterablePaginationResult: () => toIterablePaginationResult
});
module.exports = __toCommonJS(pagination_exports);
var import_async_iterable = require("./async-iterable");
function toIterablePaginationResult(fn) {
  return (options) => {
    let __promise;
    function getPromise() {
      if (__promise)
        return __promise;
      __promise = fn(options ?? {});
      return __promise;
    }
    async function* generator() {
      let hasMore = true;
      let after = void 0;
      while (hasMore) {
        const pages = await fn({ ...options, after });
        const { data, hasMore: hasMoreNext } = pages;
        hasMore = hasMoreNext;
        after = data.at(-1)?.id;
        for (const item of data) {
          yield item;
        }
      }
    }
    const result = new import_async_iterable.AsyncIterableExt({
      [Symbol.asyncIterator]: generator
    });
    result.then = (onfulfilled, onrejected) => getPromise().then(onfulfilled, onrejected);
    result.catch = (onrejected) => getPromise().catch(onrejected);
    result.finally = (onfinally) => getPromise().finally(onfinally);
    return result;
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  toIterablePaginationResult
});
//# sourceMappingURL=pagination.js.map