import { AsyncIterableExt } from "./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 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;
  };
}
export {
  toIterablePaginationResult
};
//# sourceMappingURL=pagination.js.map