
import { MarkdownString } from '../../../../../../base/common/htmlContent.js';
import { URI } from '../../../../../../base/common/uri.js';
import '../../../../../../platform/agentHost/common/state/protocol/channels-root/state.js';
import { ConfirmationOptionKind } from '../../../../../../platform/agentHost/common/state/protocol/channels-session/state.js';
import '../../../../../../platform/agentHost/common/state/protocol/channels-terminal/state.js';
import '../../../../../../platform/agentHost/common/state/protocol/channels-changeset/state.js';
import '../../../../../../platform/agentHost/common/state/protocol/channels-resource-watch/state.js';
import { ToolDataSource, ToolInvocationPresentation } from '../languageModelToolsService.js';

const ConfirmationToolId = "vscode_get_confirmation";
const ConfirmationToolWithOptionsId = "vscode_get_confirmation_with_options";
const ModifiedFilesConfirmationToolId = "vscode_get_modified_files_confirmation";
const ConfirmationToolData = {
    id: ConfirmationToolId,
    displayName: "Confirmation Tool",
    modelDescription: "A tool that demonstrates different types of confirmations. Takes a title, message, and confirmation type (basic or terminal).",
    source: ToolDataSource.Internal,
    inputSchema: {
        type: "object",
        properties: {
            title: {
                type: "string",
                description: "Title for the confirmation dialog"
            },
            message: {
                type: "string",
                description: "Message to show in the confirmation dialog"
            },
            confirmationType: {
                type: "string",
                enum: ["basic", "terminal"],
                description: "Type of confirmation to show - basic for simple confirmation, terminal for terminal command confirmation"
            },
            terminalCommand: {
                type: "string",
                description: "Terminal command to show (only used when confirmationType is \"terminal\")"
            }
        },
        required: ["title", "message", "confirmationType"],
        additionalProperties: false
    }
};
const ConfirmationToolWithOptionsData = {
    id: ConfirmationToolWithOptionsId,
    displayName: "Confirmation Tool with Options",
    modelDescription: "A tool that demonstrates different types of confirmations. Takes a title, message, and buttons.",
    source: ToolDataSource.Internal,
    inputSchema: {
        type: "object",
        properties: {
            title: {
                type: "string",
                description: "Title for the confirmation dialog"
            },
            message: {
                type: "string",
                description: "Message to show in the confirmation dialog"
            },
            buttons: {
                type: "array",
                items: {
                    type: "string"
                },
                description: "Custom button labels to display."
            }
        },
        required: ["title", "message", "buttons"],
        additionalProperties: false
    }
};
const ModifiedFilesConfirmationToolData = {
    id: ModifiedFilesConfirmationToolId,
    displayName: "Modified Files Confirmation Tool",
    modelDescription: "A tool that shows a modified-files confirmation UI with a split primary button and a hardcoded cancel action.",
    source: ToolDataSource.Internal,
    inputSchema: {
        type: "object",
        properties: {
            title: {
                type: "string",
                description: "Title for the confirmation dialog"
            },
            message: {
                type: "string",
                description: "Message to show in the confirmation dialog"
            },
            options: {
                type: "array",
                items: {
                    type: "string"
                },
                minItems: 1,
                description: "Selectable option labels. The first option is used for the primary split button and the remaining options are placed in the dropdown menu."
            },
            modifiedFiles: {
                type: "array",
                items: {
                    type: "object",
                    properties: {
                        uri: {
                            type: "string",
                            description: "URI of the modified file."
                        },
                        originalUri: {
                            type: "string",
                            description: "Optional original URI used when opening a diff."
                        },
                        insertions: {
                            type: "number",
                            description: "Optional number of lines added."
                        },
                        deletions: {
                            type: "number",
                            description: "Optional number of lines removed."
                        },
                        title: {
                            type: "string",
                            description: "Optional title shown in the file tooltip."
                        },
                        description: {
                            type: "string",
                            description: "Optional secondary label shown for the file entry."
                        }
                    },
                    required: ["uri"],
                    additionalProperties: false
                },
                description: "Modified files to show in the confirmation UI."
            }
        },
        required: ["title", "message", "options", "modifiedFiles"],
        additionalProperties: false
    }
};
class ConfirmationTool {
    async prepareToolInvocation(context, token) {
        const parameters = context.parameters;
        if (!parameters.title || !parameters.message) {
            throw ( new Error("Missing required parameters for ConfirmationTool"));
        }
        const confirmationType = parameters.confirmationType ?? "basic";
        let toolSpecificData;
        if (confirmationType === "terminal") {
            toolSpecificData = {
                kind: "terminal",
                commandLine: {
                    original: parameters.terminalCommand ?? ""
                },
                language: "bash"
            };
        } else {
            toolSpecificData = undefined;
        }
        return {
            confirmationMessages: {
                title: parameters.title,
                message: ( new MarkdownString(parameters.message)),
                allowAutoConfirm: (parameters.buttons || []).length ? false : true,
                customOptions: parameters.buttons?.map((label, index) => ({
                    id: label,
                    label,
                    kind: index === 0 ? ConfirmationOptionKind.Approve : ConfirmationOptionKind.Deny
                }))
            },
            toolSpecificData,
            presentation: ToolInvocationPresentation.HiddenAfterComplete
        };
    }
    async invoke(invocation, countTokens, progress, token) {
        if (invocation.selectedCustomButton) {
            return {
                content: [{
                    kind: "text",
                    value: invocation.selectedCustomButton
                }]
            };
        }
        return {
            content: [{
                kind: "text",
                value: "yes"
            }]
        };
    }
}
class ModifiedFilesConfirmationTool {
    async prepareToolInvocation(context, token) {
        const parameters = context.parameters;
        if (!parameters.title || !parameters.message) {
            throw ( new Error("Missing required parameters for ModifiedFilesConfirmationTool"));
        }
        if (!parameters.options?.length) {
            throw ( new Error("ModifiedFilesConfirmationTool requires at least one option"));
        }
        const toolSpecificData = {
            kind: "modifiedFilesConfirmation",
            options: parameters.options,
            modifiedFiles: ( parameters.modifiedFiles.map(file => ({
                uri: ( URI.parse(file.uri)).toJSON(),
                originalUri: file.originalUri ? ( URI.parse(file.originalUri)).toJSON() : undefined,
                insertions: file.insertions,
                deletions: file.deletions,
                title: file.title,
                description: file.description
            })))
        };
        return {
            confirmationMessages: {
                title: parameters.title,
                message: ( new MarkdownString(parameters.message)),
                allowAutoConfirm: false
            },
            toolSpecificData,
            presentation: ToolInvocationPresentation.HiddenAfterComplete
        };
    }
    async invoke(invocation, countTokens, progress, token) {
        if (invocation.selectedCustomButton) {
            return {
                content: [{
                    kind: "text",
                    value: invocation.selectedCustomButton
                }]
            };
        }
        return {
            content: [{
                kind: "text",
                value: "yes"
            }]
        };
    }
}

export { ConfirmationTool, ConfirmationToolData, ConfirmationToolId, ConfirmationToolWithOptionsData, ConfirmationToolWithOptionsId, ModifiedFilesConfirmationTool, ModifiedFilesConfirmationToolData, ModifiedFilesConfirmationToolId };
