"use strict";
// ─── Stream message browsing ───────────────────────────────────────────────
//
// Paginated queries across engine_streams and worker_streams tables.
// Both tables are HASH-partitioned on stream_name (8 partitions each).
// Status is derived from timestamp columns:
//   dead_lettered_at IS NOT NULL → 'dead_lettered'
//   expired_at IS NOT NULL       → 'processed'
//   reserved_at IS NOT NULL      → 'claimed'
//   else                         → 'pending'
Object.defineProperty(exports, "__esModule", { value: true });
exports.VALID_SORT_ORDERS = exports.VALID_SORT_COLUMNS = void 0;
exports.LIST_STREAM_MESSAGES = LIST_STREAM_MESSAGES;
exports.COUNT_STREAM_MESSAGES = COUNT_STREAM_MESSAGES;
/** Allowed sort columns — whitelist prevents injection. */
exports.VALID_SORT_COLUMNS = {
    created_at: 'created_at',
    stream_name: 'stream_name',
    priority: 'priority',
    id: 'id',
};
/** Allowed sort directions. */
exports.VALID_SORT_ORDERS = new Set(['asc', 'desc']);
/**
 * Status derivation CASE expression, reused by both list and count queries.
 */
const STATUS_CASE = `
  CASE
    WHEN dead_lettered_at IS NOT NULL THEN 'dead_lettered'
    WHEN expired_at IS NOT NULL THEN 'processed'
    WHEN reserved_at IS NOT NULL THEN 'claimed'
    ELSE 'pending'
  END`;
/**
 * Shared WHERE clause for stream message filtering.
 *
 * Parameters:
 *   $1 = stream_name filter (NULL = all, otherwise ILIKE pattern)
 *   $2 = status filter (NULL = all, otherwise exact match)
 *   $3 = msg_type filter (NULL = all, worker-only)
 *   $4 = topic filter (NULL = all, worker-only)
 *   $5 = workflow_name filter (NULL = all, worker-only)
 *   $6 = jid filter (NULL = all, worker-only)
 *   $7 = aid filter (NULL = all, worker-only)
 */
/** Engine WHERE: supports stream_name, status, and jid (v0.18.0). */
const ENGINE_WHERE_CLAUSE = `
  WHERE ($1::text IS NULL OR stream_name ILIKE $1)
    AND ($2::text IS NULL OR ${STATUS_CASE} = $2)
    AND ($3::text IS NULL)
    AND ($4::text IS NULL)
    AND ($5::text IS NULL)
    AND ($6::text IS NULL OR jid = $6)
    AND ($7::text IS NULL)`;
const WORKER_WHERE_CLAUSE = `
  WHERE ($1::text IS NULL OR stream_name ILIKE $1)
    AND ($2::text IS NULL OR ${STATUS_CASE} = $2)
    AND ($3::text IS NULL OR msg_type = $3)
    AND ($4::text IS NULL OR topic = $4)
    AND ($5::text IS NULL OR workflow_name = $5)
    AND ($6::text IS NULL OR jid = $6)
    AND ($7::text IS NULL OR aid = $7)`;
/**
 * List stream messages with pagination, filtering, and sorting.
 *
 * Source is required — engine and worker streams are separate tables
 * with different schemas and must never be commingled in a single query.
 *
 * Parameters: $1 = stream_name, $2 = status, $3 = msg_type,
 *             $4 = topic, $5 = workflow_name, $6 = jid, $7 = aid,
 *             $8 = limit, $9 = offset
 */
function LIST_STREAM_MESSAGES(schema, sortColumn, sortOrder, source) {
    const select = source === 'engine'
        ? `SELECT
        id, 'engine' AS source, stream_name,
        message, ${STATUS_CASE} AS status,
        created_at, reserved_at, reserved_by, expired_at,
        dead_lettered_at, priority, visible_at,
        retry_attempt, max_retry_attempts,
        NULL AS workflow_name, jid, NULL AS aid,
        NULL AS dad, NULL AS msg_type, NULL AS topic
      FROM "${schema}".engine_streams
      ${ENGINE_WHERE_CLAUSE}`
        : `SELECT
        id, 'worker' AS source, stream_name,
        message, ${STATUS_CASE} AS status,
        created_at, reserved_at, reserved_by, expired_at,
        dead_lettered_at, priority, visible_at,
        retry_attempt, max_retry_attempts,
        workflow_name, jid, aid, dad, msg_type, topic
      FROM "${schema}".worker_streams
      ${WORKER_WHERE_CLAUSE}`;
    return `
    SELECT * FROM (${select}) AS q
    ORDER BY ${sortColumn} ${sortOrder}, id DESC
    LIMIT $8::int OFFSET $9::int`;
}
/**
 * Count stream messages matching the filter criteria.
 *
 * Parameters: $1 = stream_name, $2 = status, $3 = msg_type
 */
function COUNT_STREAM_MESSAGES(schema, source) {
    const table = source === 'engine' ? 'engine_streams' : 'worker_streams';
    const where = source === 'engine' ? ENGINE_WHERE_CLAUSE : WORKER_WHERE_CLAUSE;
    return `SELECT COUNT(*)::int AS count FROM "${schema}".${table} ${where}`;
}
