/**
 * Kubernetes Tool Adapters
 *
 * ITool-compliant adapter for KubernetesTool operations.
 * This adapter wraps the existing KubernetesTool methods to conform
 * to the formal ITool interface for use with the FormalToolRegistry.
 */
import { KubernetesTool } from '../kubernetes-tool.js';
// ============================================================================
// Shared KubernetesTool Instance
// ============================================================================
let kubernetesInstance = null;
function getKubernetes() {
    if (!kubernetesInstance) {
        kubernetesInstance = new KubernetesTool();
    }
    return kubernetesInstance;
}
/**
 * Reset the shared KubernetesTool instance (for testing)
 */
export function resetKubernetesInstance() {
    kubernetesInstance = null;
}
// ============================================================================
// KubernetesOperationTool
// ============================================================================
/**
 * KubernetesOperationTool - ITool adapter for Kubernetes operations
 *
 * Unified tool that handles all Kubernetes operations via an operation parameter.
 */
export class KubernetesOperationTool {
    name = 'kubernetes';
    description = 'Execute Kubernetes cluster management operations using kubectl';
    async execute(input) {
        const operation = input.operation;
        const args = input.args || {};
        const k8s = getKubernetes();
        switch (operation) {
            case 'cluster_info':
                return await k8s.clusterInfo();
            case 'get_context':
                return await k8s.getCurrentContext();
            case 'list_contexts':
                return await k8s.listContexts();
            case 'use_context':
                return await k8s.useContext(args.context);
            case 'get':
                return await k8s.get(args.resourceType, args.name, {
                    namespace: args.namespace,
                    allNamespaces: args.allNamespaces,
                    selector: args.selector,
                    output: args.output,
                });
            case 'describe':
                return await k8s.describe(args.resourceType, args.name, args.namespace);
            case 'apply':
                return await k8s.apply(args.path, {
                    namespace: args.namespace,
                    dryRun: args.dryRun,
                    force: args.force,
                });
            case 'delete':
                return await k8s.delete(args.resourceType, args.name, {
                    namespace: args.namespace,
                    force: args.force,
                    gracePeriod: args.gracePeriod,
                });
            case 'logs':
                return await k8s.logs(args.name, {
                    namespace: args.namespace,
                    container: args.container,
                    tail: args.tail,
                    previous: args.previous,
                    timestamps: args.timestamps,
                });
            case 'exec':
                return await k8s.exec(args.name, args.command, {
                    namespace: args.namespace,
                    container: args.container,
                });
            case 'scale':
                return await k8s.scale(args.resourceType, args.name, {
                    namespace: args.namespace,
                    replicas: args.replicas,
                });
            case 'rollout_status':
                return await k8s.rolloutStatus(args.resourceType, args.name, args.namespace);
            case 'rollout_restart':
                return await k8s.rolloutRestart(args.resourceType, args.name, args.namespace);
            case 'port_forward':
                return await k8s.portForward(args.resourceType, args.name, args.localPort, args.remotePort, args.namespace);
            case 'get_events':
                return await k8s.getEvents(args.namespace, args.fieldSelector);
            case 'top':
                return await k8s.top(args.resourceType, args.namespace);
            case 'create_namespace':
                return await k8s.createNamespace(args.name);
            case 'set_namespace':
                return await k8s.setNamespace(args.namespace);
            case 'create_configmap':
                return await k8s.createConfigMap(args.name, args.data, args.namespace);
            case 'create_secret':
                return await k8s.createSecret(args.name, args.data, args.namespace, args.secretType);
            default:
                return {
                    success: false,
                    error: `Unknown Kubernetes operation: ${operation}`,
                };
        }
    }
    getSchema() {
        return {
            name: this.name,
            description: this.description,
            parameters: {
                type: 'object',
                properties: {
                    operation: {
                        type: 'string',
                        description: 'Kubernetes operation to perform',
                        enum: [
                            'cluster_info',
                            'get_context',
                            'list_contexts',
                            'use_context',
                            'get',
                            'describe',
                            'apply',
                            'delete',
                            'logs',
                            'exec',
                            'scale',
                            'rollout_status',
                            'rollout_restart',
                            'port_forward',
                            'get_events',
                            'top',
                            'create_namespace',
                            'set_namespace',
                            'create_configmap',
                            'create_secret',
                        ],
                    },
                    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 = [
            'cluster_info',
            'get_context',
            'list_contexts',
            'use_context',
            'get',
            'describe',
            'apply',
            'delete',
            'logs',
            'exec',
            'scale',
            'rollout_status',
            'rollout_restart',
            'port_forward',
            'get_events',
            'top',
            'create_namespace',
            'set_namespace',
            'create_configmap',
            'create_secret',
        ];
        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: ['kubernetes', 'k8s', 'kubectl', 'cluster', 'pod', 'deployment', 'service', 'devops'],
            priority: 6,
            requiresConfirmation: true,
            modifiesFiles: false,
            makesNetworkRequests: true,
        };
    }
    isAvailable() {
        return true;
    }
    dispose() {
        resetKubernetesInstance();
    }
}
// ============================================================================
// Factory Function
// ============================================================================
/**
 * Create all kubernetes tool instances
 */
export function createKubernetesTools() {
    return [
        new KubernetesOperationTool(),
    ];
}
//# sourceMappingURL=kubernetes-tools.js.map