/**
 * Port utilities — find available ports, detect conflicts.
 */
import * as net from 'node:net';
/**
 * Check if a port is available (not in use).
 */
export function isPortAvailable(port, host = '127.0.0.1') {
    return new Promise(resolve => {
        const server = net.createServer();
        server.once('error', () => {
            resolve(false);
        });
        server.once('listening', () => {
            server.close(() => resolve(true));
        });
        server.listen(port, host);
    });
}
/**
 * Find an available port starting from `preferred`, incrementing if busy.
 */
export async function findAvailablePort(preferred, host = '127.0.0.1', maxAttempts = 10) {
    for (let i = 0; i < maxAttempts; i++) {
        const port = preferred + i;
        if (await isPortAvailable(port, host)) {
            return port;
        }
    }
    throw new Error(`No available port found in range ${preferred}-${preferred + maxAttempts - 1}`);
}
/**
 * Get port usage info for diagnostics.
 */
export async function checkPorts(config) {
    const proxyAvailable = await isPortAvailable(config.port);
    let routerPort = null;
    let routerAvailable = null;
    try {
        const url = new URL(config.routerUrl);
        routerPort = parseInt(url.port, 10) || (url.protocol === 'https:' ? 443 : 80);
        // For the router, we check if something IS listening (we want it to be in use)
        routerAvailable = !(await isPortAvailable(routerPort));
    }
    catch {
        // Invalid URL
    }
    return { proxyPort: config.port, proxyAvailable, routerPort, routerAvailable };
}
//# sourceMappingURL=port-utils.js.map