import { getOs } from '../nodejs/os.mjs';

/**
 * 获取ip地址
 * @returns 字符串，形如 x.x.x.x
 *
 * @example
 * ```ts
 * getIPAddress() // 10.10.10.10
 * ```
 */
function getIPAddress() {
  var interfaces = getOs().networkInterfaces();
  // eslint-disable-next-line no-restricted-syntax
  for (var devName in interfaces) {
    var iface = interfaces[devName];
    if (!iface) continue;
    // eslint-disable-next-line @typescript-eslint/prefer-for-of
    for (var i = 0; i < iface.length; i++) {
      var alias = iface[i];
      if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
        return alias.address;
      }
    }
  }
  return '';
}
/**
 *
 * 获取ip的字符串
 * @returns 字符串，形如 x_x_x_x
 *
 * @example
 * ```ts
 * getIPAddressStr() // 10_10_10_10
 * ```
 */
function getIPAddressStr() {
  var ip = getIPAddress();
  ip = ip.replace(/\./g, '_');
  return ip;
}

export { getIPAddress, getIPAddressStr };
