import { v as validateAuth } from './auth-DuunT7Cg.js';
import { j as json } from './index-lhTMmBNn.js';
import { mkdirSync, writeFileSync, existsSync, readFileSync } from 'fs';
import { homedir } from 'os';
import { join } from 'path';
import './shared-server-DaWdgxVh.js';
import 'crypto';

const STATE_FILE = join(homedir(), ".shooter", "autopilot.json");
function control() {
  return globalThis.__shooter_autopilot;
}
function readFileEnabled() {
  try {
    if (existsSync(STATE_FILE)) {
      const parsed = JSON.parse(readFileSync(STATE_FILE, "utf-8"));
      return Boolean(parsed?.enabled);
    }
  } catch {
  }
  return false;
}
const GET = ({ request }) => {
  const authError = validateAuth(request);
  if (authError) {
    return authError;
  }
  const ctrl = control();
  return json({ enabled: ctrl ? ctrl.isEnabled() : readFileEnabled(), running: Boolean(ctrl) });
};
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.enabled !== "boolean") {
    return json({ error: "enabled must be a boolean" }, { status: 400 });
  }
  const ctrl = control();
  if (ctrl) {
    ctrl.setEnabled(body.enabled);
  } else {
    try {
      mkdirSync(join(homedir(), ".shooter"), { recursive: true });
      writeFileSync(STATE_FILE, JSON.stringify({ enabled: body.enabled }), "utf-8");
    } catch {
    }
  }
  return json({ enabled: body.enabled, running: Boolean(ctrl) });
};

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