import * as os from 'os';

export function getLocalIPv4(): string | null {

    const networkInterfaces = os.networkInterfaces?.(); // ✅ safe call

    if (!networkInterfaces) {
        console.warn('networkInterfaces is undefined');
        return null;
    }

    for (const interfaceName of Object.keys(networkInterfaces)) {
        const iface = networkInterfaces[interfaceName];
        if (!iface) continue;

        for (const config of iface) {
            if (config.family === 'IPv4' && !config.internal) {
                return config.address;
            }
        }
    }

    return null;
}
