"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 junie_exports = {};
__export(junie_exports, {
  installJunieEarly: () => installJunieEarly,
  installJunieLate: () => installJunieLate,
  printJunieNextSteps: () => printJunieNextSteps,
  uninitJunie: () => uninitJunie
});
module.exports = __toCommonJS(junie_exports);
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
var import_common = require("../common");
var import_instructions = require("../instructions");
const JUNIE_BLOCK_ID = "junie";
function installJunieEarly(projectRoot) {
  const mcpPath = path.join(projectRoot, ".junie", "mcp", "mcp.json");
  (0, import_common.ensureDir)(path.dirname(mcpPath));
  const config = (0, import_common.readJson)(mcpPath);
  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)(mcpPath, config);
  console.log(`  \u2713 Junie MCP server registered in ${mcpPath}`);
}
function installJunieLate(projectRoot, cavemanMode, shellCompressionLevel, enableMemory) {
  const opts = (0, import_common.buildInstructionOpts)(cavemanMode, shellCompressionLevel, enableMemory);
  const instructionsPath = path.join(projectRoot, ".kirograph", "junie.md");
  (0, import_common.ensureDir)(path.dirname(instructionsPath));
  fs.writeFileSync(instructionsPath, (0, import_instructions.buildAgentInstructions)(opts));
  console.log(`  \u2713 Junie instructions written to ${instructionsPath}`);
  const agentsPath = path.join(projectRoot, ".junie", "AGENTS.md");
  (0, import_common.ensureDir)(path.dirname(agentsPath));
  const changed = (0, import_common.upsertGeneratedBlock)(agentsPath, JUNIE_BLOCK_ID, "## KiroGraph", (0, import_instructions.buildAgentInstructions)(opts));
  console.log(changed ? `  \u2713 .junie/AGENTS.md updated with KiroGraph instructions` : `  \u2713 .junie/AGENTS.md already up to date`);
  const legacyPath = path.join(projectRoot, ".junie", "guidelines.md");
  if (fs.existsSync(legacyPath)) {
    const content = fs.readFileSync(legacyPath, "utf8");
    if (content.includes("kirograph") || content.includes("KiroGraph")) {
      fs.unlinkSync(legacyPath);
      console.log(`  \u2713 Removed legacy .junie/guidelines.md (migrated to .junie/AGENTS.md)`);
    }
  }
}
function uninitJunie(projectRoot) {
  const mcpPath = path.join(projectRoot, ".junie", "mcp", "mcp.json");
  if (fs.existsSync(mcpPath)) {
    const config = (0, import_common.readJson)(mcpPath);
    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)(mcpPath, config);
      console.log(`  \u2713 Removed kirograph from .junie/mcp/mcp.json`);
    }
  }
  const agentsPath = path.join(projectRoot, ".junie", "AGENTS.md");
  if ((0, import_common.removeGeneratedBlock)(agentsPath, JUNIE_BLOCK_ID)) {
    console.log(`  \u2713 Removed KiroGraph block from .junie/AGENTS.md`);
  }
  const legacyPath = path.join(projectRoot, ".junie", "guidelines.md");
  if ((0, import_common.removeGeneratedBlock)(legacyPath, JUNIE_BLOCK_ID)) {
    console.log(`  \u2713 Removed KiroGraph block from .junie/guidelines.md`);
  }
}
function printJunieNextSteps() {
  console.log("\n  Done! Restart Junie for the MCP server to load.");
  console.log("  MCP is in .junie/mcp/mcp.json");
  console.log("  KiroGraph guidelines are in .junie/AGENTS.md\n");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  installJunieEarly,
  installJunieLate,
  printJunieNextSteps,
  uninitJunie
});
//# sourceMappingURL=junie.js.map
