#!/usr/bin/env node
import {
  auditTangleCatalogRuntimePackages,
  buildTangleCatalogRuntimePackageManifest,
  renderTangleCatalogRuntimePnpmAddCommand,
  startTangleCatalogRuntimeNodeServer
} from "../chunk-M2RFFAMB.js";
import "../chunk-JCHD6L3B.js";
import "../chunk-CDY2ETYT.js";
import "../chunk-H4XYLS7T.js";
import "../chunk-S2MVWQYL.js";
import "../chunk-VVC7U7W7.js";
import "../chunk-376UBTNB.js";
import "../chunk-F4YILONK.js";
import "../chunk-2TW2QKGZ.js";
import "../chunk-Q5X3QNHR.js";

// src/bin/tangle-catalog-runtime.ts
var args = new Set(process.argv.slice(2));
if (args.has("--print-package-json")) {
  console.log(JSON.stringify(buildTangleCatalogRuntimePackageManifest({
    agentIntegrationsVersion: process.env.TANGLE_AGENT_INTEGRATIONS_VERSION
  }), null, 2));
  process.exit(0);
}
if (args.has("--print-pnpm-add")) {
  console.log(renderTangleCatalogRuntimePnpmAddCommand({
    agentIntegrationsVersion: process.env.TANGLE_AGENT_INTEGRATIONS_VERSION
  }));
  process.exit(0);
}
if (args.has("--audit-packages")) {
  const connectorIds = process.env.TANGLE_CATALOG_AUDIT_CONNECTORS?.split(",").map((id) => id.trim()).filter(Boolean);
  console.log(JSON.stringify(await auditTangleCatalogRuntimePackages({ connectorIds }), null, 2));
  process.exit(0);
}
var secret = process.env.TANGLE_CATALOG_RUNTIME_SECRET;
if (!secret || secret.length < 32) {
  console.error("TANGLE_CATALOG_RUNTIME_SECRET must be set to at least 32 characters.");
  process.exit(1);
}
var authResolverUrl = process.env.TANGLE_CATALOG_AUTH_RESOLVER_URL;
var authResolverSecret = process.env.TANGLE_CATALOG_AUTH_RESOLVER_SECRET;
if (Boolean(authResolverUrl) !== Boolean(authResolverSecret)) {
  console.error("TANGLE_CATALOG_AUTH_RESOLVER_URL and TANGLE_CATALOG_AUTH_RESOLVER_SECRET must be set together.");
  process.exit(1);
}
var port = Number(process.env.PORT ?? process.env.TANGLE_CATALOG_RUNTIME_PORT ?? 4109);
if (!Number.isInteger(port) || port < 1 || port > 65535) {
  console.error("PORT must be an integer between 1 and 65535.");
  process.exit(1);
}
var server = await startTangleCatalogRuntimeNodeServer({
  secret,
  host: process.env.HOST ?? process.env.TANGLE_CATALOG_RUNTIME_HOST ?? "0.0.0.0",
  port,
  authResolver: authResolverUrl && authResolverSecret ? {
    endpoint: authResolverUrl,
    secret: authResolverSecret
  } : false,
  onLog: (event) => {
    const line = JSON.stringify({
      level: event.level,
      message: event.message,
      ...event.metadata
    });
    if (event.level === "error") console.error(line);
    else console.log(line);
  }
});
console.log(JSON.stringify({
  level: "info",
  message: "Tangle catalog runtime listening.",
  url: server.url
}));
for (const signal of ["SIGINT", "SIGTERM"]) {
  process.once(signal, async () => {
    await server.close();
    process.exit(0);
  });
}
//# sourceMappingURL=tangle-catalog-runtime.js.map