"use strict";
/**
 * Sidebar Type Definitions
 *
 * Types for sidebar components and branch status display
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateHasUnread = calculateHasUnread;
exports.toBranchItem = toBranchItem;
/**
 * Determine branch status from Worktree data
 *
 * Status priority:
 * - waiting: Claude asked a yes/no prompt, waiting for user's answer (green dot)
 * - running: Claude is actively processing user's request (spinner)
 * - ready: Session running, waiting for user's new message (green dot)
 * - idle: Session not running (gray dot)
 */
function determineBranchStatus(worktree) {
    // Check CLI-specific status first
    const claudeStatus = worktree.sessionStatusByCli?.claude;
    if (claudeStatus) {
        if (claudeStatus.isWaitingForResponse) {
            return 'waiting';
        }
        if (claudeStatus.isProcessing) {
            return 'running';
        }
        // Session running but not processing = ready (waiting for user to type new message)
        if (claudeStatus.isRunning) {
            return 'ready';
        }
    }
    // Fall back to legacy status fields
    if (worktree.isWaitingForResponse) {
        return 'waiting';
    }
    if (worktree.isProcessing) {
        return 'running';
    }
    // Session running but not processing = ready
    if (worktree.isSessionRunning) {
        return 'ready';
    }
    return 'idle';
}
/**
 * Calculate whether a worktree has unread messages
 *
 * hasUnread is true when:
 * - There is at least one assistant message (lastAssistantMessageAt exists)
 * - AND the user has never viewed this worktree (lastViewedAt is null)
 *   OR the last assistant message is newer than the last view
 *
 * @param worktree - Source worktree data
 * @returns true if there are unread messages
 */
function calculateHasUnread(worktree) {
    // No assistant messages = no unread
    if (!worktree.lastAssistantMessageAt) {
        return false;
    }
    // Never viewed but has assistant message = unread
    if (!worktree.lastViewedAt) {
        return true;
    }
    // Compare timestamps: unread if assistant message is newer than last view
    return new Date(worktree.lastAssistantMessageAt) > new Date(worktree.lastViewedAt);
}
/**
 * Convert Worktree to SidebarBranchItem for display
 *
 * @param worktree - Source worktree data
 * @returns SidebarBranchItem for sidebar display
 */
function toBranchItem(worktree) {
    const status = determineBranchStatus(worktree);
    // Use new hasUnread logic based on lastAssistantMessageAt and lastViewedAt
    const hasUnread = calculateHasUnread(worktree);
    return {
        id: worktree.id,
        name: worktree.name,
        repositoryName: worktree.repositoryName,
        status,
        hasUnread,
        lastActivity: worktree.updatedAt,
        description: worktree.description,
    };
}
