/**
 * Active Agents sidebar chat / composer id after `new_chat` (CDP callFunction).
 */
export function readActiveChatDom() {
    function cleanTitle(raw) {
        let t = raw.replace(/\s+/g, ' ').trim();
        // VS Code editor aux tab aria-label: "New Agent, Chat Editors: Editor Group 1"
        t = t.replace(/,\s*Chat Editors:\s*Editor Group\s*\d+\s*$/i, '').trim();
        t = t.replace(/[.…\u2026]+$/g, '').trim();
        return t;
    }
    function pickAuxiliaryTabTitle(tab) {
        const labelEl = tab.querySelector('.label-name, .tab-label, [class*="label-name"]');
        const visible = cleanTitle(labelEl?.textContent || tab.textContent || '');
        const aria = cleanTitle(tab.getAttribute('aria-label') || '');
        if (visible && aria) {
            if (visible.length <= aria.length || aria.includes(visible))
                return visible;
            return aria;
        }
        return visible || aria;
    }
    const input = document.querySelector(".composer-bar [contenteditable='true'], [contenteditable='true']");
    const composerHost = input?.closest('[data-composer-id]') ||
        input?.closest('div.composer-bar[data-composer-id], .composer-bar[data-composer-id]');
    const composerBar = document.querySelector('div.composer-bar[data-composer-id]') ||
        document.querySelector('.composer-bar[data-composer-id]');
    const barId = composerHost?.getAttribute('data-composer-id')?.trim() ||
        composerBar?.getAttribute('data-composer-id')?.trim() ||
        '';
    for (const btn of document.querySelectorAll('.glass-sidebar-agent-menu-btn, div.glass-sidebar-agent-menu-btn')) {
        const active = btn.getAttribute('data-active') === 'true' ||
            btn.classList.contains('active') ||
            btn.closest('li')?.getAttribute('aria-selected') === 'true';
        if (!active)
            continue;
        const title = cleanTitle(btn.textContent || '');
        const composerId = btn.getAttribute('data-composer-id') ||
            btn.closest('li')?.getAttribute('data-composer-id') ||
            btn.closest('[data-composer-id]')?.getAttribute('data-composer-id') ||
            barId ||
            '';
        return {
            ...(composerId ? { composerId } : {}),
            ...(title ? { title } : {}),
        };
    }
    const activeTab = document.querySelector('#workbench\\.parts\\.auxiliarybar div.tab[role="tab"].active, #workbench\\.parts\\.auxiliarybar div.tab[role="tab"][aria-selected="true"]');
    if (activeTab) {
        const title = pickAuxiliaryTabTitle(activeTab);
        const composerId = activeTab.getAttribute('data-composer-id') ||
            activeTab.closest('[data-composer-id]')?.getAttribute('data-composer-id') ||
            barId ||
            '';
        return {
            ...(composerId ? { composerId } : {}),
            ...(title ? { title } : {}),
        };
    }
    if (barId) {
        const title = document.querySelector('span.auxiliary-bar-chat-title')?.textContent?.trim() ||
            document.querySelector('.auxiliary-bar-chat-title')?.textContent?.trim() ||
            '';
        return {
            composerId: barId,
            ...(title ? { title: cleanTitle(title) } : {}),
        };
    }
    return {};
}
/** Composer UUIDs on editor auxiliary-bar tabs (for `new_chat` before/after). */
export function readEditorAuxComposerIds() {
    const aux = document.querySelector('#workbench\\.parts\\.auxiliarybar');
    if (!aux)
        return [];
    const ids = [];
    for (const tab of aux.querySelectorAll('div.tab[role="tab"]')) {
        const r = tab.getAttribute('data-resource-name')?.trim() || '';
        if (r.length >= 32)
            ids.push(r);
    }
    return ids;
}
