import { normalizeAgentTitle, resolveComposerUuidFromPartial, } from './agent-title-match.js';
import { windowKindFromTitle } from './cursor-window-kind.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;
function resolveTabComposerId(tab, state) {
    const raw = tab.composerId || tab.id;
    if (UUID_RE.test(raw))
        return raw;
    const fromTitle = state.composerIdByTitle?.[normalizeAgentTitle(tab.title)];
    if (fromTitle && UUID_RE.test(fromTitle))
        return fromTitle;
    const fromPartial = resolveComposerUuidFromPartial(raw, {
        composerIdByTitle: state.composerIdByTitle,
    });
    if (fromPartial)
        return fromPartial;
    return raw;
}
export function resolveCursorListMode(state) {
    const windows = state.windows ?? [];
    const active = windows.find((w) => w.id === state.activeWindowId);
    const activeKind = active?.kind ?? (active ? windowKindFromTitle(active.title) : undefined);
    if (activeKind === 'editor')
        return 'editor';
    if (activeKind === 'agents')
        return 'agents-window';
    const hasEditor = windows.some((w) => (w.kind ?? windowKindFromTitle(w.title)) === 'editor');
    if (hasEditor && !windows.some((w) => (w.kind ?? windowKindFromTitle(w.title)) === 'agents')) {
        return 'editor';
    }
    return 'agents-window';
}
const EDITOR_SKIP_TAB = /^(new agent|marketplace)$/i;
function tabToAgent(tab, winId, state) {
    const isHome = winId === state.activeWindowId;
    const composerId = resolveTabComposerId(tab, state);
    let isWorking = tab.isWorking === true;
    // Home window only: DOM may set isWorking on active tab after detectAgentWorking.
    if (!isWorking && tab.active && isHome && state.agentWorking === true) {
        isWorking = true;
    }
    return {
        id: composerId,
        title: tab.title,
        updatedAt: Date.now(),
        messageCount: 0,
        isWorking,
        hasUnread: tab.hasUnread,
        needsAttention: tab.needsAttention,
    };
}
/** Editor layout: one Cursor editor window → repo folder; auxiliary-bar tabs → chats. */
export function buildEditorRepos(state) {
    const editorWindows = (state.windows ?? []).filter((w) => (w.kind ?? windowKindFromTitle(w.title)) === 'editor');
    if (!editorWindows.length)
        return [];
    const snapById = new Map((state.windowSnapshots ?? []).map((s) => [s.windowId, s]));
    const homeId = state.activeWindowId;
    return editorWindows.map((win) => {
        const isHome = win.id === homeId;
        const tabs = (isHome ? state.tabs ?? [] : snapById.get(win.id)?.tabs ?? []).filter((t) => t.title?.trim() && !EDITOR_SKIP_TAB.test(t.title.trim()));
        return {
            id: win.id,
            name: win.title,
            path: '',
            agents: tabs.map((tab) => tabToAgent(tab, win.id, state)),
        };
    });
}
export function computeEditorListPatch(state) {
    const editorRepos = buildEditorRepos(state);
    const cursorListMode = resolveCursorListMode(state);
    const prevRepos = state.editorRepos ?? [];
    const prevMode = state.cursorListMode;
    if (prevMode === cursorListMode &&
        JSON.stringify(prevRepos) === JSON.stringify(editorRepos)) {
        return null;
    }
    return { editorRepos, cursorListMode };
}
