import {
  FragmentCommand,
  require_lib
} from "./chunk-THGLXMNF.js";
import {
  require_source
} from "./chunk-XK2D2Z44.js";
import {
  __toESM,
  init_cjs_shims
} from "./chunk-7GH3YGSC.js";

// src/commands/designer-mcp.ts
init_cjs_shims();
var import_core = __toESM(require_lib(), 1);
var import_chalk = __toESM(require_source(), 1);
var DesignerMcp = class _DesignerMcp extends FragmentCommand {
  static {
    this.description = "Connect your LLM to the Fragment Schema designer (experimental)";
  }
  static {
    this.examples = [
      "<%= config.bin %> <%= command.id %>",
      "<%= config.bin %> <%= command.id %> --port 4321",
      "<%= config.bin %> <%= command.id %> --host 127.0.0.1 --port 5000"
    ];
  }
  static {
    this.flags = {
      port: import_core.Flags.integer({
        char: "p",
        description: "Port to bind the HTTP server on",
        default: 4321
      }),
      host: import_core.Flags.string({
        description: "Host to bind the HTTP server on",
        default: "127.0.0.1"
      }),
      "allowed-origin": import_core.Flags.string({
        description: "Additional CORS origin to allow (repeatable). Localhost is always allowed.",
        multiple: true
      })
    };
  }
  // Skip version check and analytics — this is a long-running local dev server.
  async init() {
    if (process.env.ENABLE_DESIGNER_MCP !== "true") {
      this.error("This feature is in development and not currently available.");
    }
    process.env.FRAGMENT_SKIP_AUTO_UPDATE = "true";
    process.env.DISABLE_CLI_ANALYTICS = "true";
    await super.init();
  }
  async run() {
    const { flags } = await this.parse(_DesignerMcp);
    let startDesignerMcpServer;
    try {
      const mod = await import("./designer/server.js");
      startDesignerMcpServer = mod.startDesignerMcpServer;
    } catch (err) {
      if (err instanceof Error) {
        this.logToStderr(`Failed to load designer MCP server: ${err.message}`);
      } else {
        console.error(err);
        this.logToStderr("Failed to load designer MCP server module.");
      }
      this.exit(1);
      return;
    }
    const handle = await startDesignerMcpServer({
      port: flags.port,
      host: flags.host,
      allowedOrigins: flags["allowed-origin"] ?? [],
      logger: {
        log: (...args) => this.log(
          ...args.map(
            (a) => a instanceof Error ? a.message : typeof a === "object" ? JSON.stringify(a) : String(a)
          )
        ),
        error: (...args) => this.logToStderr(
          ...args.map(
            (a) => a instanceof Error ? a.message : typeof a === "object" ? JSON.stringify(a) : String(a)
          )
        )
      }
    });
    this.log("");
    this.log(import_chalk.default.green("\u2705 Fragment Designer MCP server running"));
    this.log("");
    this.log(`  MCP endpoint:       ${import_chalk.default.cyan(`${handle.url}/mcp`)}`);
    this.log(`  Dashboard bridge:   ${import_chalk.default.cyan(`${handle.url}/bridge`)}`);
    this.log(`  Health check:       ${import_chalk.default.cyan(`${handle.url}/health`)}`);
    this.log("");
    this.log(
      "Open the schema designer in the dashboard \u2014 it will auto-connect to this server."
    );
    this.log("");
    this.log(
      `Configure your MCP client (Claude Desktop, Cursor, etc.) with the URL ${import_chalk.default.cyan(
        `${handle.url}/mcp`
      )}.`
    );
    this.log("");
    this.log(import_chalk.default.gray("Press Ctrl+C to stop."));
    const shutdown = (signal) => {
      this.log(`
Received ${signal}, shutting down...`);
      handle.close().finally(() => process.exit(0));
      setTimeout(() => process.exit(0), 500).unref();
    };
    process.on("SIGINT", () => shutdown("SIGINT"));
    process.on("SIGTERM", () => shutdown("SIGTERM"));
    await new Promise(() => {
    });
  }
};

export {
  DesignerMcp
};
