import { v as validateAuth } from './auth-DeCdZ83n.js';
import { p as ptyManager } from './pty-manager-DR0Wt2Ac.js';
import { t as toErrorMessage } from './error-DDXB3duW.js';
import { j as json } from './shared-server-sSGG17Df.js';
import 'crypto';
import 'child_process';
import 'fs';
import 'path';
import 'url';
import 'net';
import 'better-sqlite3';

const GET = ({ params, request }) => {
  const authError = validateAuth(request);
  if (authError) {
    return authError;
  }
  try {
    const terminal = ptyManager.get(params.id);
    if (!terminal) {
      return json({ error: "Terminal not found" }, { status: 404 });
    }
    return json({
      args: terminal.args,
      clientCount: terminal.clients.size,
      command: terminal.command,
      createdAt: terminal.createdAt.toISOString(),
      cwd: terminal.cwd,
      exitCode: terminal.exitCode,
      exitedAt: terminal.exitedAt?.toISOString() ?? null,
      id: terminal.id,
      lastOutput: terminal.scrollback.length > 0 ? terminal.scrollback[terminal.scrollback.length - 1] : null,
      pid: terminal.pid,
      sessionWs: `/ws/session/${terminal.id}`,
      status: terminal.status,
      timestamp: (/* @__PURE__ */ new Date()).toISOString(),
      ws: `/ws/terminal/${terminal.id}`
    });
  } catch (error) {
    console.error("[terminals] Failed to get terminal:", toErrorMessage(error));
    return json({ error: "Failed to get terminal" }, { status: 500 });
  }
};
const DELETE = ({ params, request }) => {
  const authError = validateAuth(request);
  if (authError) {
    return authError;
  }
  try {
    const terminal = ptyManager.get(params.id);
    if (!terminal) {
      return json({ error: "Terminal not found" }, { status: 404 });
    }
    if (terminal.status === "exited") {
      ptyManager.remove(params.id);
      console.log(`[terminals] Removed exited terminal ${params.id}`);
      return json({
        removed: true,
        success: true,
        timestamp: (/* @__PURE__ */ new Date()).toISOString()
      });
    }
    ptyManager.kill(params.id);
    console.log(`[terminals] Killed terminal ${params.id} (pid=${terminal.pid})`);
    return json({
      success: true,
      timestamp: (/* @__PURE__ */ new Date()).toISOString()
    });
  } catch (error) {
    console.error("[terminals] Failed to kill terminal:", toErrorMessage(error));
    return json({ error: "Failed to kill terminal" }, { status: 500 });
  }
};

export { DELETE, GET };
//# sourceMappingURL=_server.ts-C8slOHB0.js.map
