import {
  __require
} from "./chunk-3RG5ZIWI.js";

// src/mcp/project-trust.ts
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "fs";
import { createHash } from "crypto";
import { join, dirname } from "path";
var TRUST_FILE = "mcp-project-trust.json";
function hashMcpFile(filePath) {
  const buf = readFileSync(filePath);
  return createHash("sha256").update(buf).digest("hex");
}
function loadStore(baseDir) {
  const file = join(baseDir, TRUST_FILE);
  if (existsSync(file)) {
    try {
      const parsed = JSON.parse(readFileSync(file, "utf-8"));
      if (parsed && typeof parsed === "object" && Array.isArray(parsed.entries)) {
        return parsed;
      }
    } catch {
    }
  }
  return { version: 1, entries: [] };
}
function saveStore(baseDir, store) {
  mkdirSync(dirname(join(baseDir, TRUST_FILE)), { recursive: true });
  const file = join(baseDir, TRUST_FILE);
  const tmp = file + ".tmp";
  writeFileSync(tmp, JSON.stringify(store, null, 2), "utf-8");
  try {
    const { renameSync } = __require("fs");
    renameSync(tmp, file);
  } catch {
    writeFileSync(file, JSON.stringify(store, null, 2), "utf-8");
  }
}
function checkTrust(baseDir, mcpPath) {
  if (!existsSync(mcpPath)) return { trusted: true };
  const currentHash = hashMcpFile(mcpPath);
  const store = loadStore(baseDir);
  const entry = store.entries.find((e) => e.path === mcpPath);
  if (!entry) return { trusted: false, reason: "unknown", currentHash };
  if (entry.hash !== currentHash) {
    return { trusted: false, reason: "changed", previousHash: entry.hash, currentHash };
  }
  return { trusted: true };
}
function approveProject(baseDir, mcpPath) {
  const currentHash = hashMcpFile(mcpPath);
  const store = loadStore(baseDir);
  const idx = store.entries.findIndex((e) => e.path === mcpPath);
  const entry = {
    path: mcpPath,
    hash: currentHash,
    approvedAt: (/* @__PURE__ */ new Date()).toISOString()
  };
  if (idx >= 0) store.entries[idx] = entry;
  else store.entries.push(entry);
  saveStore(baseDir, store);
}
export {
  approveProject,
  checkTrust,
  hashMcpFile
};
