"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 hooks_exports = {};
__export(hooks_exports, {
  writeHooks: () => writeHooks
});
module.exports = __toCommonJS(hooks_exports);
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
var import_errors = require("../../errors");
const HOOK_EXT = ".kiro.hook";
const HOOKS = [
  {
    filename: `kirograph-sync-if-dirty${HOOK_EXT}`,
    hook: {
      name: "KiroGraph Sync on Agent Stop",
      version: "1.0.0",
      description: "Sync the KiroGraph index when the agent stops, picking up any file edits, creates, or deletes from the session.",
      when: { type: "agentStop" },
      then: { type: "runCommand", command: "kirograph sync --quiet 2>&1 > /dev/null" }
    }
  },
  {
    filename: `kirograph-compress-hint${HOOK_EXT}`,
    hook: {
      name: "KiroGraph Compression Hint",
      version: "1.0.0",
      description: "Remind the agent to use kirograph_exec for shell commands that benefit from token compression (git, gh, test, lint, build, docker, aws, grep).",
      when: { type: "preToolUse", toolTypes: ["shell"] },
      then: {
        type: "askAgent",
        prompt: "If this shell command is a git operation, GitHub CLI, test runner, linter, build tool, file listing, grep/rg, docker/kubectl, AWS CLI, or package manager command, consider using the kirograph_exec MCP tool instead for 60-90% token savings. The tool compresses output automatically while preserving error details."
      }
    }
  },
  {
    filename: `kirograph-mem-capture${HOOK_EXT}`,
    hook: {
      name: "KiroGraph Memory Capture",
      version: "1.0.0",
      description: "Prompt the agent to store important observations in memory at the end of each session.",
      when: { type: "agentStop" },
      then: {
        type: "askAgent",
        prompt: "Before ending, review what happened in this session. If there were any important decisions, bug root causes, architecture insights, error patterns, or lessons learned, store them using kirograph_mem_store with the appropriate kind (decision, error, pattern, architecture, note). Keep observations concise \u2014 one fact per observation. Skip if nothing noteworthy happened."
      }
    }
  }
];
function ensureDir(p) {
  fs.mkdirSync(p, { recursive: true });
}
function writeJson(p, data) {
  fs.writeFileSync(p, JSON.stringify(data, null, 2) + "\n");
}
function migrateOnIdleHooks(hooksDir) {
  if (!fs.existsSync(hooksDir)) return;
  let files;
  try {
    files = fs.readdirSync(hooksDir).filter((f) => f.endsWith(".json") || f.endsWith(HOOK_EXT));
  } catch {
    return;
  }
  for (const file of files) {
    const filePath = path.join(hooksDir, file);
    let raw;
    try {
      raw = fs.readFileSync(filePath, "utf8");
    } catch {
      (0, import_errors.logWarn)(`KiroGraph installer: could not read hook file ${filePath}`);
      continue;
    }
    let obj;
    try {
      obj = JSON.parse(raw);
    } catch {
      (0, import_errors.logWarn)(`KiroGraph installer: could not parse hook file ${filePath}`);
      continue;
    }
    let changed = false;
    if (obj?.when?.type === "onIdle") {
      obj.when.type = "agentStop";
      changed = true;
    }
    if (file.endsWith(".json") && file.startsWith("kirograph-")) {
      const newName = file.replace(/\.json$/, HOOK_EXT);
      const newPath = path.join(hooksDir, newName);
      try {
        fs.writeFileSync(newPath, JSON.stringify(obj, null, 2) + "\n");
        fs.unlinkSync(filePath);
      } catch {
        (0, import_errors.logWarn)(`KiroGraph installer: could not migrate hook file ${filePath} \u2192 ${newName}`);
      }
    } else if (changed) {
      try {
        fs.writeFileSync(filePath, JSON.stringify(obj, null, 2) + "\n");
      } catch {
        (0, import_errors.logWarn)(`KiroGraph installer: could not write migrated hook file ${filePath}`);
      }
    }
  }
}
function writeHooks(kiroDir, opts) {
  const hooksDir = path.join(kiroDir, "hooks");
  ensureDir(hooksDir);
  migrateOnIdleHooks(hooksDir);
  const oldHooks = [
    "kirograph-sync-on-save.json",
    "kirograph-sync-on-create.json",
    // Legacy .json versions (migrated to .kiro.hook)
    "kirograph-mark-dirty-on-save.json",
    "kirograph-mark-dirty-on-create.json",
    "kirograph-sync-on-delete.json",
    "kirograph-sync-if-dirty.json",
    "kirograph-compress-hint.json",
    "kirograph-mem-capture.json",
    // Removed per-file hooks (consolidated into agentStop sync)
    `kirograph-mark-dirty-on-save${HOOK_EXT}`,
    `kirograph-mark-dirty-on-create${HOOK_EXT}`,
    `kirograph-sync-on-delete${HOOK_EXT}`
  ];
  for (const old of oldHooks) {
    const p = path.join(hooksDir, old);
    if (fs.existsSync(p)) fs.unlinkSync(p);
  }
  for (const { filename, hook } of HOOKS) {
    if (filename === `kirograph-compress-hint${HOOK_EXT}` && opts?.enableCompression === false) {
      const p = path.join(hooksDir, filename);
      if (fs.existsSync(p)) fs.unlinkSync(p);
      continue;
    }
    if (filename === `kirograph-mem-capture${HOOK_EXT}` && !opts?.enableMemory) {
      const p = path.join(hooksDir, filename);
      if (fs.existsSync(p)) fs.unlinkSync(p);
      continue;
    }
    writeJson(path.join(hooksDir, filename), hook);
  }
  console.log(`  \u2713 Auto-sync hooks written to ${hooksDir}`);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  writeHooks
});
//# sourceMappingURL=hooks.js.map
