import { v as validateAuth } from './auth-DuunT7Cg.js';
import { j as json } from './index-CoD1IJuy.js';
import './shared-server-DaWdgxVh.js';
import 'crypto';

function control() {
  return globalThis.__shooter_autopilot;
}
const GET = ({ request, url }) => {
  const authError = validateAuth(request);
  if (authError) {
    return authError;
  }
  const terminalId = url.searchParams.get("terminalId") ?? "";
  if (!terminalId) {
    return json({ error: "terminalId query param is required" }, { status: 400 });
  }
  const ctrl = control();
  if (!ctrl?.getGoal) {
    return json({ error: "autopilot engine not running", running: false }, { status: 503 });
  }
  return json({ goal: ctrl.getGoal(terminalId) ?? null, running: true });
};
const POST = async ({ request }) => {
  const authError = validateAuth(request);
  if (authError) {
    return authError;
  }
  let body;
  try {
    body = await request.json();
  } catch {
    return json({ error: "Invalid JSON body" }, { status: 400 });
  }
  if (typeof body.terminalId !== "string" || body.terminalId.length === 0) {
    return json({ error: "terminalId must be a non-empty string" }, { status: 400 });
  }
  if (typeof body.goal !== "string") {
    return json({ error: "goal must be a string" }, { status: 400 });
  }
  if (body.goal.length > 500) {
    return json({ error: "goal must be 500 characters or fewer" }, { status: 400 });
  }
  const ctrl = control();
  if (!ctrl?.setGoal) {
    return json({ error: "autopilot engine not running", running: false }, { status: 503 });
  }
  ctrl.setGoal(body.terminalId, body.goal);
  return json({ goal: body.goal.trim() || null, running: true, terminalId: body.terminalId });
};

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