import { Timestamp } from 'firebase-admin/firestore';

export const encodeCursor = (
  announcement: boolean,
  created: Timestamp,
  docId: string,
) =>
  Buffer.from(
    JSON.stringify([announcement, created.toMillis(), docId]),
  ).toString('base64');
export const decodeCursor = (token: string): [boolean, Timestamp, string] => {
  try {
    const [a, c, id] = JSON.parse(
      Buffer.from(token, 'base64').toString('utf8'),
    );
    return [!!a, Timestamp.fromMillis(c), id];
  } catch {
    throw new Error('Bad pageToken');
  }
};
