"use strict";
/**
 * Platform-aware command suggestions and utilities
 *
 * Provides OS-specific commands for troubleshooting messages
 * to help non-technical users on Windows, macOS, and Linux.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.killProcessOnPort = exports.killAllCLIProxyProcesses = exports.killProcessByPid = exports.getPlatformName = exports.getKillPidCommand = exports.getKillCLIProxyCommand = exports.getCatCommand = exports.getPortCheckCommand = void 0;
const child_process_1 = require("child_process");
const isWindows = process.platform === 'win32';
const isMac = process.platform === 'darwin';
/**
 * Get platform-specific command to check what's using a port
 */
function getPortCheckCommand(port) {
    if (isWindows) {
        return `netstat -ano | findstr :${port}`;
    }
    return `lsof -i :${port}`;
}
exports.getPortCheckCommand = getPortCheckCommand;
/**
 * Get platform-specific command to view file contents
 */
function getCatCommand(filePath) {
    if (isWindows) {
        // Use type for CMD, Get-Content for PowerShell
        return `type "${filePath}"`;
    }
    return `cat "${filePath}"`;
}
exports.getCatCommand = getCatCommand;
/**
 * Get platform-specific command to kill CLIProxy Plus processes
 */
function getKillCLIProxyCommand() {
    if (isWindows) {
        return 'taskkill /F /IM cli-proxy-api-plus.exe';
    }
    return 'pkill -f cli-proxy-api-plus';
}
exports.getKillCLIProxyCommand = getKillCLIProxyCommand;
/**
 * Get platform-specific command to kill a process by PID
 */
function getKillPidCommand(pid) {
    if (isWindows) {
        return `taskkill /F /PID ${pid}`;
    }
    return `kill -9 ${pid}`;
}
exports.getKillPidCommand = getKillPidCommand;
/**
 * Get human-friendly platform name
 */
function getPlatformName() {
    if (isWindows)
        return 'Windows';
    if (isMac)
        return 'macOS';
    return 'Linux';
}
exports.getPlatformName = getPlatformName;
/**
 * Kill process by PID (cross-platform)
 * @returns true if killed successfully, false otherwise
 */
function killProcessByPid(pid, verbose = false) {
    try {
        if (isWindows) {
            (0, child_process_1.execSync)(`taskkill /F /PID ${pid}`, { stdio: 'pipe' });
        }
        else {
            (0, child_process_1.execSync)(`kill -9 ${pid}`, { stdio: 'pipe' });
        }
        if (verbose) {
            console.error(`[cleanup] Killed process ${pid}`);
        }
        return true;
    }
    catch {
        return false;
    }
}
exports.killProcessByPid = killProcessByPid;
/**
 * Kill all CLIProxy Plus processes (cross-platform)
 * @returns number of processes killed
 */
function killAllCLIProxyProcesses(verbose = false) {
    let killed = 0;
    try {
        if (isWindows) {
            // Windows: taskkill by image name
            // Use /T to kill child processes too
            (0, child_process_1.execSync)('taskkill /F /IM cli-proxy-api-plus.exe /T 2>nul', { stdio: 'pipe' });
            killed++;
        }
        else {
            // Unix: pkill with pattern matching
            try {
                (0, child_process_1.execSync)('pkill -9 -f cli-proxy-api-plus', { stdio: 'pipe' });
                killed++;
            }
            catch {
                // pkill returns non-zero if no processes matched - that's OK
            }
        }
    }
    catch {
        // No processes to kill or command failed
    }
    if (verbose && killed > 0) {
        console.error(`[cleanup] Killed ${killed} CLIProxy Plus process(es)`);
    }
    return killed;
}
exports.killAllCLIProxyProcesses = killAllCLIProxyProcesses;
/**
 * Kill process on specific port (cross-platform)
 * @returns true if a process was killed, false otherwise
 */
function killProcessOnPort(port, verbose = false) {
    try {
        if (isWindows) {
            // Windows: netstat + taskkill
            const result = (0, child_process_1.execSync)(`netstat -ano | findstr :${port}`, {
                encoding: 'utf-8',
                stdio: 'pipe',
            });
            const lines = result.trim().split('\n');
            let killed = false;
            for (const line of lines) {
                const parts = line.trim().split(/\s+/);
                const pid = parts[parts.length - 1];
                if (pid && /^\d+$/.test(pid)) {
                    try {
                        (0, child_process_1.execSync)(`taskkill /F /PID ${pid}`, { stdio: 'pipe' });
                        if (verbose) {
                            console.error(`[cleanup] Killed process ${pid} on port ${port}`);
                        }
                        killed = true;
                    }
                    catch {
                        // Process may have already exited
                    }
                }
            }
            return killed;
        }
        else {
            // Unix: lsof + kill
            const result = (0, child_process_1.execSync)(`lsof -ti:${port}`, { encoding: 'utf-8', stdio: 'pipe' });
            const pids = result
                .trim()
                .split('\n')
                .filter((p) => p);
            for (const pid of pids) {
                try {
                    (0, child_process_1.execSync)(`kill -9 ${pid}`, { stdio: 'pipe' });
                    if (verbose) {
                        console.error(`[cleanup] Killed process ${pid} on port ${port}`);
                    }
                }
                catch {
                    // Process may have already exited
                }
            }
            return pids.length > 0;
        }
    }
    catch {
        // No process on port or command failed
        return false;
    }
}
exports.killProcessOnPort = killProcessOnPort;
exports.default = {
    getPortCheckCommand,
    getCatCommand,
    getKillCLIProxyCommand,
    getKillPidCommand,
    getPlatformName,
    killProcessByPid,
    killAllCLIProxyProcesses,
    killProcessOnPort,
};
//# sourceMappingURL=platform-commands.js.map