import type { Page } from './types';

/**
 * getNextPageParam factory for offset/total infinite queries that fetch
 * Page<T> envelopes (items + total + limit + offset) and stop when the
 * loaded count reaches the server-reported total.
 *
 * Use with TanStack svelte-query's createInfiniteQuery:
 *   getNextPageParam: pageGetNextPageParam<TrackRef>()
 *
 * NOTE: the spec for this task described a bare-array shape
 * (lastPage: T[], pageSize-based stop). No call site in this repo uses
 * that shape — every paged endpoint returns Page<T>. The helper matches
 * the actual canonical shape so the 8 verbatim copies can collapse.
 */
export function pageGetNextPageParam<T>() {
  return (last: Page<T>): number | undefined => {
    const loaded = last.offset + last.items.length;
    return loaded >= last.total ? undefined : loaded;
  };
}
