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

const TOKENS_DIR = join(homedir(), ".shooter");
const TOKENS_FILE = join(TOKENS_DIR, "device-tokens.json");
function readTokens() {
  try {
    if (existsSync(TOKENS_FILE)) {
      const parsed = JSON.parse(readFileSync(TOKENS_FILE, "utf-8"));
      if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
        return {};
      }
      return parsed;
    }
  } catch {
  }
  return {};
}
function writeTokens(tokens) {
  if (!existsSync(TOKENS_DIR)) {
    mkdirSync(TOKENS_DIR, { mode: 448, recursive: true });
  }
  writeFileSync(TOKENS_FILE, JSON.stringify(tokens, null, 2), { encoding: "utf-8", mode: 384 });
}
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 tokens = readTokens();
  tokens[platform] = token;
  writeTokens(tokens);
  if (platform === "ios") {
    process.env.DEVICE_TOKEN = token;
  }
  console.log(`[device-token] Registered ${platform} token (length: ${token.length})`);
  return json({
    platform,
    success: true,
    timestamp: (/* @__PURE__ */ new Date()).toISOString()
  });
};

export { POST };
//# sourceMappingURL=_server.ts-BLNDdFWC.js.map
