import { permissionForCommand } from "../../access/access-control.js";
import { discordCommandCatalog } from "../shared/channel-command-catalog.js";
import { normalizeChannelCommandName, parseChannelCommand } from "../shared/channel-runtime.js";
export function parseDiscordMessageCommand(text) {
    return parseChannelCommand(text, { allowBotMention: false });
}
export function argumentFromDiscordInteraction(interaction) {
    if (interaction.commandName === "prompt") {
        return interaction.options.getString("text") ?? "";
    }
    if (interaction.commandName === "queue") {
        return [interaction.options.getString("action"), interaction.options.getString("id")].filter(Boolean).join(" ");
    }
    if (interaction.commandName === "update") {
        return [interaction.options.getString("target"), interaction.options.getString("agent"), interaction.options.getString("input")].filter(Boolean).join(" ");
    }
    return interaction.options.getString("value") ?? interaction.options.getString("query") ?? interaction.options.getString("thread_id") ?? "";
}
export function requiredPermissionForDiscordCommand(command, argument) {
    const normalized = normalizeChannelCommandName(command);
    if (normalized === "prompt")
        return "prompt.send";
    if (normalized === "queue")
        return argument.trim() ? "queue.write" : "queue.read";
    return permissionForCommand(normalized);
}
export function isUnauthenticatedDiscordCommandAllowed(command) {
    return normalizeChannelCommandName(command) === "link";
}
export function permissionForDiscordAction(action) {
    if (action.startsWith("discord_queue_") || action.startsWith("discord_peer_queue_"))
        return "queue.write";
    if (action.startsWith("discord_abort:"))
        return "prompt.abort";
    if (action.startsWith("discord_external_approval:"))
        return "prompt.abort";
    if (action.startsWith("node_target:"))
        return "peers.connect";
    if (action.startsWith("discord_sessions_page:"))
        return "sessions.read";
    if (action.startsWith("discord_pick:"))
        return "sessions.write";
    if (action.startsWith("discord_artifact_delete:"))
        return "files.write";
    if (action.startsWith("discord_artifact_"))
        return "files.read";
    if (action.startsWith("agent-update:"))
        return "updates.run";
    return null;
}
export function discordCommands() {
    return discordCommandCatalog()
        .map((entry) => command(entry.name, entry.description, entry.options));
}
function command(name, description, options = []) {
    return {
        name,
        description,
        type: 1,
        dm_permission: true,
        options,
    };
}
