globalThis.__hivemind_tuning__ ??= {};

// openclaw/src/setup-config.ts
import { existsSync, readFileSync, writeFileSync, renameSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
var HIVEMIND_TOOL_NAMES = ["hivemind_search", "hivemind_read", "hivemind_index"];
function getOpenclawConfigPath() {
  return join(homedir(), ".openclaw", "openclaw.json");
}
function isAllowlistCoveringHivemind(alsoAllow) {
  if (!Array.isArray(alsoAllow)) return false;
  for (const entry of alsoAllow) {
    if (typeof entry !== "string") continue;
    const normalized = entry.trim().toLowerCase();
    if (normalized === "hivemind") return true;
    if (normalized === "group:plugins") return true;
    if (HIVEMIND_TOOL_NAMES.includes(normalized)) return true;
  }
  return false;
}
function ensureHivemindAllowlisted() {
  const configPath = getOpenclawConfigPath();
  if (!existsSync(configPath)) {
    return { status: "error", configPath, error: "openclaw config file not found" };
  }
  let parsed;
  try {
    const raw = readFileSync(configPath, "utf-8");
    parsed = JSON.parse(raw);
  } catch (e) {
    return { status: "error", configPath, error: `could not read/parse config: ${e instanceof Error ? e.message : String(e)}` };
  }
  const tools = parsed.tools ?? {};
  const alsoAllow = Array.isArray(tools.alsoAllow) ? tools.alsoAllow : [];
  if (isAllowlistCoveringHivemind(alsoAllow)) {
    return { status: "already-set", configPath };
  }
  const updated = {
    ...parsed,
    tools: {
      ...tools,
      alsoAllow: [...alsoAllow, "hivemind"]
    }
  };
  const backupPath = `${configPath}.bak-hivemind-${Date.now()}`;
  const tmpPath = `${configPath}.tmp-hivemind-${process.pid}`;
  try {
    writeFileSync(backupPath, readFileSync(configPath, "utf-8"));
    writeFileSync(tmpPath, JSON.stringify(updated, null, 2) + "\n");
    renameSync(tmpPath, configPath);
  } catch (e) {
    return { status: "error", configPath, error: `could not write config: ${e instanceof Error ? e.message : String(e)}` };
  }
  return { status: "added", configPath, backupPath };
}
function toggleAutoUpdateConfig(setTo) {
  const configPath = getOpenclawConfigPath();
  if (!existsSync(configPath)) {
    return { status: "error", configPath, error: "openclaw config file not found" };
  }
  let parsed;
  try {
    parsed = JSON.parse(readFileSync(configPath, "utf-8"));
  } catch (e) {
    return { status: "error", configPath, error: `could not read/parse config: ${e instanceof Error ? e.message : String(e)}` };
  }
  const plugins = parsed.plugins ?? {};
  const entries = plugins.entries ?? {};
  const hivemindEntry = entries.hivemind ?? {};
  const pluginConfig = hivemindEntry.config ?? {};
  const current = pluginConfig.autoUpdate !== false;
  const newValue = typeof setTo === "boolean" ? setTo : !current;
  const updated = {
    ...parsed,
    plugins: {
      ...plugins,
      entries: {
        ...entries,
        hivemind: {
          ...hivemindEntry,
          config: { ...pluginConfig, autoUpdate: newValue }
        }
      }
    }
  };
  const backupPath = `${configPath}.bak-hivemind-${Date.now()}`;
  const tmpPath = `${configPath}.tmp-hivemind-${process.pid}`;
  try {
    writeFileSync(backupPath, readFileSync(configPath, "utf-8"));
    writeFileSync(tmpPath, JSON.stringify(updated, null, 2) + "\n");
    renameSync(tmpPath, configPath);
  } catch (e) {
    return { status: "error", configPath, error: `could not write config: ${e instanceof Error ? e.message : String(e)}` };
  }
  return { status: "updated", configPath, newValue };
}
function detectAllowlistMissing() {
  const configPath = getOpenclawConfigPath();
  if (!existsSync(configPath)) return false;
  try {
    const parsed = JSON.parse(readFileSync(configPath, "utf-8"));
    const tools = parsed.tools ?? {};
    return !isAllowlistCoveringHivemind(tools.alsoAllow);
  } catch {
    return false;
  }
}
export {
  HIVEMIND_TOOL_NAMES,
  detectAllowlistMissing,
  ensureHivemindAllowlisted,
  getOpenclawConfigPath,
  isAllowlistCoveringHivemind,
  toggleAutoUpdateConfig
};
