/**
 * Focus an editor auxiliary-bar chat tab (VS Code workbench).
 * CDP callFunction entry — keep helpers inside.
 */
export function focusEditorAuxiliaryTab(opts) {
    const wantTitle = (opts.title ?? '').trim();
    const wantId = (opts.agentId ?? '').trim();
    const stripEditorSuffix = (s) => s.replace(/,\s*Chat Editors:\s*Editor Group\s*\d+\s*$/i, '').trim();
    const norm = (s) => stripEditorSuffix(s)
        .replace(/\s+/g, ' ')
        .replace(/\d+\s*(?:s|m|h|d|w)\b/gi, '')
        .replace(/\d+[smhdw]$/i, '')
        .trim()
        .toLowerCase();
    const titleNorm = wantTitle ? norm(wantTitle) : '';
    const wantLower = wantId.toLowerCase();
    const aux = document.querySelector('#workbench\\.parts\\.auxiliarybar');
    if (!aux || document.querySelector('[class*="agent-panel"]'))
        return false;
    function titleMatches(aria, visible) {
        if (!titleNorm)
            return false;
        const ariaNorm = aria ? norm(aria) : '';
        const visibleNorm = visible ? norm(visible) : '';
        const candidates = [ariaNorm, visibleNorm].filter(Boolean);
        for (const c of candidates) {
            if (c === titleNorm)
                return true;
            if (c.length >= 8 && titleNorm.length >= 8 && (c.includes(titleNorm) || titleNorm.includes(c))) {
                return true;
            }
        }
        return false;
    }
    function idMatches(resource) {
        if (wantLower.length < 8 || !resource)
            return false;
        const r = resource.toLowerCase();
        if (r === wantLower)
            return true;
        if (wantLower.length >= 32 && (r.startsWith(wantLower) || wantLower.startsWith(r)))
            return true;
        if (wantLower.length >= 8 && r.length >= 32 && r.startsWith(wantLower))
            return true;
        return false;
    }
    function activateTab(tab) {
        const el = tab;
        el.scrollIntoView({ block: 'nearest', behavior: 'instant' });
        const label = tab.querySelector('.label-name, .tab-label, [class*="label-name"]') || tab;
        const target = label ?? el;
        target.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, cancelable: true }));
        target.dispatchEvent(new MouseEvent('mouseup', { bubbles: true, cancelable: true }));
        target.click();
    }
    const tabs = aux.querySelectorAll('div.tab[role="tab"]');
    for (const tab of tabs) {
        const aria = tab.getAttribute('aria-label') || '';
        const visible = (tab.textContent || '').trim();
        const resource = tab.getAttribute('data-resource-name') || '';
        if (!titleMatches(aria, visible) && !idMatches(resource))
            continue;
        activateTab(tab);
        const active = aux.querySelector('div.tab[role="tab"].active, div.tab[role="tab"][aria-selected="true"]');
        if (!active)
            return true;
        const activeAria = active.getAttribute('aria-label') || '';
        const activeVisible = (active.textContent || '').trim();
        if (titleNorm && !titleMatches(activeAria, activeVisible)) {
            return false;
        }
        if (wantLower.length >= 32 && resource) {
            const activeRes = active.getAttribute('data-resource-name') || '';
            if (!idMatches(activeRes) && activeRes.toLowerCase() !== wantLower)
                return false;
        }
        return true;
    }
    return false;
}
