import { v as validateAuth } from './auth-DuunT7Cg.js';
import { s as sosCoordinator } from './coordinator-DMU_ADXf.js';
import { j as json } from './index-lhTMmBNn.js';
import { randomBytes } from 'crypto';
import './shared-server-DaWdgxVh.js';
import 'better-sqlite3';
import 'fs';
import 'os';
import 'path';

const ACTIONS = /* @__PURE__ */ new Set(["block", "escalate", "relay"]);
const GET = ({ params, request }) => {
  const authError = validateAuth(request);
  if (authError) {
    return authError;
  }
  const rules = sosCoordinator.getRoutingRules(params.id ?? "");
  if (rules === null) {
    return json({ error: "Super-session not found" }, { status: 404 });
  }
  return json({ routingRules: rules });
};
const PATCH = async ({ params, request }) => {
  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 });
  }
  if (typeof body !== "object" || body === null) {
    return json({ error: "Invalid JSON in request body" }, { status: 400 });
  }
  const payload = body;
  if (!Array.isArray(payload.routingRules)) {
    return json({ error: "routingRules must be an array" }, { status: 400 });
  }
  const rules = [];
  for (const raw of payload.routingRules) {
    const rule = normalizeRule(raw);
    if (!rule) {
      return json(
        { error: "Each rule needs fromMemberId, toMemberId, and action" },
        { status: 400 }
      );
    }
    rules.push(rule);
  }
  const error = sosCoordinator.setRoutingRules(params.id ?? "", rules);
  if (error) {
    return json({ error }, { status: error.includes("not found") ? 404 : 400 });
  }
  return json({ routingRules: rules });
};
function normalizeRule(raw) {
  if (typeof raw !== "object" || raw === null) {
    return null;
  }
  const r = raw;
  if (typeof r.fromMemberId !== "string" || typeof r.toMemberId !== "string" || typeof r.action !== "string" || !ACTIONS.has(r.action)) {
    return null;
  }
  return {
    action: r.action,
    fromMemberId: r.fromMemberId,
    id: typeof r.id === "string" && r.id ? r.id : randomBytes(6).toString("hex"),
    matchPattern: typeof r.matchPattern === "string" ? r.matchPattern : "",
    priority: typeof r.priority === "number" ? r.priority : 100,
    toMemberId: r.toMemberId
  };
}

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