import { v as validateAuth } from './auth-DuunT7Cg.js';
import { p as ptyManager } from './pty-manager-DDjG7DlH.js';
import { s as shareStore, h as hashPassword } from './share-store-B9jMpVg0.js';
import { c as closeGuests } from './guest-registry-Dxvd7p-g.js';
import { j as json } from './index-CoD1IJuy.js';
import './shared-server-DaWdgxVh.js';
import 'crypto';
import 'child_process';
import 'fs';
import 'path';
import 'url';
import './qwen-reader-DGfUbKaJ.js';
import 'os';
import './super-session-handler-DPyxFgmz.js';
import './registry-D4J_CuzW.js';
import 'better-sqlite3';
import './coordinator-DMU_ADXf.js';
import 'net';
import 'node:module';
import './shooter-home-4f_HkdGI.js';

const MIN_PASSWORD_LENGTH = 6;
const MODES = ["view", "control"];
function toInfo(terminalId) {
  const share = shareStore.getShare(terminalId);
  if (!share) {
    return { active: false, createdAt: null, mode: null, updatedAt: null };
  }
  return { active: true, createdAt: share.createdAt, mode: share.mode, updatedAt: share.updatedAt };
}
const GET = ({ params, request }) => {
  const authError = validateAuth(request);
  if (authError) {
    return authError;
  }
  return json(toInfo(params.id));
};
const PUT = async ({ params, request }) => {
  const authError = validateAuth(request);
  if (authError) {
    return authError;
  }
  if (!ptyManager.get(params.id)) {
    return json({ error: "Terminal not found" }, { status: 404 });
  }
  let body;
  try {
    body = await request.json();
  } catch {
    return json({ error: "Invalid JSON" }, { status: 400 });
  }
  if (!MODES.includes(body.mode)) {
    return json({ error: "mode must be 'view' or 'control'" }, { status: 400 });
  }
  const existing = shareStore.getShare(params.id);
  const password = typeof body.password === "string" ? body.password : "";
  if (!existing && password.length < MIN_PASSWORD_LENGTH) {
    return json(
      { error: `password is required (min ${String(MIN_PASSWORD_LENGTH)} chars)` },
      { status: 400 }
    );
  }
  if (password && password.length < MIN_PASSWORD_LENGTH) {
    return json(
      { error: `password must be at least ${String(MIN_PASSWORD_LENGTH)} chars` },
      { status: 400 }
    );
  }
  const now = Date.now();
  shareStore.setShare({
    createdAt: existing?.createdAt ?? now,
    mode: body.mode,
    // `existing` is guaranteed non-null when password is empty (validated above).
    passwordHash: password ? hashPassword(password) : existing?.passwordHash ?? "",
    terminalId: params.id,
    updatedAt: now
  });
  if (password) {
    shareStore.deleteSessions(params.id);
  }
  if (password || existing?.mode !== body.mode) {
    closeGuests(params.id);
  }
  return json(toInfo(params.id));
};
const DELETE = ({ params, request }) => {
  const authError = validateAuth(request);
  if (authError) {
    return authError;
  }
  shareStore.deleteShare(params.id);
  const closed = closeGuests(params.id);
  return json({ closedConnections: closed, success: true });
};

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