/**
 * @license
 * Copyright 2025-2026 Open Home Foundation
 * SPDX-License-Identifier: Apache-2.0
 */
import { spawn } from "node:child_process";
import { platform } from "node:os";
const PLATFORM_MAC = platform() === "darwin";
async function pingIp(ipAddress, timeout = 2, attempts = 1) {
  const isIpv6 = ipAddress.includes(":");
  let command;
  let args;
  if (isIpv6 && PLATFORM_MAC) {
    command = "ping6";
    args = ["-c", "1", ipAddress];
  } else if (isIpv6) {
    command = "ping";
    args = ["-6", "-c", "1", "-W", timeout.toString(), ipAddress];
  } else {
    command = "ping";
    args = ["-c", "1", "-W", timeout.toString(), ipAddress];
  }
  while (attempts > 0) {
    attempts--;
    try {
      const success = await runPingCommand(command, args, timeout + 2);
      if (success || attempts === 0) {
        return success;
      }
    } catch {
      if (attempts === 0) {
        return false;
      }
    }
    if (attempts > 0) {
      await new Promise((resolve) => setTimeout(resolve, 1e4));
    }
  }
  return false;
}
function runPingCommand(command, args, timeoutSeconds) {
  return new Promise((resolve, reject) => {
    const proc = spawn(command, args, {
      stdio: ["ignore", "pipe", "pipe"]
    });
    const timer = setTimeout(() => {
      proc.kill();
      reject(new Error("Ping timeout"));
    }, timeoutSeconds * 1e3);
    proc.on("close", (code) => {
      clearTimeout(timer);
      resolve(code === 0);
    });
    proc.on("error", (err) => {
      clearTimeout(timer);
      reject(err);
    });
  });
}
export {
  pingIp
};
//# sourceMappingURL=network.js.map
