// src/service/tunnel.ts
import { spawn } from "node:child_process";
import { resolve as pathResolve } from "node:path";
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
function startTunnel() {
  const defaultPath = process.platform === "win32" ? "pymobiledevice3\\pymobiledevice3.exe" : "pymobiledevice3/pymobiledevice3";
  const cliPath = pathResolve(
    dirname(fileURLToPath(import.meta.url)),
    process.env.PMD_CLI_PATH ?? defaultPath
  );
  console.log(`Starting tunnel with: ${cliPath} remote tunneld`);
  const child = spawn(cliPath, ["remote", "tunneld"], {
    stdio: "inherit"
  });
  child.on("error", (error) => {
    console.error("Failed to start tunnel:", error);
    process.exit(1);
  });
  child.on("exit", (code, signal) => {
    console.log(`Tunnel process exited with code ${code} and signal ${signal}`);
    process.exit(code ?? 1);
  });
  process.on("SIGTERM", () => {
    console.log("Received SIGTERM, stopping tunnel...");
    child.kill();
  });
  process.on("SIGINT", () => {
    console.log("Received SIGINT, stopping tunnel...");
    child.kill();
  });
}
startTunnel();
//# sourceMappingURL=tunnel.mjs.map
