#!/usr/bin/env node
import {
  loadConfig
} from "./chunk-4543KN27.js";
import "./chunk-NQDJ6QAM.js";
import "./chunk-LWDOXOWY.js";
import "./chunk-7RSBFN2G.js";

// src/relay-token.ts
function toHttpUrl(relayUrl) {
  return relayUrl.replace(/^ws:/i, "http:").replace(/^wss:/i, "https:").replace(/\/$/, "");
}
async function runRelayTokenCommand(options) {
  let config;
  try {
    config = loadConfig({ relayName: options.relayName });
  } catch (err) {
    console.error(err instanceof Error ? err.message : String(err));
    process.exit(1);
  }
  const { relayName, relayUrl, relayToken } = config;
  if (!relayUrl) {
    console.error(
      `Relay "${relayName}" has no URL configured. Edit ~/.dev-anywhere/config.json or set RELAY_URL.`
    );
    process.exit(1);
  }
  if (!relayToken) {
    console.error(
      `Relay "${relayName}" has no proxy token configured. The admin endpoint requires one.`
    );
    process.exit(1);
  }
  const adminUrl = `${toHttpUrl(relayUrl)}/api/admin/client-token`;
  let res;
  try {
    res = await fetch(adminUrl, {
      headers: { authorization: `Bearer ${relayToken}` },
      cache: "no-store"
    });
  } catch (err) {
    console.error(`Request to ${adminUrl} failed: ${err instanceof Error ? err.message : err}`);
    process.exit(1);
  }
  if (res.status === 401) {
    console.error(
      `Relay rejected the proxy token (HTTP 401). Verify ~/.dev-anywhere/config.json matches the relay's RELAY_PROXY_TOKEN.`
    );
    process.exit(1);
  }
  if (res.status === 204) {
    const result = { status: "no_client_token" };
    printResult(relayName, relayUrl, result);
    return;
  }
  if (!res.ok) {
    const body = await res.text().catch(() => "");
    console.error(`Unexpected response from relay (HTTP ${res.status}): ${body}`);
    process.exit(1);
  }
  const json = await res.json();
  if (typeof json.clientToken !== "string" || json.clientToken.length === 0) {
    console.error(`Relay returned a malformed payload: ${JSON.stringify(json)}`);
    process.exit(1);
  }
  printResult(relayName, relayUrl, { status: "ok", clientToken: json.clientToken });
}
function printResult(relayName, relayUrl, result) {
  const httpBase = toHttpUrl(relayUrl);
  console.log(`Relay: ${relayName} (${relayUrl})`);
  if (result.status === "no_client_token") {
    console.log(`Token: (not configured \u2014 /client endpoint is open)`);
    console.log(`URL:   ${httpBase}/`);
    return;
  }
  console.log(`Token: ${result.clientToken}`);
  console.log(`URL:   ${httpBase}/`);
  console.log(`Use:   paste the token in Settings -> Relay Token`);
}
export {
  runRelayTokenCommand
};
//# sourceMappingURL=relay-token-AANHZIGJ.js.map