/**
 * Docker Tool Adapters
 *
 * ITool-compliant adapter for DockerTool operations.
 * This adapter wraps the existing DockerTool methods to conform
 * to the formal ITool interface for use with the FormalToolRegistry.
 */
import { DockerTool } from '../docker-tool.js';
// ============================================================================
// Shared DockerTool Instance
// ============================================================================
let dockerInstance = null;
function getDocker() {
    if (!dockerInstance) {
        dockerInstance = new DockerTool();
    }
    return dockerInstance;
}
/**
 * Reset the shared DockerTool instance (for testing)
 */
export function resetDockerInstance() {
    dockerInstance = null;
}
// ============================================================================
// DockerOperationTool
// ============================================================================
/**
 * DockerOperationTool - ITool adapter for Docker operations
 *
 * Unified tool that handles all Docker operations via an operation parameter.
 */
export class DockerOperationTool {
    name = 'docker';
    description = 'Execute Docker container and image management operations';
    async execute(input) {
        const operation = input.operation;
        const args = input.args || {};
        const docker = getDocker();
        switch (operation) {
            case 'list_containers':
                return await docker.listContainers(args.all);
            case 'list_images':
                return await docker.listImages();
            case 'run':
                return await docker.run(args.image, args.command, {
                    name: args.name,
                    ports: args.ports,
                    volumes: args.volumes,
                    env: args.env,
                    detach: args.detach,
                    rm: args.rm,
                    network: args.network,
                });
            case 'stop':
                return await docker.stop(args.container);
            case 'start':
                return await docker.start(args.container);
            case 'remove_container':
                return await docker.removeContainer(args.container, args.force);
            case 'remove_image':
                return await docker.removeImage(args.image, args.force);
            case 'logs':
                return await docker.logs(args.container, args.tail);
            case 'exec':
                return await docker.exec(args.container, args.command);
            case 'build':
                return await docker.build(args.context, {
                    dockerfile: args.dockerfile,
                    tag: args.tag,
                    noCache: args.noCache,
                    buildArgs: args.buildArgs,
                });
            case 'pull':
                return await docker.pull(args.image);
            case 'push':
                return await docker.push(args.image);
            case 'inspect':
                return await docker.inspect(args.container || args.image, args.image ? 'image' : 'container');
            case 'compose_up':
                return await docker.composeUp(args.file, args.services, args.detach ?? true);
            case 'compose_down':
                return await docker.composeDown(args.file, args.removeVolumes);
            case 'system_info':
                return await docker.systemInfo();
            case 'prune':
                return await docker.prune(args.pruneType);
            default:
                return {
                    success: false,
                    error: `Unknown Docker operation: ${operation}`,
                };
        }
    }
    getSchema() {
        return {
            name: this.name,
            description: this.description,
            parameters: {
                type: 'object',
                properties: {
                    operation: {
                        type: 'string',
                        description: 'Docker operation to perform',
                        enum: [
                            'list_containers',
                            'list_images',
                            'run',
                            'stop',
                            'start',
                            'remove_container',
                            'remove_image',
                            'logs',
                            'exec',
                            'build',
                            'pull',
                            'push',
                            'inspect',
                            'compose_up',
                            'compose_down',
                            'system_info',
                            'prune',
                        ],
                    },
                    args: {
                        type: 'object',
                        description: 'Operation-specific arguments',
                    },
                },
                required: ['operation'],
            },
        };
    }
    validate(input) {
        if (typeof input !== 'object' || input === null) {
            return { valid: false, errors: ['Input must be an object'] };
        }
        const data = input;
        if (typeof data.operation !== 'string' || data.operation.trim() === '') {
            return { valid: false, errors: ['operation must be a non-empty string'] };
        }
        const validOperations = [
            'list_containers',
            'list_images',
            'run',
            'stop',
            'start',
            'remove_container',
            'remove_image',
            'logs',
            'exec',
            'build',
            'pull',
            'push',
            'inspect',
            'compose_up',
            'compose_down',
            'system_info',
            'prune',
        ];
        if (!validOperations.includes(data.operation)) {
            return { valid: false, errors: [`Unknown operation: ${data.operation}`] };
        }
        return { valid: true };
    }
    getMetadata() {
        return {
            name: this.name,
            description: this.description,
            category: 'system',
            keywords: ['docker', 'container', 'image', 'compose', 'devops'],
            priority: 6,
            requiresConfirmation: true,
            modifiesFiles: false,
            makesNetworkRequests: true,
        };
    }
    isAvailable() {
        return true;
    }
    dispose() {
        resetDockerInstance();
    }
}
// ============================================================================
// Factory Function
// ============================================================================
/**
 * Create all docker tool instances
 */
export function createDockerTools() {
    return [
        new DockerOperationTool(),
    ];
}
//# sourceMappingURL=docker-tools.js.map