"use strict";
/**
 * Session state database operations
 * CRUD operations for session_states table
 *
 * Issue #479: Extracted from db.ts for single-responsibility separation
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSessionState = getSessionState;
exports.updateSessionState = updateSessionState;
exports.setInProgressMessageId = setInProgressMessageId;
exports.clearInProgressMessageId = clearInProgressMessageId;
exports.deleteSessionState = deleteSessionState;
/**
 * Get session state for a worktree
 */
function getSessionState(db, worktreeId, cliToolId = 'claude') {
    const stmt = db.prepare(`
    SELECT worktree_id, cli_tool_id, last_captured_line, in_progress_message_id
    FROM session_states
    WHERE worktree_id = ? AND cli_tool_id = ?
  `);
    const row = stmt.get(worktreeId, cliToolId);
    if (!row) {
        return null;
    }
    return {
        worktreeId: row.worktree_id,
        cliToolId: row.cli_tool_id,
        lastCapturedLine: row.last_captured_line,
        inProgressMessageId: row.in_progress_message_id || null,
    };
}
/**
 * Update session state for a worktree
 */
function updateSessionState(db, worktreeId, cliToolId, lastCapturedLine) {
    const stmt = db.prepare(`
    INSERT INTO session_states (worktree_id, cli_tool_id, last_captured_line)
    VALUES (?, ?, ?)
    ON CONFLICT(worktree_id, cli_tool_id) DO UPDATE SET
      last_captured_line = excluded.last_captured_line
  `);
    stmt.run(worktreeId, cliToolId, lastCapturedLine);
}
/**
 * Set the in-progress message ID for a session
 */
function setInProgressMessageId(db, worktreeId, cliToolId, messageId) {
    const stmt = db.prepare(`
    INSERT INTO session_states (worktree_id, cli_tool_id, last_captured_line, in_progress_message_id)
    VALUES (?, ?, 0, ?)
    ON CONFLICT(worktree_id, cli_tool_id) DO UPDATE SET
      in_progress_message_id = excluded.in_progress_message_id
  `);
    stmt.run(worktreeId, cliToolId, messageId);
}
/**
 * Clear the in-progress message ID for a session
 */
function clearInProgressMessageId(db, worktreeId, cliToolId) {
    setInProgressMessageId(db, worktreeId, cliToolId, null);
}
/**
 * Delete session state for a worktree
 * Called when a session is killed or reset
 */
function deleteSessionState(db, worktreeId, cliToolId) {
    if (cliToolId) {
        const stmt = db.prepare(`
      DELETE FROM session_states
      WHERE worktree_id = ? AND cli_tool_id = ?
    `);
        stmt.run(worktreeId, cliToolId);
    }
    else {
        const stmt = db.prepare(`
      DELETE FROM session_states
      WHERE worktree_id = ?
    `);
        stmt.run(worktreeId);
    }
}
