import { isPassiveBackgroundShellState } from './message-filter.js';
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
export function normalizeAgentTitle(title) {
    return title
        .trim()
        .replace(/\s+/g, ' ')
        .replace(/\d+\s*(?:s|m|h|d|w)\b/gi, '')
        .replace(/\d+[smhdw]$/i, '')
        .trim()
        .toLowerCase();
}
export function isComposerUuid(id) {
    return !!id && UUID_RE.test(id);
}
function lookupComposerIdByTitle(title, map) {
    if (!title?.trim() || !map)
        return undefined;
    return map[normalizeAgentTitle(title)];
}
export function resolveCursorActiveComposerId(state) {
    const activeTab = state.tabs.find((t) => t.active);
    if (activeTab?.composerId && isComposerUuid(activeTab.composerId)) {
        return activeTab.composerId;
    }
    if (isComposerUuid(state.activeComposerId))
        return state.activeComposerId;
    const fromTabTitle = lookupComposerIdByTitle(activeTab?.title, state.composerIdByTitle);
    if (fromTabTitle)
        return fromTabTitle;
    const fromHeader = lookupComposerIdByTitle(state.activeChatTitle, state.composerIdByTitle);
    if (fromHeader)
        return fromHeader;
    if (activeTab?.composerId && isComposerUuid(activeTab.composerId)) {
        return activeTab.composerId;
    }
    return activeTab?.composerId || activeTab?.id || state.activeComposerId;
}
/** Cursor is generating for this composer (active tab or sidebar spinner). */
export function isAgentGenerating(agentId, state) {
    if (!agentId)
        return false;
    const activeId = resolveCursorActiveComposerId(state);
    if (activeId === agentId && isPassiveBackgroundShellState(state))
        return false;
    if (activeId === agentId && state.agentWorking)
        return true;
    for (const tab of state.tabs) {
        const id = tab.composerId ?? tab.id;
        if (id !== agentId || !tab.isWorking)
            continue;
        if (activeId === agentId && isPassiveBackgroundShellState(state))
            continue;
        return true;
    }
    return false;
}
function isSyntheticRouteId(agentId) {
    return /^sidebar-\d+$/.test(agentId) || agentId.startsWith('title:');
}
/** Live DOM applies only when the subscribed app chat matches Cursor's active composer. */
export function isChatSyncedWithCursor(selectedAgentId, selectedTitle, state) {
    if (!selectedAgentId)
        return false;
    const cursorActiveId = resolveCursorActiveComposerId({
        connected: true,
        windows: [],
        tabs: state.tabs,
        messages: [],
        todos: [],
        queuedMessages: [],
        pendingApprovals: [],
        activeComposerId: state.activeComposerId,
        composerIdByTitle: state.composerIdByTitle,
        activeChatTitle: state.activeChatTitle,
        updatedAt: 0,
    });
    if (!cursorActiveId)
        return false;
    if (selectedAgentId === cursorActiveId)
        return true;
    const normSelected = selectedTitle ? normalizeAgentTitle(selectedTitle) : '';
    if (normSelected &&
        state.activeChatTitle &&
        normSelected === normalizeAgentTitle(state.activeChatTitle) &&
        isComposerUuid(cursorActiveId)) {
        return true;
    }
    const mappedFromTitle = lookupComposerIdByTitle(selectedTitle, state.composerIdByTitle);
    if (mappedFromTitle &&
        mappedFromTitle === cursorActiveId &&
        (selectedAgentId === mappedFromTitle || isSyntheticRouteId(selectedAgentId))) {
        return true;
    }
    const tabForActive = state.tabs.find((t) => t.active ||
        t.composerId === cursorActiveId ||
        t.id === cursorActiveId);
    if (tabForActive && normSelected && isComposerUuid(cursorActiveId)) {
        if (normalizeAgentTitle(tabForActive.title) === normSelected)
            return true;
    }
    if (!tabForActive || !selectedTitle)
        return false;
    if (normalizeAgentTitle(tabForActive.title) !== normalizeAgentTitle(selectedTitle)) {
        return false;
    }
    const tabComposerId = tabForActive.composerId || tabForActive.id;
    if (isComposerUuid(selectedAgentId)) {
        return tabComposerId === selectedAgentId || cursorActiveId === selectedAgentId;
    }
    return false;
}
