if (typeof process === 'object' && process !== null &&
    process.env !== null && typeof process.env === 'object') {
  process.env.DD_GIT_REPOSITORY_URL = 'https://token@github.com/superblocksteam/superblocks.git';
  process.env.DD_GIT_COMMIT_SHA = '71329cb2a26133447acee36261ec5a5145024e84';
}
import { createRequire as $dd_createRequire } from 'module';
import { fileURLToPath as $dd_fileURLToPath } from 'url';
import { dirname as $dd_dirname } from 'path';
globalThis.require ??= $dd_createRequire(import.meta.url);
globalThis.__filename ??= $dd_fileURLToPath(import.meta.url);
globalThis.__dirname ??= $dd_dirname(globalThis.__filename);

import {
  require_lib
} from "./chunk-DNVZMNUA.js";
import {
  __toESM,
  init_cjs_shims
} from "./chunk-MTQ2JLIW.js";

// ../mcp-server/dist/setup/commands.js
init_cjs_shims();

// ../mcp-server/dist/utils/args.js
init_cjs_shims();
function getFlagValue(args, flagName) {
  const flagIndex = args.findIndex((arg) => arg === flagName || arg.startsWith(`${flagName}=`));
  if (flagIndex === -1) {
    return void 0;
  }
  const matchedArg = args[flagIndex];
  if (matchedArg.includes("=")) {
    return matchedArg.slice(flagName.length + 1);
  }
  return args[flagIndex + 1];
}
function hasFlag(args, flagName) {
  return args.includes(flagName);
}

// ../mcp-server/dist/setup/setup-config.js
init_cjs_shims();
var import_fs_extra = __toESM(require_lib());
import { execFileSync } from "node:child_process";
import os from "node:os";
import path from "node:path";
var CLIENT_CONFIG_PATHS = {
  claude: path.join(os.homedir(), ".claude.json"),
  cursor: path.join(os.homedir(), ".cursor", "mcp.json")
};
var CLIENT_LABELS = {
  claude: "Claude Code (~/.claude.json)",
  cursor: "Cursor (~/.cursor/mcp.json)",
  generic: "Generic MCP client"
};
function whichBinary(name) {
  try {
    return execFileSync("which", [name], {
      encoding: "utf-8",
      stdio: ["ignore", "pipe", "ignore"]
    }).trim();
  } catch {
    return void 0;
  }
}
function buildMcpEntry(options = {}) {
  const mcpBin = (options.resolveBinary ?? (() => whichBinary("superblocks-mcp")))();
  if (mcpBin) {
    return { command: mcpBin, args: [] };
  }
  const cliBin = (options.resolveCliBinary ?? (() => whichBinary("superblocks")))();
  if (cliBin) {
    return { command: cliBin, args: ["mcp", "serve"] };
  }
  return {
    command: "npx",
    args: [
      "-y",
      "--registry=https://registry.npmjs.org",
      "@superblocksteam/mcp-server@latest"
    ]
  };
}
function configPathForClient(client) {
  return CLIENT_CONFIG_PATHS[client];
}
function labelForClient(client) {
  return CLIENT_LABELS[client] ?? client;
}
function deepEqual(a, b) {
  if (a === b)
    return true;
  if (typeof a !== "object" || typeof b !== "object" || !a || !b)
    return false;
  const keysA = Object.keys(a);
  const keysB = Object.keys(b);
  if (keysA.length !== keysB.length)
    return false;
  return keysA.every((k) => deepEqual(a[k], b[k]));
}
async function mergeMcpConfig(configPath, options = {}) {
  const entry = buildMcpEntry({
    resolveBinary: options.resolveBinary,
    resolveCliBinary: options.resolveCliBinary
  });
  let existing;
  try {
    existing = await import_fs_extra.default.readJson(configPath);
  } catch (err) {
    const code = err.code;
    if (code !== "ENOENT" && !(err instanceof SyntaxError)) {
      throw new Error(`Cannot read MCP config at ${configPath}: ${err instanceof Error ? err.message : String(err)}`);
    }
  }
  let action;
  if (!existing || typeof existing !== "object") {
    existing = { mcpServers: {} };
    action = "created";
  } else if (!existing.mcpServers || typeof existing.mcpServers !== "object") {
    existing.mcpServers = {};
    action = "created";
  } else if (deepEqual(existing.mcpServers.superblocks, entry)) {
    action = "already_current";
  } else if (existing.mcpServers.superblocks) {
    action = "updated";
  } else {
    action = "merged";
  }
  existing.mcpServers.superblocks = entry;
  if (!options.dryRun && action !== "already_current") {
    await import_fs_extra.default.ensureDir(path.dirname(configPath));
    await import_fs_extra.default.writeJson(configPath, existing, { spaces: 2 });
  }
  return { action, config: existing };
}

// ../mcp-server/dist/setup/commands.js
var VALID_CLIENTS = /* @__PURE__ */ new Set(["claude", "cursor", "generic"]);
async function maybeRunSetupCommand(args, options = {}) {
  if (args.length === 0 || args[0] !== "setup") {
    return false;
  }
  const subArgs = args.slice(1);
  const clientRaw = getFlagValue(subArgs, "--client") ?? "generic";
  if (!VALID_CLIENTS.has(clientRaw)) {
    throw new Error(`Unknown client '${clientRaw}'. Valid options: ${[...VALID_CLIENTS].join(", ")}`);
  }
  const client = clientRaw;
  const dryRun = hasFlag(subArgs, "--dry-run");
  const configPath = options.configPathOverride ?? configPathForClient(client);
  if (!configPath) {
    console.log(`# MCP configuration for ${labelForClient(client)}`);
    console.log("# Copy this into your MCP client configuration file:\n");
    console.log(JSON.stringify({
      mcpServers: {
        superblocks: buildMcpEntry({
          resolveBinary: options.resolveBinary,
          resolveCliBinary: options.resolveCliBinary
        })
      }
    }, null, 2));
    return true;
  }
  const { action, config } = await mergeMcpConfig(configPath, {
    dryRun,
    resolveBinary: options.resolveBinary,
    resolveCliBinary: options.resolveCliBinary
  });
  if (dryRun) {
    console.log(`# MCP configuration for ${labelForClient(client)} (dry run)`);
    console.log(`# Would write to: ${configPath}
`);
    console.log(JSON.stringify(config, null, 2));
    return true;
  }
  switch (action) {
    case "created":
      console.log(`Wrote Superblocks MCP config to ${configPath}`);
      break;
    case "merged":
      console.log(`Added Superblocks MCP server to ${configPath}`);
      break;
    case "updated":
      console.log(`Updated Superblocks MCP server config in ${configPath}`);
      break;
    case "already_current":
      console.log(`Superblocks MCP server is already configured in ${configPath}`);
      return true;
  }
  console.log(`Restart ${labelForClient(client)} to activate the MCP server.`);
  return true;
}

export {
  getFlagValue,
  buildMcpEntry,
  configPathForClient,
  labelForClient,
  mergeMcpConfig,
  maybeRunSetupCommand
};
//# sourceMappingURL=chunk-AE2RQW4A.js.map
