"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 copilot_exports = {};
__export(copilot_exports, {
  installCopilotEarly: () => installCopilotEarly,
  installCopilotLate: () => installCopilotLate,
  printCopilotNextSteps: () => printCopilotNextSteps,
  uninitCopilot: () => uninitCopilot
});
module.exports = __toCommonJS(copilot_exports);
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
var import_common = require("../common");
var import_instructions = require("../instructions");
const COPILOT_BLOCK_ID = "copilot";
function buildCopilotHooks() {
  return {
    hooks: {
      "session-end": [
        { command: "kirograph sync --quiet 2>/dev/null || true" }
      ]
    }
  };
}
function installCopilotEarly(projectRoot) {
  const mcpPath = path.join(projectRoot, ".github", "copilot-mcp.json");
  (0, import_common.writeMcpServersConfig)(mcpPath, {
    command: import_common.KIROGRAPH_COMMAND,
    args: import_common.KIROGRAPH_MCP_ARGS
  });
  console.log(`  \u2713 GitHub Copilot MCP server registered in ${mcpPath}`);
}
function installCopilotLate(projectRoot, cavemanMode, shellCompressionLevel, enableMemory) {
  const opts = (0, import_common.buildInstructionOpts)(cavemanMode, shellCompressionLevel, enableMemory, true);
  const instructionsPath = path.join(projectRoot, ".kirograph", "copilot.md");
  (0, import_common.ensureDir)(path.dirname(instructionsPath));
  fs.writeFileSync(instructionsPath, (0, import_instructions.buildAgentInstructions)(opts));
  console.log(`  \u2713 Copilot instructions written to ${instructionsPath}`);
  const rulesPath = path.join(projectRoot, ".github", "copilot-instructions.md");
  (0, import_common.ensureDir)(path.dirname(rulesPath));
  const changed = (0, import_common.upsertGeneratedBlock)(rulesPath, COPILOT_BLOCK_ID, "## KiroGraph", (0, import_instructions.buildAgentInstructions)(opts));
  console.log(changed ? `  \u2713 .github/copilot-instructions.md updated with KiroGraph instructions` : `  \u2713 .github/copilot-instructions.md already up to date`);
  const hooksPath = path.join(projectRoot, ".github", "hooks.json");
  const existing = (0, import_common.readJson)(hooksPath);
  const kgHooks = buildCopilotHooks();
  existing.hooks = existing.hooks ?? {};
  for (const [event, commands] of Object.entries(kgHooks.hooks)) {
    existing.hooks[event] = existing.hooks[event] ?? [];
    for (const cmd of commands) {
      if (!existing.hooks[event].some((h) => h.command === cmd.command)) {
        existing.hooks[event].push(cmd);
      }
    }
  }
  (0, import_common.writeJson)(hooksPath, existing);
  console.log(`  \u2713 Copilot hooks written to ${hooksPath}`);
}
function uninitCopilot(projectRoot) {
  const mcpPath = path.join(projectRoot, ".github", "copilot-mcp.json");
  if ((0, import_common.removeMcpServersConfig)(mcpPath)) {
    console.log(`  \u2713 Removed kirograph from .github/copilot-mcp.json`);
  }
  const rulesPath = path.join(projectRoot, ".github", "copilot-instructions.md");
  if ((0, import_common.removeGeneratedBlock)(rulesPath, COPILOT_BLOCK_ID)) {
    console.log(`  \u2713 Removed KiroGraph block from .github/copilot-instructions.md`);
  }
  const hooksPath = path.join(projectRoot, ".github", "hooks.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((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 .github/hooks.json`);
      }
    }
  }
}
function printCopilotNextSteps() {
  console.log("\n  Done! Restart your editor for the Copilot MCP server and hooks to load.");
  console.log("  Auto-sync hook runs on session end.\n");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  installCopilotEarly,
  installCopilotLate,
  printCopilotNextSteps,
  uninitCopilot
});
//# sourceMappingURL=copilot.js.map
