import { invokeNLDatomicQueryInterpreter } from "./agents/nl-datomic-query";
import { invokeNLQueryInterpreter } from "./agents/nl-query";
import { askYourGraph } from "./agents/search-agent/ask-your-graph";
import {
  openLastAskYourGraphResults,
  chatWithLinkedRefs,
} from "../components/full-results-popup";
import { languages } from "./languagesSupport";
import { isSupported } from "dompurify";

export const CATEGORY_ICON = {
  "OUTLINER AGENT": "properties",
  "QUERY AGENTS": "filter",
  "CONTENT ANALYSIS": "path-search", // "intelligence", //
  ACTION: "tick-circle",
  CREATION: "new-drawing",
  REPHRASING: "automatic-updates",
  TRANSLATION: "translate",
  "CRITICAL REASONING TOOLKIT": "predictive-analysis",
  "CUSTOM PROMPTS": "user",
  "MCP AGENTS": "data-connection",
  "MCP TOOLS": "wrench",
  "MCP RESOURCES": "database",
  "MCP PROMPTS": "chat",
  CHAT: "chat",
};

export const BUILTIN_COMMANDS = [
  { id: 0, name: "Use this custom prompt", category: "", onlyGen: true },
  {
    id: 1,
    name: "Focused block as prompt",
    category: "",
    includeUids: true,
  },
  {
    id: 102,
    name: "Choosen context as prompt",
    category: "",
    includeUids: true,
    isIncompatibleWith: {
      outliner: true,
    },
  },
  {
    id: 96,
    name: "Open Chat panel",
    // callback: async (args) => {
    //   // The roamContext will be passed from commandProcessing.js via args.roamContext
    //   return openChatPopup({
    //     model: args.model,
    //     rootUid: args.rootUid,
    //     roamContext: args.roamContext || null,
    //     viewMode: args.roamContext ? "both" : "chat-only",
    //   });
    // },
    category: "",
    icon: "chat",
    isIncompatibleWith: {
      outliner: true,
    },
    keyWords: "chat conversation popup dialog",
  },
  {
    id: 10,
    name: "Continue conversation in blocks",
    isIncompatibleWith: {
      outliner: true,
    },
    category: "",
    keyWords: "inline",
  },
  {
    id: 100,
    name: "Focused block as prompt",
    category: "",
    isIncompatibleWith: {
      outliner: true,
    },
    includeUids: true,
  },
  {
    id: 19,
    name: "Web search",
    icon: "globe-network",
    category: "",
    prompt: "Web search",
    includeUids: false,
    isIncompatibleWith: {
      outliner: true,
    },
  },
  {
    id: 191,
    name: "Fetch URL (with Claude)",
    icon: "globe-network",
    category: "",
    prompt: "Fetch url",
    includeUids: false,
    isSub: true,
    isIncompatibleWith: {
      outliner: false,
      chat: true,
    },
  },

  // OUTLINER AGENT COMMANDS
  {
    id: 2,
    name: "Apply custom prompt to Live Outline",
    category: "OUTLINER AGENT",
    isIncompatibleWith: {
      completion: true,
      chat: true,
    },
  },
  {
    id: 21,
    name: "Apply selected blocks as prompt",
    prompt: "",
    category: "OUTLINER AGENT",
    isIncompatibleWith: {
      completion: true,
      chat: true,
    },
  },
  {
    id: 20,
    name: "Set as active Live Outline",
    prompt: "",
    category: "OUTLINER AGENT",
    isIncompatibleWith: {
      completion: true,
      chat: true,
    },
  },
  // {
  //   id: 22,
  //   name: "Set as active outline",
  //   prompt: "",
  //   category: "OUTLINER AGENT",
  //   onlyOutliner: true,
  // },
  // AGENTS,
  {
    id: 22,
    name: "Create new Live Outline",
    prompt: "",
    category: "OUTLINER AGENT",
    isIncompatibleWith: {
      completion: true,
      chat: true,
    },
  },
  {
    id: 3,
    icon: "volume-up",
    name: "Text to Speech",
    category: "",
    keyWords: "TTS, read, voice",
    isIncompatibleWith: {
      chat: true,
    },
  },
  {
    id: 4,
    icon: "volume-down",
    name: "Audio transcription",
    category: "",
    keyWords: "STT, transcribe, speech, audio, transcription",
    showOnlyIfAudioInBlock: true,
  },
  {
    id: 80,
    name: "Natural language query",
    callback: invokeNLQueryInterpreter,
    target: "new",
    category: "QUERY AGENTS",
    isIncompatibleWith: {
      outliner: true,
      style: true,
    },
  },
  {
    id: 81,
    name: "Natural language :q Datomic query",
    callback: invokeNLDatomicQueryInterpreter,
    target: "new",
    category: "QUERY AGENTS",
    isIncompatibleWith: {
      outliner: true,
      style: true,
    },
  },
  {
    id: 92,
    name: "Ask Your Graph",
    callback: askYourGraph,
    category: "QUERY AGENTS",
    isIncompatibleWith: {
      outliner: true,
      chat: true,
    },
    target: "new",
    keyWords: "Natural language search privacy modes secure balanced full",
    submenu: [920, 921, 922, 923],
  },
  {
    id: 920,
    name: "Ask Your Graph (Private)",
    callback: (args) => askYourGraph({ ...args, forcePrivacyMode: "Private" }),
    category: "QUERY AGENTS",
    isIncompatibleWith: {
      outliner: true,
      chat: true,
    },
    target: "new",
    keyWords: "secure private UIDs only",
    isSub: true,
    hideIfDefaultMode: "Private",
  },
  {
    id: 921,
    name: "Ask Your Graph (Balanced)",
    callback: (args) => askYourGraph({ ...args, forcePrivacyMode: "Balanced" }),
    category: "QUERY AGENTS",
    isIncompatibleWith: {
      outliner: true,
      chat: true,
    },
    target: "new",
    keyWords: "balanced secure tools summary",
    isSub: true,
    hideIfDefaultMode: "Balanced",
  },
  {
    id: 922,
    name: "Ask Your Graph (Full Access)",
    callback: (args) =>
      askYourGraph({ ...args, forcePrivacyMode: "Full Access" }),
    category: "QUERY AGENTS",
    isIncompatibleWith: {
      outliner: true,
      chat: true,
    },
    target: "new",
    keyWords: "full access complete content analysis",
    isSub: true,
    hideIfDefaultMode: "Full Access",
  },
  {
    id: 923,
    name: "Ask Your Graph => Results directly in Context panel",
    callback: (args) =>
      askYourGraph({
        ...args,
        forcePrivacyMode: "Private",
        forcePopupOnly: true,
      }),
    category: "QUERY AGENTS",
    icon: "list-detail-view",
    isSub: true,
    isIncompatibleWith: {
      outliner: true,
      chat: true,
    },
    target: "new",
    keyWords: "popup full view chat",
  },
  {
    id: 93,
    name: "Ask Your Graph - Pattern analysis",
    callback: (args) =>
      askYourGraph({
        ...args,
        prompt: "Show me patterns and themes in my graph",
        previousAgentState: {
          // Force scope selection dialog
          forceScopeSelection: true,
        },
      }),
    category: "QUERY AGENTS",
    icon: "chart",
    isSub: true,
    isIncompatibleWith: {
      outliner: true,
      chat: true,
    },
    target: "new",
    keyWords: "pattern analysis explore themes categories scope",
  },
  {
    id: 94,
    name: "View last results in Context panel (Ask Your Graph)",
    callback: openLastAskYourGraphResults,
    category: "QUERY AGENTS",
    icon: "list-detail-view",
    isIncompatibleWith: {
      outliner: true,
      chat: true,
    },
    keyWords: "full, load",
  },
  {
    id: 95,
    name: "Ask Linked References of current page",
    callback: (args) =>
      chatWithLinkedRefs({
        model: args.model,
        rootUid: args.rootUid, // This might be undefined, which our function handles
        targetUid: args.targetUid,
        target: args.target,
      }),
    category: "QUERY AGENTS",
    icon: "chat",
    isIncompatibleWith: {
      outliner: true,
      chat: true,
    },
    target: "new",
    keyWords: "linked references backlinks current page",
  },

  // CONTENT ANALYSIS
  {
    id: 130,
    name: "Summarize",
    prompt: "summarize",
    category: "CONTENT ANALYSIS",
    target: "new w/o",
  },
  {
    id: 131,
    name: "Extract key insights",
    prompt: "keyInsights",
    category: "CONTENT ANALYSIS",
    includeUids: true,
    target: "new",
    submenu: [1310, 1311],
  },
  {
    id: 1310,
    name: "Extract actionable items",
    prompt: "extractActionable",
    category: "CONTENT ANALYSIS",
    isIncompatibleWith: {
      specificStyles: ["Quiz"],
    },
    includeUids: true,
    isSub: true,
  },
  {
    id: 1311,
    name: "Extract highlighted text",
    prompt: "extractHighlights",
    category: "CONTENT ANALYSIS",
    target: "new w/o",
    includeUids: true,
    isSub: true,
    isIncompatibleWith: {
      outliner: true,
    },
  },
  {
    id: 132,
    name: "Extract reasoning structure",
    prompt: "reasoningAnalysis",
    category: "CONTENT ANALYSIS",
    target: "new",
    includeUids: true,
    submenu: [1322, 1320, 1321],
    isIncompatibleWith: {
      outliner: true,
      style: true,
    },
  },
  {
    id: 1322,
    name: "Extract argument map (Mermaid)",
    prompt: "argumentMapMermaid",
    category: "CONTENT ANALYSIS",
    keyWords: "reasoning, diagram",
    isIncompatibleWith: {
      outliner: true,
      style: true,
    },
    target: "new",
    isSub: true,
  },
  {
    id: 1320,
    name: "Sentiment Analysis",
    prompt: "sentimentAnalysis",
    category: "CONTENT ANALYSIS",
    target: "new",
    includeUids: true,
    isSub: true,
  },
  {
    id: 1321,
    name: "Value Analysis",
    prompt: "valueAnalysis",
    category: "CONTENT ANALYSIS",
    target: "new",
    includeUids: true,
    isSub: true,
  },
  {
    id: 133,
    name: "Extract text from image",
    prompt: "imageOCR",
    category: "CONTENT ANALYSIS",
    keyWords: "OCR",
    target: "new",
    submenu: [1330],
  },
  {
    id: 1330,
    name: "Image Analysis",
    prompt: "imageAnalysis",
    category: "CONTENT ANALYSIS",
    isIncompatibleWith: {
      outliner: true,
    },
    keyWords: "visual art, painting",
    isSub: true,
  },

  // CONTENT CREATION
  {
    id: 140,
    name: "Complete sentence",
    prompt: "sentenceCompletion",
    category: "CREATION",
    target: "append",
    isIncompatibleWith: {
      specificStyles: ["Atomic", "Quiz"],
    },
    submenu: [1400],
  },
  {
    id: 1400,
    name: "Complete paragraph",
    prompt: "paragraphCompletion",
    category: "CREATION",
    target: "append",
    isIncompatibleWith: {
      specificStyles: ["Atomic", "Quiz"],
    },
    isSub: true,
  },
  {
    id: 142,
    name: "Short message for social network",
    prompt: "socialNetworkPost",
    category: "CREATION",
    keyWords: "post, X, Twitter, BlueSky, Threads, Mastodon",
    target: "new",
    submenu: [1420],
    isIncompatibleWith: {
      style: true,
    },
  },
  {
    id: 1420,
    name: "Thread for social network",
    prompt: "socialNetworkThread",
    category: "CREATION",
    keyWords: "post, X, Twitter, BlueSky, Threads, Mastodon",
    target: "new",
    isSub: true,
  },
  {
    id: 143,
    name: "Quiz on provided content",
    prompt: "quiz",
    category: "CREATION",
    keyWords: "active learning, question, test",
    isIncompatibleWith: {
      outliner: true,
      style: true,
    },
    includeUids: true,
    target: "new",
  },
  {
    id: 144,
    name: "Another similar content",
    prompt: "similarContent",
    category: "CREATION",
    keyWords: "extend, variant, clone",
    isIncompatibleWith: {
      style: true,
    },
    target: "new w/o",
  },
  {
    id: 145,
    name: "Mermaid diagram",
    prompt: "mermaid",
    category: "CREATION",
    keyWords: "chart, worfkow",
    isIncompatibleWith: {
      style: true,
    },
    target: "new",
  },
  {
    id: 1460,
    name: "Image generation (low)",
    icon: "media",
    prompt: "",
    category: "CREATION",
    keyWords: "draw, picture, nano banana",
    includeUids: false,
    isIncompatibleWith: {
      style: true,
    },
    target: "new w/o",
    submenu: [1461, 1462],
  },
  {
    id: 1461,
    name: "Image generation (medium)",
    icon: "media",
    prompt: "",
    category: "CREATION",
    keyWords: "draw, picture",
    includeUids: false,
    isIncompatibleWith: {
      style: true,
    },
    target: "new w/o",
    isSub: true,
  },
  {
    id: 1462,
    name: "Image generation (high)",
    icon: "media",
    prompt: "",
    category: "CREATION",
    keyWords: "draw, picture",
    includeUids: false,
    isIncompatibleWith: {
      style: true,
    },
    target: "new w/o",
    isSub: true,
  },

  {
    id: 192,
    name: "Export to PDF — clean document (with Claude)",
    icon: "document",
    category: "CREATION",
    prompt: "Export to PDF",
    includeUids: false,
    isIncompatibleWith: {
      outliner: true,
      chat: true,
    },
    submenu: [1920],
  },
  {
    id: 1920,
    name: "Export to PDF — outline (with Claude)",
    icon: "document",
    category: "CREATION",
    prompt: "Export to PDF outline",
    includeUids: false,
    isSub: true,
    isIncompatibleWith: {
      outliner: true,
      chat: true,
    },
  },
  {
    id: 193,
    name: "Export to PDF (with Claude)",
    icon: "document",
    category: "CREATION",
    prompt: "Export to PDF",
    includeUids: false,
    isIncompatibleWith: {
      outliner: true,
      completion: true,
    },
  },
  {
    id: 194,
    name: "Export to DOCX — clean document (with Claude)",
    icon: "document",
    category: "CREATION",
    prompt: "Export to DOCX",
    includeUids: false,
    isIncompatibleWith: {
      outliner: true,
      chat: true,
    },
  },
  {
    id: 196,
    name: "Export to DOCX — outline (with Claude)",
    icon: "document",
    category: "CREATION",
    prompt: "Export to DOCX outline",
    includeUids: false,
    isIncompatibleWith: {
      outliner: true,
      chat: true,
    },
  },
  {
    id: 195,
    name: "Export to PPTX — shortened (with Claude)",
    icon: "presentation",
    category: "CREATION",
    prompt: "Export to PPTX",
    includeUids: false,
    isIncompatibleWith: {
      outliner: true,
      chat: true,
    },
  },
  {
    id: 197,
    name: "Export to PPTX — full content (with Claude)",
    icon: "presentation",
    category: "CREATION",
    prompt: "Export to PPTX full",
    includeUids: false,
    isIncompatibleWith: {
      outliner: true,
      chat: true,
    },
  },
  {
    id: 1940,
    name: "Export to Word document / DOCX (with Claude)",
    icon: "document",
    category: "CREATION",
    prompt: "Export to DOCX",
    includeUids: false,
    isIncompatibleWith: {
      outliner: true,
      completion: true,
    },
  },
  {
    id: 1950,
    name: "Export to PowerPoint / PPTX (with Claude)",
    icon: "presentation",
    category: "CREATION",
    prompt: "Export to PPTX",
    includeUids: false,
    isIncompatibleWith: {
      outliner: true,
      completion: true,
    },
  },

  // REPHRASING
  {
    id: 120,
    name: "Fix spelling & grammar",
    prompt: "correctWording",
    category: "REPHRASING",
    target: "new w/o",
    isIncompatibleWith: {
      outliner: true,
      style: true,
    },
    submenu: [1200, 1201, 1202],
  },
  {
    id: 1200,
    name: "Fix spelling & grammar + explain",
    prompt: "correctWordingAndExplain",
    category: "REPHRASING",
    target: "new w/o",
    isIncompatibleWith: {
      outliner: true,
      style: true,
    },
    isSub: true,
  },
  {
    id: 1201,
    name: "Fix spelling & grammar / suggestions",
    prompt: "correctWordingAndSuggestions",
    category: "REPHRASING",
    withSuggestions: true,
    target: "replace",
    includeUids: true,
    isIncompatibleWith: {
      style: true,
      chat: true,
    },
    isSub: true,
  },
  {
    id: 1202,
    name: "Accept corrections/suggestions",
    prompt: "acceptSuggestions",
    category: "REPHRASING",
    withSuggestions: true,
    target: "replace",
    includeUids: true,
    isIncompatibleWith: {
      style: true,
      chat: true,
    },
    isSub: true,
  },
  {
    id: 121,
    name: "Rephrase",
    prompt: "rephrase",
    category: "REPHRASING",

    target: "new w/o",
    submenu: [1210, 1211, 1212, 1213, 1214, 1215, 1216, 1217],
  },
  {
    id: 1210,
    name: "Shorter",
    prompt: "shorten",
    category: "REPHRASING",

    target: "new w/o",
    isSub: true,
  },
  {
    id: 1211,
    name: "More accessible",
    prompt: "accessible",
    category: "REPHRASING",

    target: "new w/o",
    isSub: true,
  },
  {
    id: 1212,
    name: "Clearer and more explicit",
    prompt: "clearer",
    category: "REPHRASING",

    target: "new w/o",
    isSub: true,
  },
  {
    id: 1213,
    name: "More formal",
    prompt: "formal",
    category: "REPHRASING",

    target: "new w/o",
    isSub: true,
  },
  {
    id: 1214,
    name: "More casual",
    prompt: "casual",
    category: "REPHRASING",

    target: "new w/o",
    isSub: true,
  },
  {
    id: 1215,
    name: "More engaging",
    prompt: "enhance",
    category: "REPHRASING",
    keyWords: "enhance, synonym",

    target: "new w/o",
    isSub: true,
  },
  {
    id: 1216,
    name: "More engaging with suggestions",
    prompt: "enhanceWithSuggestions",
    category: "REPHRASING",
    keyWords: "enhance, synonym",
    withSuggestions: true,
    isIncompatibleWith: {
      chat: true,
    },
    target: "replace",
    includeUids: true,
    isSub: true,
  },
  {
    id: 1217,
    name: "Vocabulary suggestions",
    prompt: "vocabularySuggestions",
    category: "REPHRASING",
    keyWords: "enhance, synonym",
    withSuggestions: true,
    target: "replace",
    includeUids: true,
    isIncompatibleWith: {
      style: true,
      chat: true,
    },
    isSub: true,
  },
  {
    id: 122,
    name: "Outline to Paragraph",
    prompt: "linearParagraph",
    category: "REPHRASING",
    isIncompatibleWith: {
      specificStyles: ["Atomic"],
    },
    target: "new w/o",
    submenu: [1220],
  },
  {
    id: 1220,
    name: "Paragraph to Outline",
    prompt: "outline",
    category: "REPHRASING",
    isIncompatibleWith: {
      specificStyles: ["No bullet points"],
    },
    target: "new w/o",
    isSub: true,
  },

  // TRANSLATION
  {
    id: 11,
    name: "Translate to... (<default>)",
    prompt: "translate",
    category: "TRANSLATION",
    target: "new w/o",
    isIncompatibleWith: {
      style: true,
    },
    submenu: [
      1100, 1101, 1102, 1103, 1104, 1105, 1106, 1107, 1108, 1109, 1110, 1111,
      1112, 1113, 1114, 1115, 1116, 1199,
    ],
  },
  {
    id: 1199,
    name: "User defined",
    prompt: "translate",
    category: "TRANSLATATION",
    target: "new w/o",
    isSub: true,
  },

  // ACTION
  {
    id: 145,
    name: "Action plan for a project",
    prompt: "actionPlan",
    category: "ACTION",
    keyWords: "task",

    target: "new",
    includeUids: true,
    submenu: [1450, 1451, 1452],
  },
  {
    id: 1450,
    name: "How to... (active learning)",
    prompt: "howTo",
    category: "ACTION",
    keyWords: "help, self discovery, problem-solving, step",

    target: "new",
    includeUids: true,
    isSub: true,
  },
  {
    id: 1451,
    name: "Roadmap to achieve a goal",
    prompt: "guidanceToGoal",
    category: "ACTION",
    keyWords: "coach",

    target: "new",
    includeUids: true,
    isSub: true,
  },
  {
    id: 1452,
    name: "Practical tip",
    prompt: "practicalTip",
    category: "ACTION",
    keyWords: "advice, value, principle",

    target: "new w/o",
    isSub: true,
  },
  {
    id: 146,
    name: "Help to make a choice",
    prompt: "choice",
    category: "ACTION",
    keyword: "choose, decision",

    includeUids: true,
    target: "new",
  },

  // CRITICAL REASONING TOOLKIT
  {
    id: 151,
    name: "Argument",
    prompt: "argument",
    category: "CRITICAL REASONING TOOLKIT",

    submenu: [1511, 1512, 1513, 1514],
  },
  {
    id: 1511,
    name: "Consolidate or base on evidence",
    prompt: "consolidate",
    category: "CRITICAL REASONING TOOLKIT",
    keyWords: "argument",

    includeUids: true,
    isSub: true,
  },
  {
    id: 1512,
    name: "Objection, counterargument",
    prompt: "objection",
    category: "CRITICAL REASONING TOOLKIT",

    includeUids: true,
    isSub: true,
  },
  {
    id: 1513,
    name: "Counterexample",
    prompt: "counterExample",
    category: "CRITICAL REASONING TOOLKIT",

    isSub: true,
  },
  {
    id: 1514,
    name: "Logical fallacy challenge",
    prompt: "fallacy",
    category: "CRITICAL REASONING TOOLKIT",
    isIncompatibleWith: {
      outliner: true,
    },
    keyWords: "cognitive biases",
    isSub: true,
  },
  {
    id: 154,
    name: "Explanation",
    prompt: "explanation",
    category: "CRITICAL REASONING TOOLKIT",
    includeUids: true,
    submenu: [1540, 1541, 1542, 1543],
  },
  {
    id: 1540,
    name: "Definition, meaning",
    prompt: "meaning",
    category: "CRITICAL REASONING TOOLKIT",
    keyWords: "explanation",
    target: "new w/o",
    isSub: true,
  },
  {
    id: 1541,
    name: "Example",
    prompt: "example",
    category: "CRITICAL REASONING TOOLKIT",
    isSub: true,
  },
  {
    id: 1542,
    name: "Causal explanation",
    prompt: "causalExplanation",
    category: "CRITICAL REASONING TOOLKIT",
    isSub: true,
  },
  {
    id: 1543,
    name: "Explanation by analogy",
    prompt: "analogicalExplanation",
    category: "CRITICAL REASONING TOOLKIT",
    isSub: true,
  },
  {
    id: 156,
    name: "Challenge my ideas!",
    prompt: "challengeMyIdeas",
    category: "CRITICAL REASONING TOOLKIT",
    submenu: [1561, 1562, 1563],
    includeUids: true,
  },
  {
    id: 1561,
    name: "Raise questions",
    prompt: "raiseQuestions",
    category: "CRITICAL REASONING TOOLKIT",
    includeUids: true,
    isSub: true,
  },
  {
    id: 1562,
    name: "Perspective shift",
    prompt: "perspectiveShift",
    category: "CRITICAL REASONING TOOLKIT",
    keyWords: "change, view, frame, reframing",
    includeUids: true,
    isSub: true,
  },
  {
    id: 1563,
    name: "Brainstorming",
    prompt: "brainstorming",
    category: "CRITICAL REASONING TOOLKIT",
    includeUids: true,
    isSub: true,
  },
].concat(
  languages.map((lgg, index) => {
    return {
      id: 1100 + index,
      name: lgg[0],
      label: lgg[1],
      prompt: "translate",
      category: "TRANSLATION",
      target: "new w/o",
      isIncompatibleWith: {
        style: true,
      },
      isSub: true,
    };
  }),
);
