// @bun
import {
  buildCliLaunch,
  init_cliLaunch,
  quoteCliLaunch
} from "./chunk-4kqt1pm1.js";
import"./chunk-rpmntgyh.js";
import"./chunk-v4ypszbb.js";
import {
  execFileNoThrow,
  init_execFileNoThrow
} from "./chunk-hzmzwte2.js";
import"./chunk-pdvg91cg.js";
import"./chunk-c1yc761e.js";
import"./chunk-c5g9shkw.js";
import"./chunk-aeysytks.js";
import"./chunk-s3hafnk3.js";
import"./chunk-kb3758f7.js";
import"./chunk-9h9k5vz3.js";
import"./chunk-xmpgmeb8.js";
import"./chunk-2kz09j2s.js";
import"./chunk-c0k7b0jw.js";
import {
  __esm
} from "./chunk-hhsxm2yr.js";

// src/cli/bg/engines/tmux.ts
import { spawnSync } from "child_process";

class TmuxEngine {
  name = "tmux";
  supportsInteractiveInput = true;
  async available() {
    const { code } = await execFileNoThrow("tmux", ["-V"], { useCwd: false });
    return code === 0;
  }
  async start(opts) {
    const launch = buildCliLaunch(opts.args, {
      env: {
        ...opts.env,
        CLAUDE_CODE_SESSION_KIND: "bg",
        CLAUDE_CODE_SESSION_NAME: opts.sessionName,
        CLAUDE_CODE_SESSION_LOG: opts.logPath,
        CLAUDE_CODE_TMUX_SESSION: opts.sessionName
      }
    });
    const cmd = quoteCliLaunch(launch);
    const result = spawnSync("tmux", ["new-session", "-d", "-s", opts.sessionName, cmd], { stdio: "inherit", env: launch.env });
    if (result.status !== 0) {
      throw new Error("Failed to create tmux session.");
    }
    return {
      pid: 0,
      sessionName: opts.sessionName,
      logPath: opts.logPath,
      engineUsed: "tmux"
    };
  }
  async attach(session) {
    if (!session.tmuxSessionName) {
      throw new Error(`Session ${session.sessionId} has no tmux session name.`);
    }
    const result = spawnSync("tmux", ["attach-session", "-t", session.tmuxSessionName], { stdio: "inherit" });
    if (result.status !== 0) {
      throw new Error(`Failed to attach to tmux session '${session.tmuxSessionName}'.`);
    }
  }
}
function getTmuxInstallHint() {
  if (process.platform === "darwin") {
    return "Install with: brew install tmux";
  }
  if (process.platform === "win32") {
    return "tmux is not natively available on Windows. Consider using WSL.";
  }
  return "Install with: sudo apt install tmux  (or your package manager)";
}
var init_tmux = __esm(() => {
  init_execFileNoThrow();
  init_cliLaunch();
});
init_tmux();

export {
  getTmuxInstallHint,
  TmuxEngine
};
