export interface PageState {
  cursor: string | null;
  previous: Array<string | null>;
}

export function advancePage(page: PageState, nextCursor: string | null): PageState {
  if (!nextCursor) {
    return page;
  }
  return {
    cursor: nextCursor,
    previous: [...page.previous, page.cursor]
  };
}

export function previousPage(page: PageState): PageState {
  if (page.previous.length === 0) {
    return page;
  }
  return {
    cursor: page.previous[page.previous.length - 1] ?? null,
    previous: page.previous.slice(0, -1)
  };
}
