#!/usr/bin/env node

// src/tools/types.ts
function isFileWriteTool(name) {
  return name === "write_file" || name === "edit_file" || name === "notebook_edit";
}
function getDangerLevel(toolName, args) {
  if (toolName.startsWith("mcp__")) return "safe";
  if (toolName === "bash") {
    const cmd = String(args["command"] ?? "");
    if (/\brm\s+[^\n]*(?:-\w*[rRfF]\w*|--recursive|--force)\b/.test(cmd)) return "destructive";
    if (/\brm\s+\S/.test(cmd)) return "destructive";
    if (/\brmdir\b|\bformat\b|\bmkfs\b|\bdd\s+if=|\bshred\b|\bfdisk\b|\bparted\b/.test(cmd)) return "destructive";
    if (/\bshutdown\b|\breboot\b|\bhalt\b|\bpoweroff\b/.test(cmd)) return "destructive";
    if (/\bkill\s+-9\b|\bkillall\b/.test(cmd)) return "destructive";
    if (/\bRemove-Item\b|\bri\s+\S/i.test(cmd)) return "destructive";
    if (/\brd\s+\/s\b|\brmdir\s+\/s\b/i.test(cmd)) return "destructive";
    if (/\bdel\s+\S/.test(cmd)) return "destructive";
    if (/\bShutdown(-Computer)?\b|\bRestart-Computer\b/i.test(cmd)) return "destructive";
    if (/(?:^|[\s|;&])>>?\s*\S/.test(cmd)) return "write";
    if (/\btee\b|\bcp\b|\bmv\b|\bln\s+-s/.test(cmd)) return "write";
    if (/\bchmod\b|\bchown\b/.test(cmd)) return "write";
    if (/\bSet-Content\b|\bOut-File\b|\bAdd-Content\b|\bCopy-Item\b|\bMove-Item\b|\bSet-ItemProperty\b|\bNew-ItemProperty\b/i.test(cmd)) return "write";
    if (/\b(python3?|node|deno|bun|perl|ruby|php|bash|sh|zsh|pwsh|powershell)(\.exe)?\b[^\n]*?\s-(?:Command|EncodedCommand|c|e|p|r)\b/i.test(cmd)) return "write";
    if (/\bcmd(\.exe)?\s+\/[ck]\b/i.test(cmd)) return "write";
    return "safe";
  }
  if (toolName === "write_file") return "write";
  if (toolName === "edit_file") return "write";
  if (toolName === "save_last_response") return "write";
  if (toolName === "run_interactive") {
    const exe = String(args["executable"] ?? "").toLowerCase();
    if (/\b(rm|rmdir|del|format|mkfs|Remove-Item)\b/i.test(exe)) return "destructive";
    if (/\b(bash|sh|zsh|cmd|powershell|pwsh|python|node|ruby|perl)\b/i.test(exe)) return "write";
    return "write";
  }
  if (toolName === "task_create" || toolName === "task_stop") return "write";
  if (toolName === "task_list") return "safe";
  if (toolName === "git_commit") return "write";
  if (toolName === "git_status" || toolName === "git_diff" || toolName === "git_log") return "safe";
  if (toolName === "notebook_edit") return "write";
  if (toolName === "read_file" || toolName === "list_dir" || toolName === "grep_files" || toolName === "glob_files" || toolName === "web_fetch" || toolName === "save_memory" || toolName === "ask_user" || toolName === "write_todos" || toolName === "google_search" || toolName === "spawn_agent" || toolName === "run_tests") return "safe";
  return "write";
}
function schemaToJsonSchema(schema) {
  const result = {
    type: schema.type,
    description: schema.description
  };
  if (schema.enum) result["enum"] = schema.enum;
  if (schema.items) result["items"] = schemaToJsonSchema(schema.items);
  if (schema.properties) {
    result["properties"] = Object.fromEntries(
      Object.entries(schema.properties).map(([k, v]) => [k, schemaToJsonSchema(v)])
    );
  }
  return result;
}

export {
  isFileWriteTool,
  getDangerLevel,
  schemaToJsonSchema
};
