import { a as cleanup, b as getDecision, s as setDecision, g as getRichRequest } from './pending-requests-8rWjrF6d.js';
import { v as validateAuth } from './auth-DuunT7Cg.js';
import { t as toErrorMessage } from './error-DDXB3duW.js';
import 'type-decoder';
import { j as json } from './index-lhTMmBNn.js';
import 'better-sqlite3';
import 'fs';
import 'path';
import './shooter-home-4f_HkdGI.js';
import 'os';
import './shared-server-DaWdgxVh.js';
import 'crypto';

const DECISION_KINDS = [
  "allow",
  "deny",
  "option_1",
  "option_2",
  "option_3",
  "option_4",
  "plan_accept",
  "plan_auto",
  "plan_keep",
  "plan_review"
];
function isDecisionKind(value) {
  return typeof value === "string" && DECISION_KINDS.includes(value);
}
const POST = async ({ request }) => {
  cleanup();
  const authError = validateAuth(request);
  if (authError) {
    return authError;
  }
  let body;
  try {
    body = await request.json();
  } catch {
    return json({ error: "Invalid JSON in request body" }, { status: 400 });
  }
  try {
    const { decision, requestId } = body;
    if (typeof requestId !== "string" || requestId.length === 0) {
      return json({ error: "requestId must be a non-empty string" }, { status: 400 });
    }
    if (!isDecisionKind(decision)) {
      return json(
        {
          error: "decision must be one of allow, deny, option_1..4, plan_auto, plan_accept, plan_review, plan_keep"
        },
        { status: 400 }
      );
    }
    const decisionTyped = decision;
    const found = setDecision(requestId, decisionTyped);
    if (!found) {
      return json({ error: "Request not found or expired" }, { status: 404 });
    }
    const entry = getRichRequest(requestId);
    const responseKind = entry?.responseKind ?? "hook";
    console.log(
      `[response] Decision received: ${decisionTyped} for request ${requestId} (responseKind=${responseKind})`
    );
    if (responseKind === "pty") {
      return json({
        message: "Decision recorded; PTY routing not yet implemented",
        responseKind,
        success: true,
        timestamp: (/* @__PURE__ */ new Date()).toISOString()
      });
    }
    return json({
      message: "Decision recorded",
      responseKind,
      success: true,
      timestamp: (/* @__PURE__ */ new Date()).toISOString()
    });
  } catch (error) {
    console.error("[response] Failed to process response:", toErrorMessage(error));
    return json({ error: "Failed to process response" }, { status: 500 });
  }
};
const GET = ({ request, url }) => {
  cleanup();
  const authErr = validateAuth(request);
  if (authErr) {
    return authErr;
  }
  const requestId = url.searchParams.get("requestId");
  if (!requestId) {
    return json({ error: "requestId query parameter is required" }, { status: 400 });
  }
  const result = getDecision(requestId);
  if (result.status === "not_found") {
    return json({ error: "Request not found or expired" }, { status: 404 });
  }
  return json({
    decision: result.decision ?? null,
    status: result.status,
    timestamp: (/* @__PURE__ */ new Date()).toISOString()
  });
};

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