'use strict';

var child_process = require('child_process');

// src/features/watch-pr-review/infrastructure/system.ts
function createStopController() {
  let stopped = false;
  const previousListeners = process.listeners("SIGINT");
  process.removeAllListeners("SIGINT");
  const handleSigint = () => {
    stopped = true;
  };
  process.on("SIGINT", handleSigint);
  return {
    dispose: () => {
      process.removeListener("SIGINT", handleSigint);
      previousListeners.forEach((listener) => process.on("SIGINT", listener));
    },
    isStopped: () => stopped,
    requestStop: () => {
      stopped = true;
    }
  };
}
function waitInterruptible(durationMs, isStopped) {
  const checkIntervalMs = 250;
  return new Promise((resolve) => {
    let elapsedMs = 0;
    const timer = setInterval(() => {
      elapsedMs += checkIntervalMs;
      if (isStopped() || elapsedMs >= durationMs) {
        clearInterval(timer);
        resolve();
      }
    }, checkIntervalMs);
  });
}
function createMutex() {
  let tail = Promise.resolve();
  return (task) => {
    const run = tail.then(() => task());
    tail = run.then(
      () => void 0,
      () => void 0
    );
    return run;
  };
}
function openInEditor(filePath) {
  const editor = process.env.VISUAL || process.env.EDITOR;
  if (editor) {
    child_process.execFileSync(editor, [filePath], { stdio: "inherit" });
    return;
  }
  const opener = resolvePlatformOpener();
  child_process.spawn(opener.command, [...opener.args, filePath], { detached: true, stdio: "ignore" }).unref();
}
function resolvePlatformOpener() {
  if (process.platform === "darwin") {
    return { args: [], command: "open" };
  }
  if (process.platform === "win32") {
    return { args: ["/c", "start", ""], command: "cmd" };
  }
  return { args: [], command: "xdg-open" };
}

exports.createMutex = createMutex;
exports.createStopController = createStopController;
exports.openInEditor = openInEditor;
exports.waitInterruptible = waitInterruptible;
