import { execSync, spawn, spawnSync } from 'child_process';
import { askYesNo } from './ask.js';
import { closeSync, existsSync, openSync, readFileSync, writeFileSync } from 'fs';
import { BRIDGE_LOG_FILE, BRIDGE_PID_FILE } from './paths.js';
import { resolveBridgeDir } from './bridge-dir.js';
import { ensureBridgeBuilt } from './bridge-build.js';
import { t } from './i18n.js';
export function isBridgeRunning() {
    if (!existsSync(BRIDGE_PID_FILE))
        return false;
    const pid = parseInt(readFileSync(BRIDGE_PID_FILE, 'utf-8').trim(), 10);
    if (!Number.isFinite(pid))
        return false;
    try {
        process.kill(pid, 0);
        return true;
    }
    catch {
        return false;
    }
}
export function stopBridge() {
    if (!existsSync(BRIDGE_PID_FILE))
        return;
    const pid = parseInt(readFileSync(BRIDGE_PID_FILE, 'utf-8').trim(), 10);
    try {
        process.kill(pid, 'SIGTERM');
    }
    catch {
        /* already dead */
    }
    try {
        writeFileSync(BRIDGE_PID_FILE, '');
    }
    catch {
        /* ignore */
    }
}
function killProcessesOnBridgePort() {
    try {
        execSync('lsof -t -i:3847 2>/dev/null | xargs kill -TERM 2>/dev/null', {
            stdio: 'ignore',
            shell: '/bin/bash',
        });
        /* give old bridge time to exit */
        execSync('sleep 0.5', { stdio: 'ignore', shell: '/bin/bash' });
    }
    catch {
        /* port free */
    }
}
export function startBridge(env) {
    stopBridge();
    killProcessesOnBridgePort();
    const bridgeDir = resolveBridgeDir();
    ensureBridgeBuilt(bridgeDir);
    if (!process.env.CURSORCONNECT_BRIDGE_DIR?.trim()) {
        process.env.CURSORCONNECT_BRIDGE_DIR = bridgeDir;
    }
    const logFd = openLogAppend();
    const child = spawn('node', ['dist/index.js'], {
        cwd: bridgeDir,
        env: { ...process.env, ...env },
        detached: true,
        stdio: ['ignore', logFd, logFd],
    });
    child.unref();
    try {
        closeSync(logFd);
    }
    catch {
        /* ignore */
    }
    writeFileSync(BRIDGE_PID_FILE, String(child.pid));
    return child;
}
function openLogAppend() {
    return openSync(BRIDGE_LOG_FILE, 'a');
}
export async function waitBridgeHealth(maxMs = 30_000, opts) {
    const requireCdp = opts?.requireCdp === true;
    const deadline = Date.now() + maxMs;
    while (Date.now() < deadline) {
        try {
            const res = await fetch('http://127.0.0.1:3847/health');
            if (res.ok) {
                const j = (await res.json());
                const ok = Boolean(j.ok);
                const cdp = Boolean(j.cdp);
                if (ok && (!requireCdp || cdp))
                    return { ok, cdp };
            }
        }
        catch {
            /* retry */
        }
        await sleep(500);
    }
    try {
        const res = await fetch('http://127.0.0.1:3847/health');
        if (res.ok) {
            const j = (await res.json());
            return { ok: Boolean(j.ok), cdp: Boolean(j.cdp) };
        }
    }
    catch {
        /* ignore */
    }
    return { ok: false, cdp: false };
}
export async function waitRelayConnector(relayUrl, maxMs = 45_000) {
    const base = relayUrl.replace(/\/$/, '');
    const deadline = Date.now() + maxMs;
    while (Date.now() < deadline) {
        try {
            const res = await fetch(`${base}/health`);
            if (res.ok) {
                const h = (await res.json());
                if (h.connector)
                    return true;
            }
        }
        catch {
            /* retry */
        }
        await sleep(800);
    }
    return false;
}
/** Mac в relay-комнате, код на сервере, токен зарегистрирован (иначе app получит 404). */
export async function waitRelayPairingReady(relayUrl, roomId, maxMs = 45_000, clientToken) {
    const base = relayUrl.replace(/\/$/, '');
    const deadline = Date.now() + maxMs;
    let lastConnector = false;
    let lastCodes = 0;
    let lastTokens = 0;
    let lastTokenRegistered = false;
    while (Date.now() < deadline) {
        try {
            const q = new URLSearchParams({ roomId });
            if (clientToken)
                q.set('token', clientToken);
            const res = await fetch(`${base}/api/diagnostics?${q}`, {
                signal: AbortSignal.timeout(8_000),
            });
            if (res.ok) {
                const d = (await res.json());
                lastConnector = Boolean(d.connectorInRoom);
                lastCodes = d.pairingCodesActive ?? 0;
                lastTokens = d.tokensRegistered ?? 0;
                lastTokenRegistered = d.tokenRegistered === true;
                const tokenOk = clientToken ? lastTokenRegistered : lastTokens > 0;
                if (lastConnector && lastCodes > 0 && tokenOk) {
                    return {
                        ok: true,
                        connectorInRoom: true,
                        pairingCodesActive: lastCodes,
                        tokensRegistered: lastTokens,
                        tokenRegistered: lastTokenRegistered,
                    };
                }
            }
        }
        catch {
            /* retry */
        }
        await sleep(800);
    }
    return {
        ok: false,
        connectorInRoom: lastConnector,
        pairingCodesActive: lastCodes,
        tokensRegistered: lastTokens,
        tokenRegistered: lastTokenRegistered,
    };
}
export function isCursorRunning() {
    try {
        execSync('pgrep -x Cursor', { stdio: 'ignore' });
        return true;
    }
    catch {
        return false;
    }
}
export async function quitCursor(maxWaitMs = 20_000) {
    if (!isCursorRunning())
        return true;
    console.log(t('cdp.quitCursor'));
    spawnSync('osascript', ['-e', 'tell application "Cursor" to quit'], {
        stdio: 'ignore',
    });
    const deadline = Date.now() + maxWaitMs;
    while (Date.now() < deadline) {
        await sleep(400);
        if (!isCursorRunning())
            return true;
    }
    console.log(t('cdp.forceQuitCursor'));
    try {
        spawnSync('killall', ['Cursor'], { stdio: 'ignore' });
    }
    catch {
        /* ignore */
    }
    await sleep(800);
    return !isCursorRunning();
}
const CDP_LAUNCH_CMD = 'open -a Cursor --args --remote-debugging-port=9222';
export function printManualCdpInstructions() {
    console.log(`\n${t('cdp.manualTitle')}`);
    console.log(`  ${CDP_LAUNCH_CMD}`);
    console.log(`  ${t('cdp.manualQuitFirst')}\n`);
}
export function launchCursorWithCdp() {
    spawn('open', ['-a', 'Cursor', '--args', '--remote-debugging-port=9222'], {
        detached: true,
        stdio: 'ignore',
    }).unref();
}
async function waitCdp(cdpUrl, attempts = 40) {
    for (let i = 0; i < attempts; i++) {
        if (await cdpReachable(cdpUrl))
            return true;
        await sleep(500);
    }
    return false;
}
export async function ensureCursorCdp(opts = {}) {
    const cdpUrl = opts.cdpUrl ?? 'http://127.0.0.1:9222';
    if (await cdpReachable(cdpUrl)) {
        return true;
    }
    const running = isCursorRunning();
    if (!running) {
        console.log(t('cdp.notRunningLaunch'));
        launchCursorWithCdp();
        if (await waitCdp(cdpUrl, 50))
            return true;
        printManualCdpInstructions();
        return false;
    }
    // Cursor запущен, порт 9222 не отвечает — обычный запуск без --remote-debugging-port
    let shouldRestart = opts.noRestartCursor !== true;
    if (!shouldRestart) {
        shouldRestart = Boolean(opts.restartCursor);
        if (!shouldRestart && process.stdin.isTTY) {
            shouldRestart = await askYesNo(t('cdp.askRestart'));
        }
    }
    if (!shouldRestart) {
        printManualCdpInstructions();
        return false;
    }
    console.log(t('cdp.restartingWithCdp'));
    const quitOk = await quitCursor();
    if (!quitOk) {
        console.warn(t('cdp.quitFailed'));
        printManualCdpInstructions();
        return false;
    }
    launchCursorWithCdp();
    if (await waitCdp(cdpUrl, 50))
        return true;
    printManualCdpInstructions();
    return false;
}
async function cdpReachable(cdpUrl) {
    try {
        const res = await fetch(`${cdpUrl.replace(/\/$/, '')}/json/version`);
        return res.ok;
    }
    catch {
        return false;
    }
}
function sleep(ms) {
    return new Promise((r) => setTimeout(r, ms));
}
