"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 windsurf_exports = {};
__export(windsurf_exports, {
  installWindsurfEarly: () => installWindsurfEarly,
  installWindsurfLate: () => installWindsurfLate,
  printWindsurfNextSteps: () => printWindsurfNextSteps,
  uninitWindsurf: () => uninitWindsurf
});
module.exports = __toCommonJS(windsurf_exports);
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
var import_common = require("../common");
var import_instructions = require("../instructions");
const WINDSURF_RULES_FILE = "kirograph.md";
function buildWindsurfHooks() {
  return {
    hooks: {
      post_cascade_response: [
        { command: "kirograph sync --quiet 2>/dev/null || true", show_output: false }
      ]
    }
  };
}
function buildWindsurfRule(instructions) {
  const frontmatter = [
    "---",
    "trigger: always_on",
    "---",
    ""
  ].join("\n");
  return frontmatter + instructions;
}
function installWindsurfEarly(_projectRoot) {
}
function installWindsurfLate(projectRoot, cavemanMode, shellCompressionLevel, enableMemory) {
  const opts = (0, import_common.buildInstructionOpts)(cavemanMode, shellCompressionLevel, enableMemory, true);
  const instructionsPath = path.join(projectRoot, ".kirograph", "windsurf.md");
  (0, import_common.ensureDir)(path.dirname(instructionsPath));
  fs.writeFileSync(instructionsPath, (0, import_instructions.buildAgentInstructions)(opts));
  console.log(`  \u2713 Windsurf instructions written to ${instructionsPath}`);
  const rulesDir = path.join(projectRoot, ".windsurf", "rules");
  (0, import_common.ensureDir)(rulesDir);
  const rulePath = path.join(rulesDir, WINDSURF_RULES_FILE);
  fs.writeFileSync(rulePath, buildWindsurfRule((0, import_instructions.buildAgentInstructions)(opts)));
  console.log(`  \u2713 Windsurf rule written to ${rulePath}`);
  const legacyPath = path.join(projectRoot, ".windsurfrules");
  if (fs.existsSync(legacyPath)) {
    const content = fs.readFileSync(legacyPath, "utf8");
    if (content.includes("kirograph")) {
      fs.unlinkSync(legacyPath);
      console.log(`  \u2713 Removed legacy .windsurfrules (migrated to .windsurf/rules/)`);
    }
  }
  const hooksPath = path.join(projectRoot, ".windsurf", "hooks.json");
  const existing = (0, import_common.readJson)(hooksPath);
  const kgHooks = buildWindsurfHooks();
  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 Windsurf hooks written to ${hooksPath}`);
}
function uninitWindsurf(projectRoot) {
  const rulePath = path.join(projectRoot, ".windsurf", "rules", WINDSURF_RULES_FILE);
  if (fs.existsSync(rulePath)) {
    fs.unlinkSync(rulePath);
    console.log(`  \u2713 Removed .windsurf/rules/${WINDSURF_RULES_FILE}`);
  }
  const legacyPath = path.join(projectRoot, ".windsurfrules");
  if (fs.existsSync(legacyPath)) {
    const content = fs.readFileSync(legacyPath, "utf8");
    if (content.includes("kirograph")) {
      fs.unlinkSync(legacyPath);
      console.log(`  \u2713 Removed legacy .windsurfrules`);
    }
  }
  const hooksPath = path.join(projectRoot, ".windsurf", "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 .windsurf/hooks.json`);
      }
    }
  }
}
function printWindsurfNextSteps(projectRoot) {
  console.log("\n  Done! KiroGraph rule is in .windsurf/rules/kirograph.md");
  console.log("  Auto-sync hook is in .windsurf/hooks.json");
  (0, import_common.printMcpSetup)("~/.codeium/windsurf/mcp_config.json", projectRoot);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  installWindsurfEarly,
  installWindsurfLate,
  printWindsurfNextSteps,
  uninitWindsurf
});
//# sourceMappingURL=windsurf.js.map
