import { b as private_env } from './shared-server-DaWdgxVh.js';
import { v as validateAuth } from './auth-DuunT7Cg.js';
import { t as toDeviceListItem } from './device-format-DTgEz4Yr.js';
import { d as deviceTokenStore, M as MAX_TOKEN_LENGTH, a as MAX_DEVICE_ID_LENGTH, b as MAX_NAME_LENGTH, c as MAX_BUNDLE_ID_LENGTH } from './device-token-store-Ct7aTeR8.js';
import { j as json } from './index-lhTMmBNn.js';
import 'crypto';
import 'better-sqlite3';
import 'fs';
import 'path';
import './shooter-home-4f_HkdGI.js';
import 'os';

function resolveAppEnv(requested) {
  if (requested === "production" || requested === "sandbox") {
    return requested;
  }
  return private_env.APNS_PRODUCTION === "true" ? "production" : "sandbox";
}
const GET = ({ request }) => {
  const authError = validateAuth(request);
  if (authError) {
    return authError;
  }
  const ios = deviceTokenStore.listActive("ios");
  const android = deviceTokenStore.listActive("android");
  const devices = [...ios, ...android].map(toDeviceListItem);
  return json({
    counts: { android: android.length, ios: ios.length, total: devices.length },
    devices
  });
};
const DELETE = 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 });
  }
  const id = body && typeof body === "object" && !Array.isArray(body) ? body.id : void 0;
  if (typeof id !== "string" || id.trim().length === 0) {
    return json({ error: "Missing required field: id" }, { status: 400 });
  }
  const removed = deviceTokenStore.deleteById(id.trim());
  if (removed === 0) {
    return json({ error: "Device not found or already removed", id: id.trim() }, { status: 404 });
  }
  return json({ removed, success: true });
};
const POST = async ({ request }) => {
  const authError = validateAuth(request);
  if (authError) {
    return authError;
  }
  let body;
  try {
    const parsed = await request.json();
    if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
      return json({ error: "Invalid JSON body: expected an object" }, { status: 400 });
    }
    body = parsed;
  } catch {
    return json({ error: "Invalid JSON body" }, { status: 400 });
  }
  const platform = body.platform;
  if (!platform || platform !== "ios" && platform !== "android") {
    return json(
      { error: 'Missing or invalid platform (must be "ios" or "android")' },
      { status: 400 }
    );
  }
  const rawToken = body.deviceToken || body.token;
  if (!rawToken || typeof rawToken !== "string" || rawToken.trim().length === 0) {
    return json({ error: "Missing device token (deviceToken or token)" }, { status: 400 });
  }
  const token = rawToken.trim();
  const deviceId = typeof body.deviceId === "string" ? body.deviceId.trim() || null : null;
  const friendlyName = typeof body.deviceName === "string" ? body.deviceName.trim() || null : null;
  const bundleId = typeof body.bundleId === "string" ? body.bundleId.trim() || null : null;
  const tooLong = token.length > MAX_TOKEN_LENGTH ? "token" : deviceId && deviceId.length > MAX_DEVICE_ID_LENGTH ? "deviceId" : friendlyName && friendlyName.length > MAX_NAME_LENGTH ? "deviceName" : bundleId && bundleId.length > MAX_BUNDLE_ID_LENGTH ? "bundleId" : null;
  if (tooLong) {
    return json({ error: `Field "${tooLong}" exceeds maximum allowed length` }, { status: 400 });
  }
  const appEnv = resolveAppEnv(body.appEnv);
  const record = deviceTokenStore.upsert({
    appEnv,
    bundleId,
    deviceId,
    friendlyName,
    platform,
    token
  });
  if (record.deviceId !== deviceId) {
    return json(
      { error: "Token is already registered to a different active device" },
      { status: 409 }
    );
  }
  console.log(
    `[device-token] Registered ${platform} device ${record.id} (token len ${token.length})`
  );
  return json({
    deviceId: record.deviceId,
    id: record.id,
    platform,
    success: true,
    timestamp: (/* @__PURE__ */ new Date()).toISOString()
  });
};

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