"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 gemini_cli_exports = {};
__export(gemini_cli_exports, {
  installGeminiCliEarly: () => installGeminiCliEarly,
  installGeminiCliLate: () => installGeminiCliLate,
  printGeminiCliNextSteps: () => printGeminiCliNextSteps,
  uninitGeminiCli: () => uninitGeminiCli
});
module.exports = __toCommonJS(gemini_cli_exports);
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
var import_common = require("../common");
var import_instructions = require("../instructions");
const GEMINI_CLI_BLOCK_ID = "gemini-cli";
function installGeminiCliEarly(projectRoot) {
  const settingsPath = path.join(projectRoot, ".gemini", "settings.json");
  (0, import_common.ensureDir)(path.dirname(settingsPath));
  const settings = (0, import_common.readJson)(settingsPath);
  settings.mcpServers = settings.mcpServers ?? {};
  settings.mcpServers[import_common.KIROGRAPH_SERVER_NAME] = {
    command: import_common.KIROGRAPH_COMMAND,
    args: import_common.KIROGRAPH_MCP_ARGS
  };
  (0, import_common.writeJson)(settingsPath, settings);
  console.log(`  \u2713 Gemini CLI MCP server registered in ${settingsPath}`);
}
function installGeminiCliLate(projectRoot, cavemanMode, shellCompressionLevel, enableMemory) {
  const opts = (0, import_common.buildInstructionOpts)(cavemanMode, shellCompressionLevel, enableMemory, true);
  const instructionsPath = path.join(projectRoot, ".kirograph", "gemini-cli.md");
  (0, import_common.ensureDir)(path.dirname(instructionsPath));
  fs.writeFileSync(instructionsPath, (0, import_instructions.buildAgentInstructions)(opts));
  console.log(`  \u2713 Gemini CLI instructions written to ${instructionsPath}`);
  const geminiPath = path.join(projectRoot, "GEMINI.md");
  const changed = (0, import_common.upsertGeneratedBlock)(geminiPath, GEMINI_CLI_BLOCK_ID, "## KiroGraph", (0, import_instructions.buildAgentInstructions)(opts));
  console.log(changed ? `  \u2713 GEMINI.md updated with KiroGraph instructions` : `  \u2713 GEMINI.md already up to date`);
  const settingsPath = path.join(projectRoot, ".gemini", "settings.json");
  const settings = (0, import_common.readJson)(settingsPath);
  settings.hooks = settings.hooks ?? {};
  settings.hooks.SessionEnd = settings.hooks.SessionEnd ?? [];
  const hasKirograph = settings.hooks.SessionEnd.some(
    (m) => m.hooks?.some((h) => h.command?.includes("kirograph"))
  );
  if (!hasKirograph) {
    settings.hooks.SessionEnd.push({
      hooks: [
        {
          type: "command",
          command: "kirograph sync --quiet 2>/dev/null || true",
          timeout: 5e3
        }
      ]
    });
  }
  (0, import_common.writeJson)(settingsPath, settings);
  console.log(`  \u2713 Gemini CLI hooks written to ${settingsPath}`);
}
function uninitGeminiCli(projectRoot) {
  const settingsPath = path.join(projectRoot, ".gemini", "settings.json");
  if (fs.existsSync(settingsPath)) {
    const settings = (0, import_common.readJson)(settingsPath);
    let changed = false;
    if (settings.mcpServers?.[import_common.KIROGRAPH_SERVER_NAME]) {
      delete settings.mcpServers[import_common.KIROGRAPH_SERVER_NAME];
      if (Object.keys(settings.mcpServers).length === 0) delete settings.mcpServers;
      changed = true;
      console.log(`  \u2713 Removed kirograph from .gemini/settings.json mcpServers`);
    }
    if (settings.hooks?.SessionEnd) {
      const before = settings.hooks.SessionEnd.length;
      settings.hooks.SessionEnd = settings.hooks.SessionEnd.filter(
        (m) => !m.hooks?.some((h) => h.command?.includes("kirograph"))
      );
      if (settings.hooks.SessionEnd.length === 0) delete settings.hooks.SessionEnd;
      if (Object.keys(settings.hooks).length === 0) delete settings.hooks;
      if (settings.hooks?.SessionEnd?.length !== before) {
        changed = true;
        console.log(`  \u2713 Removed kirograph hooks from .gemini/settings.json`);
      }
    }
    if (changed) (0, import_common.writeJson)(settingsPath, settings);
  }
  const geminiPath = path.join(projectRoot, "GEMINI.md");
  if ((0, import_common.removeGeneratedBlock)(geminiPath, GEMINI_CLI_BLOCK_ID)) {
    console.log(`  \u2713 Removed KiroGraph block from GEMINI.md`);
  }
}
function printGeminiCliNextSteps() {
  console.log("\n  Done! Restart Gemini CLI for the MCP server and hooks to load.");
  console.log("  MCP and hooks are in .gemini/settings.json");
  console.log("  KiroGraph instructions are in GEMINI.md\n");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  installGeminiCliEarly,
  installGeminiCliLate,
  printGeminiCliNextSteps,
  uninitGeminiCli
});
//# sourceMappingURL=gemini-cli.js.map
