"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 devin_exports = {};
__export(devin_exports, {
  installDevinEarly: () => installDevinEarly,
  installDevinLate: () => installDevinLate,
  printDevinNextSteps: () => printDevinNextSteps,
  uninitDevin: () => uninitDevin
});
module.exports = __toCommonJS(devin_exports);
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
var import_common = require("../common");
var import_instructions = require("../instructions");
const DEVIN_BLOCK_ID = "devin";
function buildDevinHooks() {
  return {
    hooks: {
      Stop: [
        {
          matcher: "",
          hooks: [
            {
              type: "command",
              command: "kirograph sync --quiet 2>/dev/null || true",
              timeout: 30
            }
          ]
        }
      ]
    }
  };
}
function installDevinEarly(projectRoot) {
  const configPath = path.join(projectRoot, ".devin", "config.json");
  (0, import_common.ensureDir)(path.dirname(configPath));
  const config = (0, import_common.readJson)(configPath);
  config.mcpServers = config.mcpServers ?? {};
  config.mcpServers[import_common.KIROGRAPH_SERVER_NAME] = {
    command: import_common.KIROGRAPH_COMMAND,
    args: import_common.KIROGRAPH_MCP_ARGS
  };
  (0, import_common.writeJson)(configPath, config);
  console.log(`  \u2713 Devin MCP server registered in ${configPath}`);
}
function installDevinLate(projectRoot, cavemanMode, shellCompressionLevel, enableMemory) {
  const opts = (0, import_common.buildInstructionOpts)(cavemanMode, shellCompressionLevel, enableMemory, true);
  const instructionsPath = path.join(projectRoot, ".kirograph", "devin.md");
  (0, import_common.ensureDir)(path.dirname(instructionsPath));
  fs.writeFileSync(instructionsPath, (0, import_instructions.buildAgentInstructions)(opts));
  console.log(`  \u2713 Devin instructions written to ${instructionsPath}`);
  const agentsPath = path.join(projectRoot, "AGENTS.md");
  const changed = (0, import_common.upsertGeneratedBlock)(agentsPath, DEVIN_BLOCK_ID, "## KiroGraph", (0, import_instructions.buildAgentInstructions)(opts));
  console.log(changed ? `  \u2713 AGENTS.md updated with KiroGraph instructions (Devin)` : `  \u2713 AGENTS.md already up to date`);
  const hooksPath = path.join(projectRoot, ".devin", "hooks.v1.json");
  const existing = (0, import_common.readJson)(hooksPath);
  const kgHooks = buildDevinHooks();
  existing.hooks = existing.hooks ?? {};
  for (const [event, matchers] of Object.entries(kgHooks.hooks)) {
    existing.hooks[event] = existing.hooks[event] ?? [];
    const hasKirograph = existing.hooks[event].some(
      (m) => m.hooks?.some((h) => h.command?.includes("kirograph"))
    );
    if (!hasKirograph) {
      for (const matcher of matchers) {
        existing.hooks[event].push(matcher);
      }
    }
  }
  (0, import_common.writeJson)(hooksPath, existing);
  console.log(`  \u2713 Devin hooks written to ${hooksPath}`);
}
function uninitDevin(projectRoot) {
  const configPath = path.join(projectRoot, ".devin", "config.json");
  if (fs.existsSync(configPath)) {
    const config = (0, import_common.readJson)(configPath);
    if (config.mcpServers?.[import_common.KIROGRAPH_SERVER_NAME]) {
      delete config.mcpServers[import_common.KIROGRAPH_SERVER_NAME];
      if (Object.keys(config.mcpServers).length === 0) delete config.mcpServers;
      (0, import_common.writeJson)(configPath, config);
      console.log(`  \u2713 Removed kirograph from .devin/config.json`);
    }
  }
  const agentsPath = path.join(projectRoot, "AGENTS.md");
  if ((0, import_common.removeGeneratedBlock)(agentsPath, DEVIN_BLOCK_ID)) {
    console.log(`  \u2713 Removed KiroGraph block from AGENTS.md (Devin)`);
  }
  const hooksPath = path.join(projectRoot, ".devin", "hooks.v1.json");
  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(
          (m) => !m.hooks?.some((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 .devin/hooks.v1.json`);
      }
    }
  }
}
function printDevinNextSteps() {
  console.log("\n  Done! Restart Devin for Terminal for the MCP server and hooks to load.");
  console.log("  MCP is in .devin/config.json");
  console.log("  Auto-sync hook is in .devin/hooks.v1.json");
  console.log("  KiroGraph instructions are in AGENTS.md\n");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  installDevinEarly,
  installDevinLate,
  printDevinNextSteps,
  uninitDevin
});
//# sourceMappingURL=devin.js.map
