"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSocketReport = void 0;
const child_process_1 = require("child_process");
const util_1 = require("util");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
async function getSocketStatistics() {
    try {
        const { stdout } = await execAsync('ss -s');
        const lines = stdout.trim().split('\n');
        const totalLine = lines.find((line) => line.startsWith('Total:')) || '';
        const tcpLine = lines.find((line) => line.startsWith('TCP:')) || '';
        const [, total] = totalLine.match(/Total:\s+(\d+)/) || [];
        const [, tcp, estab, closed, orphaned, timewait] = tcpLine.match(/TCP:\s+(\d+)\s+\(estab\s+(\d+),\s+closed\s+(\d+),\s+orphaned\s+(\d+),\s+timewait\s+(\d+)\)/) || [];
        const result = {
            error: false,
            version: null,
            statistics: {
                total: parseInt(total || '0', 10),
                tcp: {
                    total: parseInt(tcp || '0', 10),
                    estab: parseInt(estab || '0', 10),
                    closed: parseInt(closed || '0', 10),
                    orphaned: parseInt(orphaned || '0', 10),
                    timewait: parseInt(timewait || '0', 10),
                },
            },
        };
        return result;
    }
    catch (error) {
        const result = {
            error: true,
            version: null,
            message: error.message,
        };
        return result;
    }
}
async function getVersion() {
    try {
        const { stdout } = await execAsync('ss -V');
        const version = stdout.trim();
        return version;
    }
    catch (error) {
        //console.error('Error retrieving version:', error)
        return null;
    }
}
// async function main() {
//   const socketStatistics = await getSocketStatistics();
//   const version = await getVersion();
//   socketStatistics.version = version;
//   console.log(JSON.stringify(socketStatistics, null, 2));
// }
// main().catch((error) => {
//   console.error('An error occurred:', error);
// });
async function getSocketReport() {
    const socketStatistics = await getSocketStatistics();
    const version = await getVersion();
    socketStatistics.version = version;
    //console.log(JSON.stringify(socketStatistics, null, 2))
    return socketStatistics;
}
exports.getSocketReport = getSocketReport;
//# sourceMappingURL=debugUtils.js.map