import chalk from 'chalk';
import { createSpinner } from '../utils/logger.js';
import { APIClient } from '../utils/api.js';
import { getDesktopBridgeStatus } from '../utils/desktop-bridge-client.js';
function displayEndpoint(endpoint) {
    if (/^(?:wss?|https?):\/\/(?:127\.0\.0\.1|localhost)(?::\d+)?/i.test(endpoint)) {
        return 'local loopback endpoint';
    }
    return endpoint;
}
export class BridgeCommand {
    api;
    constructor(config, logger) {
        this.api = new APIClient(config, logger);
    }
    async status() {
        const spinner = createSpinner('Checking bridges...').start();
        const [devtoolsBridge, desktopBridge] = await Promise.all([
            this.api.getDevtoolsBridgeStatus(),
            getDesktopBridgeStatus(),
        ]);
        spinner.stop();
        console.log();
        console.log(chalk.white('DevTools Bridge (browser, port 4016):'));
        console.log(chalk.gray('  Status: ') + (devtoolsBridge.ok ? chalk.green('Reachable') : chalk.yellow('Not running')));
        if (devtoolsBridge.ok) {
            console.log(chalk.gray('  Endpoint: ') + displayEndpoint(devtoolsBridge.endpoint));
        }
        else {
            const detail = String(devtoolsBridge.error || 'Connection refused').trim() || 'Connection refused';
            console.log(chalk.gray('  Error: ') + chalk.yellow(detail));
        }
        console.log(chalk.gray('  Use for: ') + (devtoolsBridge.ok
            ? chalk.green('Local browser observability in CLI chat flows.')
            : chalk.gray('Start vigthoria-devtools-bridge on port 4016.')));
        console.log();
        console.log(chalk.white('Desktop Bridge (Windows desktop, port 49160):'));
        console.log(chalk.gray('  Status: ') + (desktopBridge.ok ? chalk.green('Reachable') : chalk.yellow('Not running')));
        if (desktopBridge.ok) {
            console.log(chalk.gray('  Endpoint: ') + displayEndpoint(desktopBridge.endpoint));
            if (desktopBridge.service)
                console.log(chalk.gray('  Service: ') + desktopBridge.service);
            if (desktopBridge.version)
                console.log(chalk.gray('  Version: ') + desktopBridge.version);
            console.log(chalk.gray('  Control: ') + (desktopBridge.controlEnabled ? chalk.green('enabled') : chalk.yellow('disabled')));
        }
        else {
            const detail = String(desktopBridge.error || 'Connection refused').trim() || 'Connection refused';
            console.log(chalk.gray('  Error: ') + chalk.yellow(detail));
            console.log(chalk.gray('  Windows setup: ') + 'Run extension.vigthoria.io/downloads/vigthoria-desktop-bridge-bringup.ps1');
            console.log(chalk.gray('  Tunnel: ') + 'ssh -N -R 127.0.0.1:49160:127.0.0.1:49160 root@78.46.154.201');
        }
        console.log(chalk.gray('  Use for: ') + (desktopBridge.ok
            ? chalk.green('Remote Vigthoria Code screenshots, input, and Electron DevTools (9229).')
            : chalk.gray('Start local-desktop-bridge.js on Windows, then open the SSH reverse tunnel.')));
        console.log();
        this.api.destroy();
        process.exitCode = 0;
    }
}
