import { getTerminalAdvertisedPort } from './terminal-url.js';
import { getBotBrand } from '../bot-registry.js';
import { chatAppLink } from '../im/lark/lark-hosts.js';
import { getSessionTokenUsage } from './cost-calculator.js';
export function feishuChatLink(chatId, brand = 'feishu') {
    return chatAppLink(chatId, brand);
}
let cachedBotName = '';
export function setBotName(name) { cachedBotName = name; }
export function getBotName() { return cachedBotName; }
function parseSessionTime(iso) {
    if (!iso)
        return undefined;
    const ms = Date.parse(iso);
    return Number.isFinite(ms) ? ms : undefined;
}
function sessionCreatedAtMs(s) {
    return parseSessionTime(s.createdAt) ?? 0;
}
export function sessionLastActivityAtMs(s) {
    return parseSessionTime(s.lastMessageAt) ?? sessionCreatedAtMs(s);
}
function sessionTokenUsage(s, workingDir) {
    return getSessionTokenUsage({
        cliId: s.cliId ?? 'unknown',
        sessionId: s.sessionId,
        cliSessionId: s.cliSessionId,
        cwd: workingDir ?? s.workingDir,
    });
}
export function composeRowFromActive(ds) {
    return {
        sessionId: ds.session.sessionId,
        larkAppId: ds.larkAppId,
        botName: cachedBotName,
        cliId: ds.session.cliId ?? 'unknown',
        status: ds.lastScreenStatus ?? 'starting',
        adopt: !!ds.adoptedFrom,
        spawnedAt: sessionCreatedAtMs(ds.session) || ds.spawnedAt,
        lastMessageAt: sessionLastActivityAtMs(ds.session) || ds.lastMessageAt,
        workingDir: ds.workingDir,
        chatId: ds.chatId,
        rootMessageId: ds.session.rootMessageId,
        scope: ds.session.scope,
        title: ds.session.title,
        kanbanColumn: ds.session.kanbanColumn,
        kanbanPosition: ds.session.kanbanPosition,
        // Read from the persisted Session — single source of truth.
        // ds.ownerOpenId is a parallel in-memory copy that gets cleared on
        // restoreActiveSessions (which builds a fresh DaemonSession from disk
        // without copying this field). Reading session.ownerOpenId works for
        // both fresh and restored sessions.
        ownerOpenId: ds.session.ownerOpenId,
        webPort: ds.workerPort ?? null,
        proxyPort: getTerminalAdvertisedPort() || undefined,
        cliVersion: ds.cliVersion,
        hasHistory: ds.hasHistory,
        feishuChatLink: feishuChatLink(ds.chatId, getBotBrand(ds.larkAppId)),
        pendingRepo: !!ds.pendingRepo,
        tuiPromptActive: !!ds.tuiPromptCardId,
        agentAttention: ds.agentAttention
            ? { kind: ds.agentAttention.kind, reason: ds.agentAttention.reason, at: ds.agentAttention.at }
            : undefined,
        tokenUsage: sessionTokenUsage(ds.session, ds.workingDir),
    };
}
export function composeRowFromClosed(s) {
    return {
        sessionId: s.sessionId,
        larkAppId: s.larkAppId ?? '',
        botName: cachedBotName,
        cliId: s.cliId ?? 'unknown',
        status: 'closed',
        adopt: !!s.adoptedFrom,
        spawnedAt: sessionCreatedAtMs(s),
        lastMessageAt: s.closedAt ? (parseSessionTime(s.closedAt) ?? sessionLastActivityAtMs(s)) : sessionLastActivityAtMs(s),
        closedAt: s.closedAt ? Date.parse(s.closedAt) : undefined,
        workingDir: s.workingDir,
        chatId: s.chatId,
        rootMessageId: s.rootMessageId,
        scope: s.scope,
        title: s.title,
        kanbanColumn: s.kanbanColumn,
        kanbanPosition: s.kanbanPosition,
        ownerOpenId: s.ownerOpenId,
        webPort: s.webPort ?? null,
        feishuChatLink: feishuChatLink(s.chatId, getBotBrand(s.larkAppId ?? '')),
        tokenUsage: sessionTokenUsage(s),
    };
}
//# sourceMappingURL=dashboard-rows.js.map