"use strict";
/**
 * Sidebar Type Definitions
 *
 * Types for sidebar components and branch status display
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.deriveCliStatus = deriveCliStatus;
exports.calculateHasUnread = calculateHasUnread;
exports.toBranchItem = toBranchItem;
const selected_agents_validator_1 = require("../lib/selected-agents-validator");
/**
 * Derive BranchStatus from per-CLI tool session status flags.
 * Shared by sidebar (toBranchItem) and WorktreeDetailRefactored tab dots.
 */
function deriveCliStatus(toolStatus) {
    if (!toolStatus)
        return 'idle';
    if (toolStatus.isWaitingForResponse)
        return 'waiting';
    if (toolStatus.isProcessing)
        return 'running';
    if (toolStatus.isRunning)
        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) {
    // Issue #4: Sidebar no longer shows per-CLI session status.
    // Status is always 'idle' since detailed status is shown in WorktreeDetail.
    const status = 'idle';
    // Use new hasUnread logic based on lastAssistantMessageAt and lastViewedAt
    const hasUnread = calculateHasUnread(worktree);
    // Issue #368: Use selectedAgents to determine which tools to show status for
    // Falls back to DEFAULT_SELECTED_AGENTS when selectedAgents is not set
    const agents = worktree.selectedAgents ?? selected_agents_validator_1.DEFAULT_SELECTED_AGENTS;
    const cliStatus = {};
    for (const agent of agents) {
        cliStatus[agent] = deriveCliStatus(worktree.sessionStatusByCli?.[agent]);
    }
    return {
        id: worktree.id,
        name: worktree.name,
        repositoryName: worktree.repositoryName,
        status,
        hasUnread,
        lastActivity: worktree.updatedAt,
        description: worktree.description,
        cliStatus,
    };
}
