"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var common_exports = {};
__export(common_exports, {
  KIROGRAPH_COMMAND: () => KIROGRAPH_COMMAND,
  KIROGRAPH_MCP_ARGS: () => KIROGRAPH_MCP_ARGS,
  KIROGRAPH_SCOPED_TOOLS: () => KIROGRAPH_SCOPED_TOOLS,
  KIROGRAPH_SERVER_NAME: () => KIROGRAPH_SERVER_NAME,
  KIROGRAPH_SYNC_CMD: () => KIROGRAPH_SYNC_CMD,
  KIROGRAPH_TOOLS: () => KIROGRAPH_TOOLS,
  appendImportLine: () => appendImportLine,
  ensureDir: () => ensureDir,
  readJson: () => readJson,
  removeGeneratedBlock: () => removeGeneratedBlock,
  removeImportLine: () => removeImportLine,
  removeMcpServersConfig: () => removeMcpServersConfig,
  upsertGeneratedBlock: () => upsertGeneratedBlock,
  writeJson: () => writeJson,
  writeMcpServersConfig: () => writeMcpServersConfig
});
module.exports = __toCommonJS(common_exports);
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
var import_tool_names = require("../../mcp/tool-names");
const KIROGRAPH_SERVER_NAME = "kirograph";
const KIROGRAPH_COMMAND = "kirograph";
const KIROGRAPH_MCP_ARGS = ["serve", "--mcp"];
const KIROGRAPH_SYNC_CMD = "kirograph sync-if-dirty --quiet 2>/dev/null || true";
const KIROGRAPH_TOOLS = import_tool_names.KIROGRAPH_TOOL_NAMES;
const KIROGRAPH_SCOPED_TOOLS = import_tool_names.KIROGRAPH_TOOL_NAMES.map((name) => `@kirograph/${name}`);
function ensureDir(p) {
  fs.mkdirSync(p, { recursive: true });
}
function readJson(p) {
  try {
    return JSON.parse(fs.readFileSync(p, "utf8"));
  } catch {
    return {};
  }
}
function writeJson(p, data) {
  fs.writeFileSync(p, JSON.stringify(data, null, 2) + "\n");
}
function writeMcpServersConfig(configPath, serverConfig) {
  ensureDir(path.dirname(configPath));
  const existing = readJson(configPath);
  existing.mcpServers = existing.mcpServers ?? {};
  existing.mcpServers[KIROGRAPH_SERVER_NAME] = serverConfig;
  writeJson(configPath, existing);
}
function removeMcpServersConfig(configPath) {
  if (!fs.existsSync(configPath)) return false;
  const existing = readJson(configPath);
  if (!existing.mcpServers?.[KIROGRAPH_SERVER_NAME]) return false;
  delete existing.mcpServers[KIROGRAPH_SERVER_NAME];
  writeJson(configPath, existing);
  return true;
}
function appendImportLine(filePath, line, heading) {
  const existing = fs.existsSync(filePath) ? fs.readFileSync(filePath, "utf8") : "";
  if (existing.includes(line)) return false;
  const prefix = existing.length > 0 && !existing.endsWith("\n") ? "\n" : "";
  const separator = existing.trim().length > 0 ? "\n" : "";
  fs.writeFileSync(filePath, existing + prefix + separator + heading + "\n" + line + "\n");
  return true;
}
function removeImportLine(filePath, line) {
  if (!fs.existsSync(filePath)) return false;
  const original = fs.readFileSync(filePath, "utf8");
  const next = original.split("\n").filter((l) => l.trim() !== line).join("\n").replace(/\n{3,}/g, "\n\n");
  if (next === original) return false;
  fs.writeFileSync(filePath, next.endsWith("\n") ? next : next + "\n");
  return true;
}
function upsertGeneratedBlock(filePath, blockId, heading, content) {
  const start = `<!-- kirograph:${blockId}:start -->`;
  const end = `<!-- kirograph:${blockId}:end -->`;
  const block = `${start}
${heading}

${content.trim()}
${end}`;
  const existing = fs.existsSync(filePath) ? fs.readFileSync(filePath, "utf8") : "";
  const pattern = new RegExp(`${escapeRegExp(start)}[\\s\\S]*?${escapeRegExp(end)}`);
  if (pattern.test(existing)) {
    const next = existing.replace(pattern, block);
    if (next === existing) return false;
    fs.writeFileSync(filePath, next.endsWith("\n") ? next : next + "\n");
    return true;
  }
  const prefix = existing.length > 0 && !existing.endsWith("\n") ? "\n" : "";
  const separator = existing.trim().length > 0 ? "\n" : "";
  fs.writeFileSync(filePath, existing + prefix + separator + block + "\n");
  return true;
}
function removeGeneratedBlock(filePath, blockId) {
  if (!fs.existsSync(filePath)) return false;
  const start = `<!-- kirograph:${blockId}:start -->`;
  const end = `<!-- kirograph:${blockId}:end -->`;
  const original = fs.readFileSync(filePath, "utf8");
  const pattern = new RegExp(`\\n?${escapeRegExp(start)}[\\s\\S]*?${escapeRegExp(end)}\\n?`);
  const next = original.replace(pattern, "\n").replace(/\n{3,}/g, "\n\n");
  if (next === original) return false;
  fs.writeFileSync(filePath, next.endsWith("\n") ? next : next + "\n");
  return true;
}
function escapeRegExp(s) {
  return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  KIROGRAPH_COMMAND,
  KIROGRAPH_MCP_ARGS,
  KIROGRAPH_SCOPED_TOOLS,
  KIROGRAPH_SERVER_NAME,
  KIROGRAPH_SYNC_CMD,
  KIROGRAPH_TOOLS,
  appendImportLine,
  ensureDir,
  readJson,
  removeGeneratedBlock,
  removeImportLine,
  removeMcpServersConfig,
  upsertGeneratedBlock,
  writeJson,
  writeMcpServersConfig
});
//# sourceMappingURL=common.js.map
