// runtime/subsystems/events/event-keyset-cursor.ts
var DEFAULT_EVENT_LIST_LIMIT = 50;
var MAX_EVENT_LIST_LIMIT = 200;
function clampEventLimit(limit) {
  if (typeof limit !== "number" || !Number.isFinite(limit)) {
    return DEFAULT_EVENT_LIST_LIMIT;
  }
  const floored = Math.floor(limit);
  if (floored < 1) return 1;
  if (floored > MAX_EVENT_LIST_LIMIT) return MAX_EVENT_LIST_LIMIT;
  return floored;
}
function encodeEventCursor(keyset) {
  const tuple = [keyset.occurredAt.toISOString(), keyset.id];
  return Buffer.from(JSON.stringify(tuple), "utf8").toString("base64url");
}
function decodeEventCursor(cursor) {
  try {
    const json = Buffer.from(cursor, "base64url").toString("utf8");
    const parsed = JSON.parse(json);
    if (!Array.isArray(parsed) || parsed.length !== 2) return null;
    const [iso, id] = parsed;
    if (typeof iso !== "string" || typeof id !== "string") return null;
    const occurredAt = new Date(iso);
    if (Number.isNaN(occurredAt.getTime())) return null;
    return { occurredAt, id };
  } catch {
    return null;
  }
}

export {
  DEFAULT_EVENT_LIST_LIMIT,
  MAX_EVENT_LIST_LIMIT,
  clampEventLimit,
  encodeEventCursor,
  decodeEventCursor
};
//# sourceMappingURL=chunk-UQ5EHOH2.js.map