import { ApiError } from "@api-client/core/build/browser.js";
/**
 * A class that serializes and deserializes the pagination cursor.
 */
export class Cursor {
    /**
     * Reads the cursor received from the client by decrypting its contents.
     * It throws an error when the cursor is invalid
     */
    async decodeCursor(cursor) {
        let result;
        try {
            const decoded = window.atob(cursor);
            result = JSON.parse(decoded);
        }
        catch (e) {
            throw new ApiError(`Invalid page cursor.`, 400);
        }
        return result;
    }
    /**
     * Encodes the state into a cursor.
     * @param state The list state.
     * @returns The encoded (base64) cursor.
     */
    async encodeCursor(state) {
        const str = JSON.stringify(state);
        return window.btoa(str);
    }
}
//# sourceMappingURL=Cursor.js.map