"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 cursor_exports = {};
__export(cursor_exports, {
  installCursorEarly: () => installCursorEarly,
  installCursorLate: () => installCursorLate,
  printCursorNextSteps: () => printCursorNextSteps,
  uninitCursor: () => uninitCursor
});
module.exports = __toCommonJS(cursor_exports);
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
var import_common = require("../common");
var import_instructions = require("../instructions");
const CURSOR_RULES_FILE = "kirograph.mdc";
const CURSOR_HOOKS_FILE = "hooks.json";
function buildCursorHooks(enableCompression) {
  const hooks = {
    stop: [
      { command: "kirograph sync --quiet 2>/dev/null || true" }
    ]
  };
  if (enableCompression) {
    hooks.beforeShellExecution = [
      { command: "kirograph compress-hint 2>/dev/null || true" }
    ];
  }
  return { version: 1, hooks };
}
function installCursorEarly(projectRoot) {
  const mcpPath = path.join(projectRoot, ".cursor", "mcp.json");
  (0, import_common.writeMcpServersConfig)(mcpPath, {
    command: import_common.KIROGRAPH_COMMAND,
    args: import_common.KIROGRAPH_MCP_ARGS
  });
  console.log(`  \u2713 Cursor MCP server registered in ${mcpPath}`);
}
function installCursorLate(projectRoot, cavemanMode, shellCompressionLevel, enableMemory) {
  const enableCompression = shellCompressionLevel !== void 0 && shellCompressionLevel !== "off";
  const opts = (0, import_common.buildInstructionOpts)(cavemanMode, shellCompressionLevel, enableMemory, true);
  const instructionsPath = path.join(projectRoot, ".kirograph", "cursor.md");
  (0, import_common.ensureDir)(path.dirname(instructionsPath));
  fs.writeFileSync(instructionsPath, (0, import_instructions.buildAgentInstructions)(opts));
  console.log(`  \u2713 Cursor instructions written to ${instructionsPath}`);
  const rulesDir = path.join(projectRoot, ".cursor", "rules");
  (0, import_common.ensureDir)(rulesDir);
  const rulePath = path.join(rulesDir, CURSOR_RULES_FILE);
  const frontmatter = [
    "---",
    "description: KiroGraph semantic code knowledge graph \u2014 use graph tools instead of grep/glob",
    "alwaysApply: true",
    "---",
    ""
  ].join("\n");
  fs.writeFileSync(rulePath, frontmatter + (0, import_instructions.buildAgentInstructions)(opts));
  console.log(`  \u2713 Cursor rule written to ${rulePath}`);
  const hooksPath = path.join(projectRoot, ".cursor", CURSOR_HOOKS_FILE);
  const existing = (0, import_common.readJson)(hooksPath);
  const kirographHooks = buildCursorHooks(enableCompression);
  const merged = { ...existing, ...kirographHooks };
  if (existing.hooks) {
    merged.hooks = { ...existing.hooks };
    const kgHooks = kirographHooks.hooks;
    for (const [event, commands] of Object.entries(kgHooks)) {
      merged.hooks[event] = merged.hooks[event] ?? [];
      for (const cmd of commands) {
        if (!merged.hooks[event].some((h) => h.command === cmd.command)) {
          merged.hooks[event].push(cmd);
        }
      }
    }
  }
  (0, import_common.writeJson)(hooksPath, merged);
  console.log(`  \u2713 Cursor hooks written to ${hooksPath}`);
}
function uninitCursor(projectRoot) {
  const mcpPath = path.join(projectRoot, ".cursor", "mcp.json");
  if ((0, import_common.removeMcpServersConfig)(mcpPath)) {
    console.log(`  \u2713 Removed kirograph from .cursor/mcp.json`);
  }
  const rulePath = path.join(projectRoot, ".cursor", "rules", CURSOR_RULES_FILE);
  if (fs.existsSync(rulePath)) {
    fs.unlinkSync(rulePath);
    console.log(`  \u2713 Removed .cursor/rules/${CURSOR_RULES_FILE}`);
  }
  const hooksPath = path.join(projectRoot, ".cursor", CURSOR_HOOKS_FILE);
  if (fs.existsSync(hooksPath)) {
    const config = (0, import_common.readJson)(hooksPath);
    if (config.hooks) {
      let changed = false;
      for (const event of Object.keys(config.hooks)) {
        const before = config.hooks[event].length;
        config.hooks[event] = config.hooks[event].filter((h) => !h.command?.includes("kirograph"));
        if (config.hooks[event].length === 0) delete config.hooks[event];
        if (config.hooks[event]?.length !== before) changed = true;
      }
      if (Object.keys(config.hooks).length === 0) delete config.hooks;
      if (changed) {
        (0, import_common.writeJson)(hooksPath, config);
        console.log(`  \u2713 Removed kirograph hooks from .cursor/hooks.json`);
      }
    }
  }
}
function printCursorNextSteps() {
  console.log("\n  Done! Restart Cursor for the MCP server and hooks to load.");
  console.log("  The kirograph rule is active in .cursor/rules/kirograph.mdc");
  console.log("  Auto-sync hook runs on task completion.\n");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  installCursorEarly,
  installCursorLate,
  printCursorNextSteps,
  uninitCursor
});
//# sourceMappingURL=cursor.js.map
