// runtime/subsystems/jobs/job-run-keyset-cursor.ts
var DEFAULT_LIST_LIMIT = 50;
var MAX_LIST_LIMIT = 200;
function clampLimit(limit) {
  if (typeof limit !== "number" || !Number.isFinite(limit)) {
    return DEFAULT_LIST_LIMIT;
  }
  const floored = Math.floor(limit);
  if (floored < 1) return 1;
  if (floored > MAX_LIST_LIMIT) return MAX_LIST_LIMIT;
  return floored;
}
function encodeKeysetCursor(keyset) {
  const tuple = [keyset.createdAt.toISOString(), keyset.id];
  return Buffer.from(JSON.stringify(tuple), "utf8").toString("base64url");
}
function decodeKeysetCursor(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 createdAt = new Date(iso);
    if (Number.isNaN(createdAt.getTime())) return null;
    return { createdAt, id };
  } catch {
    return null;
  }
}
function toJobRunSummary(r) {
  return {
    runId: r.id,
    rootRunId: r.rootRunId,
    parentRunId: r.parentRunId,
    triggerSource: r.triggerSource,
    triggerRef: r.triggerRef,
    jobType: r.jobType,
    pool: r.pool,
    status: r.status,
    scopeEntityType: r.scopeEntityType,
    scopeEntityId: r.scopeEntityId,
    tenantId: r.tenantId,
    attempts: r.attempts,
    errorMessage: r.error?.message ?? null,
    runAt: r.runAt,
    startedAt: r.startedAt,
    finishedAt: r.finishedAt,
    createdAt: r.createdAt
  };
}

export {
  DEFAULT_LIST_LIMIT,
  MAX_LIST_LIMIT,
  clampLimit,
  encodeKeysetCursor,
  decodeKeysetCursor,
  toJobRunSummary
};
//# sourceMappingURL=chunk-L3LZWWSX.js.map