/**
 * Find / click agent **generation** stop (not shell «Stop command»).
 * Single entry for CDP callFunction — helpers must live inside this function.
 */
export function generationStopDomAction(action) {
    function isVisibleButton(btn) {
        const st = getComputedStyle(btn);
        if (st.display === 'none' || st.visibility === 'hidden' || Number(st.opacity) < 0.05) {
            return false;
        }
        const r = btn.getBoundingClientRect();
        return r.width >= 2 && r.height >= 2;
    }
    function resolveStopClickTarget(el) {
        if (!el)
            return null;
        if (el.classList.contains('codicon-debug-stop')) {
            return el.closest('.anysphere-icon-button, button, [role="button"], a') || el;
        }
        return el;
    }
    function findGenerationStopButton() {
        const selectors = [
            'button.ui-prompt-input-submit-button[data-state="stop"]',
            'button[aria-label="Stop generation"]',
            'button.ui-prompt-input-submit-button[aria-label="Stop generation"]',
            'button[data-state="stop"]',
            '.composer-button-area .anysphere-icon-button:has(.codicon-debug-stop)',
            '.send-with-mode .anysphere-icon-button:has(.codicon-debug-stop)',
        ];
        const scopes = [
            document.querySelector('[class*="agent-panel"]'),
            document.querySelector('.composer-bar'),
            document.querySelector('[class*="composer-bar"]'),
            document,
        ].filter((el) => Boolean(el));
        for (const scope of scopes) {
            for (const sel of selectors) {
                try {
                    const hit = scope.querySelector(sel);
                    const btn = resolveStopClickTarget(hit);
                    if (!btn || !isVisibleButton(btn))
                        continue;
                    if (btn.closest('.ui-shell-tool-call'))
                        continue;
                    const aria = (btn.getAttribute('aria-label') || '').trim().toLowerCase();
                    if (aria === 'stop command')
                        continue;
                    return btn;
                }
                catch {
                    /* skip */
                }
            }
        }
        return null;
    }
    const btn = findGenerationStopButton();
    if (action === 'has')
        return !!btn;
    if (!btn)
        return false;
    const target = resolveStopClickTarget(btn) || btn;
    target.scrollIntoView({ block: 'center' });
    target.click();
    return true;
}
