"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 opencode_exports = {};
__export(opencode_exports, {
  installOpenCodeEarly: () => installOpenCodeEarly,
  installOpenCodeLate: () => installOpenCodeLate,
  printOpenCodeNextSteps: () => printOpenCodeNextSteps,
  uninitOpenCode: () => uninitOpenCode
});
module.exports = __toCommonJS(opencode_exports);
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
var import_common = require("../common");
var import_instructions = require("../instructions");
const OPENCODE_CONFIG = ".opencode.json";
const OPENCODE_MCP_NAME = "kirograph";
const OPENCODE_INSTRUCTIONS_PATH = ".kirograph/opencode.md";
const OPENCODE_PLUGIN_FILE = "kirograph-sync.js";
const OPENCODE_PLUGIN_CONTENT = `/**
 * KiroGraph auto-sync plugin for OpenCode.
 * Syncs the KiroGraph index when the agent session goes idle.
 */
export const KirographSync = async ({ $ }) => {
  return {
    event: async ({ event }) => {
      if (event.type === "session.idle") {
        await $\`kirograph sync --quiet 2>/dev/null || true\`
      }
    },
  }
}
`;
function installOpenCodeEarly(projectRoot) {
  const configPath = path.join(projectRoot, OPENCODE_CONFIG);
  const config = (0, import_common.readJson)(configPath);
  config.mcp = config.mcp ?? {};
  config.mcp[OPENCODE_MCP_NAME] = {
    type: "local",
    command: ["kirograph", "serve", "--mcp"],
    enabled: true
  };
  if (!config.$schema) {
    config.$schema = "https://opencode.ai/config.json";
  }
  (0, import_common.writeJson)(configPath, config);
  console.log(`  \u2713 OpenCode MCP server registered in ${configPath}`);
}
function installOpenCodeLate(projectRoot, cavemanMode, shellCompressionLevel, enableMemory) {
  const opts = (0, import_common.buildInstructionOpts)(cavemanMode, shellCompressionLevel, enableMemory, true);
  const instructionsPath = path.join(projectRoot, ".kirograph", "opencode.md");
  (0, import_common.ensureDir)(path.dirname(instructionsPath));
  fs.writeFileSync(instructionsPath, (0, import_instructions.buildAgentInstructions)(opts));
  console.log(`  \u2713 OpenCode instructions written to ${instructionsPath}`);
  const configPath = path.join(projectRoot, OPENCODE_CONFIG);
  const config = (0, import_common.readJson)(configPath);
  const instructions = config.instructions ?? [];
  if (!instructions.includes(OPENCODE_INSTRUCTIONS_PATH)) {
    instructions.push(OPENCODE_INSTRUCTIONS_PATH);
  }
  config.instructions = instructions;
  (0, import_common.writeJson)(configPath, config);
  console.log(`  \u2713 OpenCode instructions referenced in ${configPath}`);
  const pluginsDir = path.join(projectRoot, ".opencode", "plugins");
  (0, import_common.ensureDir)(pluginsDir);
  const pluginPath = path.join(pluginsDir, OPENCODE_PLUGIN_FILE);
  fs.writeFileSync(pluginPath, OPENCODE_PLUGIN_CONTENT);
  console.log(`  \u2713 OpenCode sync plugin written to ${pluginPath}`);
}
function uninitOpenCode(projectRoot) {
  const configPath = path.join(projectRoot, OPENCODE_CONFIG);
  if (fs.existsSync(configPath)) {
    const config = (0, import_common.readJson)(configPath);
    let changed = false;
    if (config.mcp?.[OPENCODE_MCP_NAME]) {
      delete config.mcp[OPENCODE_MCP_NAME];
      if (Object.keys(config.mcp).length === 0) delete config.mcp;
      changed = true;
      console.log(`  \u2713 Removed kirograph from .opencode.json mcp`);
    }
    if (Array.isArray(config.instructions)) {
      const idx = config.instructions.indexOf(OPENCODE_INSTRUCTIONS_PATH);
      if (idx !== -1) {
        config.instructions.splice(idx, 1);
        if (config.instructions.length === 0) delete config.instructions;
        changed = true;
        console.log(`  \u2713 Removed kirograph instructions from .opencode.json`);
      }
    }
    if (changed) (0, import_common.writeJson)(configPath, config);
  }
  const pluginPath = path.join(projectRoot, ".opencode", "plugins", OPENCODE_PLUGIN_FILE);
  if (fs.existsSync(pluginPath)) {
    fs.unlinkSync(pluginPath);
    console.log(`  \u2713 Removed OpenCode sync plugin`);
  }
}
function printOpenCodeNextSteps() {
  console.log("\n  Done! Restart OpenCode for the MCP server and sync plugin to load.");
  console.log("  MCP and instructions are in .opencode.json");
  console.log("  Auto-sync plugin is in .opencode/plugins/kirograph-sync.js\n");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  installOpenCodeEarly,
  installOpenCodeLate,
  printOpenCodeNextSteps,
  uninitOpenCode
});
//# sourceMappingURL=opencode.js.map
