"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServerConfigModal = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
/**
 * Server Configuration Modal
 * Handles adding and editing MCP server configurations
 */
const react_1 = require("react");
const react_2 = require("@chakra-ui/react");
const react_hook_form_1 = require("react-hook-form");
const react_query_1 = require("@tanstack/react-query");
const useApi_1 = require("../../hooks/useApi");
const ServerConfigModal = ({ isOpen, onClose, server, }) => {
    const toast = (0, react_2.useToast)();
    const queryClient = (0, react_query_1.useQueryClient)();
    const api = (0, useApi_1.useApi)();
    const [isSubmitting, setIsSubmitting] = (0, react_1.useState)(false);
    const { control, handleSubmit, reset, formState: { errors }, } = (0, react_hook_form_1.useForm)({
        defaultValues: {
            name: '',
            endpoint: '',
            version: '1.0.0',
            maxConcurrentRequests: 100,
            timeout: 30000,
            retryPolicy: {
                maxAttempts: 3,
                backoff: 'exponential',
                maxDelay: 5000,
            },
            security: {
                authentication: true,
                ssl: true,
                allowedOrigins: '*',
                rateLimit: {
                    requestsPerMinute: 1000,
                    burstSize: 100,
                },
            },
        },
    });
    // Reset form when server changes
    (0, react_1.useEffect)(() => {
        if (server) {
            reset({
                name: server.name,
                endpoint: server.endpoint,
                version: server.version,
                maxConcurrentRequests: server.configuration.maxConcurrentRequests,
                timeout: server.configuration.timeout,
                retryPolicy: server.configuration.retryPolicy,
                security: {
                    ...server.configuration.security,
                    allowedOrigins: server.configuration.security.allowedOrigins.join('\n'),
                },
            });
        }
        else {
            reset({
                name: '',
                endpoint: '',
                version: '1.0.0',
                maxConcurrentRequests: 100,
                timeout: 30000,
                retryPolicy: {
                    maxAttempts: 3,
                    backoff: 'exponential',
                    maxDelay: 5000,
                },
                security: {
                    authentication: true,
                    ssl: true,
                    allowedOrigins: '*',
                    rateLimit: {
                        requestsPerMinute: 1000,
                        burstSize: 100,
                    },
                },
            });
        }
    }, [server, reset]);
    // Mutations
    const createMutation = (0, react_query_1.useMutation)({
        mutationFn: async (data) => {
            const response = await api.post('/api/mcp/servers', {
                ...data,
                security: {
                    ...data.security,
                    allowedOrigins: data.security.allowedOrigins
                        .split('\n')
                        .map((origin) => origin.trim())
                        .filter(Boolean),
                },
            });
            return response.data;
        },
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['mcp-servers'] });
            toast({
                title: 'Server created',
                status: 'success',
                duration: 3000,
            });
            onClose();
        },
        onError: (error) => {
            toast({
                title: 'Error creating server',
                description: error.message,
                status: 'error',
                duration: 5000,
            });
        },
    });
    const updateMutation = (0, react_query_1.useMutation)({
        mutationFn: async (data) => {
            if (!server)
                throw new Error('No server selected');
            const response = await api.put(`/api/mcp/servers/${server.id}`, {
                ...data,
                security: {
                    ...data.security,
                    allowedOrigins: data.security.allowedOrigins
                        .split('\n')
                        .map((origin) => origin.trim())
                        .filter(Boolean),
                },
            });
            return response.data;
        },
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['mcp-servers'] });
            toast({
                title: 'Server updated',
                status: 'success',
                duration: 3000,
            });
            onClose();
        },
        onError: (error) => {
            toast({
                title: 'Error updating server',
                description: error.message,
                status: 'error',
                duration: 5000,
            });
        },
    });
    const onSubmit = async (data) => {
        setIsSubmitting(true);
        try {
            if (server) {
                await updateMutation.mutateAsync(data);
            }
            else {
                await createMutation.mutateAsync(data);
            }
        }
        finally {
            setIsSubmitting(false);
        }
    };
    return ((0, jsx_runtime_1.jsxs)(react_2.Modal, { isOpen: isOpen, onClose: onClose, size: "xl", children: [(0, jsx_runtime_1.jsx)(react_2.ModalOverlay, {}), (0, jsx_runtime_1.jsx)(react_2.ModalContent, { children: (0, jsx_runtime_1.jsxs)("form", { onSubmit: handleSubmit(onSubmit), children: [(0, jsx_runtime_1.jsx)(react_2.ModalHeader, { children: server ? 'Edit Server Configuration' : 'Add New Server' }), (0, jsx_runtime_1.jsx)(react_2.ModalCloseButton, {}), (0, jsx_runtime_1.jsx)(react_2.ModalBody, { children: (0, jsx_runtime_1.jsxs)(react_2.Stack, { spacing: 4, children: [(0, jsx_runtime_1.jsxs)(react_2.FormControl, { isInvalid: !!errors.name, isRequired: true, children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Server Name" }), (0, jsx_runtime_1.jsx)(react_hook_form_1.Controller, { name: "name", control: control, rules: { required: 'Server name is required' }, render: ({ field }) => (0, jsx_runtime_1.jsx)(react_2.Input, { ...field }) }), (0, jsx_runtime_1.jsx)(react_2.FormErrorMessage, { children: errors.name?.message })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { isInvalid: !!errors.endpoint, isRequired: true, children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Endpoint URL" }), (0, jsx_runtime_1.jsx)(react_hook_form_1.Controller, { name: "endpoint", control: control, rules: {
                                                    required: 'Endpoint URL is required',
                                                    pattern: {
                                                        value: /^https?:\/\/.+/,
                                                        message: 'Must be a valid URL',
                                                    },
                                                }, render: ({ field }) => (0, jsx_runtime_1.jsx)(react_2.Input, { ...field }) }), (0, jsx_runtime_1.jsx)(react_2.FormErrorMessage, { children: errors.endpoint?.message })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { isInvalid: !!errors.version, isRequired: true, children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Version" }), (0, jsx_runtime_1.jsx)(react_hook_form_1.Controller, { name: "version", control: control, rules: { required: 'Version is required' }, render: ({ field }) => (0, jsx_runtime_1.jsx)(react_2.Input, { ...field }) }), (0, jsx_runtime_1.jsx)(react_2.FormErrorMessage, { children: errors.version?.message })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { isInvalid: !!errors.maxConcurrentRequests, children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Max Concurrent Requests" }), (0, jsx_runtime_1.jsx)(react_hook_form_1.Controller, { name: "maxConcurrentRequests", control: control, render: ({ field }) => ((0, jsx_runtime_1.jsxs)(react_2.NumberInput, { min: 1, max: 1000, ...field, children: [(0, jsx_runtime_1.jsx)(react_2.NumberInputField, {}), (0, jsx_runtime_1.jsxs)(react_2.NumberInputStepper, { children: [(0, jsx_runtime_1.jsx)(react_2.NumberIncrementStepper, {}), (0, jsx_runtime_1.jsx)(react_2.NumberDecrementStepper, {})] })] })) })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { isInvalid: !!errors.timeout, children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Timeout (ms)" }), (0, jsx_runtime_1.jsx)(react_hook_form_1.Controller, { name: "timeout", control: control, render: ({ field }) => ((0, jsx_runtime_1.jsxs)(react_2.NumberInput, { min: 1000, max: 60000, step: 1000, ...field, children: [(0, jsx_runtime_1.jsx)(react_2.NumberInputField, {}), (0, jsx_runtime_1.jsxs)(react_2.NumberInputStepper, { children: [(0, jsx_runtime_1.jsx)(react_2.NumberIncrementStepper, {}), (0, jsx_runtime_1.jsx)(react_2.NumberDecrementStepper, {})] })] })) })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Retry Policy" }), (0, jsx_runtime_1.jsxs)(react_2.Stack, { spacing: 2, children: [(0, jsx_runtime_1.jsx)(react_hook_form_1.Controller, { name: "retryPolicy.maxAttempts", control: control, render: ({ field }) => ((0, jsx_runtime_1.jsxs)(react_2.NumberInput, { min: 1, max: 10, ...field, children: [(0, jsx_runtime_1.jsx)(react_2.NumberInputField, { placeholder: "Max Attempts" }), (0, jsx_runtime_1.jsxs)(react_2.NumberInputStepper, { children: [(0, jsx_runtime_1.jsx)(react_2.NumberIncrementStepper, {}), (0, jsx_runtime_1.jsx)(react_2.NumberDecrementStepper, {})] })] })) }), (0, jsx_runtime_1.jsx)(react_hook_form_1.Controller, { name: "retryPolicy.backoff", control: control, render: ({ field }) => ((0, jsx_runtime_1.jsxs)(react_2.Select, { ...field, children: [(0, jsx_runtime_1.jsx)("option", { value: "linear", children: "Linear" }), (0, jsx_runtime_1.jsx)("option", { value: "exponential", children: "Exponential" })] })) }), (0, jsx_runtime_1.jsx)(react_hook_form_1.Controller, { name: "retryPolicy.maxDelay", control: control, render: ({ field }) => ((0, jsx_runtime_1.jsxs)(react_2.NumberInput, { min: 1000, max: 30000, step: 1000, ...field, children: [(0, jsx_runtime_1.jsx)(react_2.NumberInputField, { placeholder: "Max Delay (ms)" }), (0, jsx_runtime_1.jsxs)(react_2.NumberInputStepper, { children: [(0, jsx_runtime_1.jsx)(react_2.NumberIncrementStepper, {}), (0, jsx_runtime_1.jsx)(react_2.NumberDecrementStepper, {})] })] })) })] })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Security Settings" }), (0, jsx_runtime_1.jsxs)(react_2.Stack, { spacing: 4, children: [(0, jsx_runtime_1.jsx)(react_hook_form_1.Controller, { name: "security.authentication", control: control, render: ({ field }) => ((0, jsx_runtime_1.jsxs)(react_2.FormControl, { display: "flex", alignItems: "center", children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { mb: "0", children: "Enable Authentication" }), (0, jsx_runtime_1.jsx)(react_2.Switch, { ...field, isChecked: field.value })] })) }), (0, jsx_runtime_1.jsx)(react_hook_form_1.Controller, { name: "security.ssl", control: control, render: ({ field }) => ((0, jsx_runtime_1.jsxs)(react_2.FormControl, { display: "flex", alignItems: "center", children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { mb: "0", children: "Enable SSL" }), (0, jsx_runtime_1.jsx)(react_2.Switch, { ...field, isChecked: field.value })] })) }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Allowed Origins" }), (0, jsx_runtime_1.jsx)(react_hook_form_1.Controller, { name: "security.allowedOrigins", control: control, render: ({ field }) => ((0, jsx_runtime_1.jsx)(react_2.Textarea, { ...field, placeholder: "Enter allowed origins (one per line)", rows: 3 })) })] }), (0, jsx_runtime_1.jsxs)(react_2.Stack, { direction: "row", spacing: 4, children: [(0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Rate Limit (req/min)" }), (0, jsx_runtime_1.jsx)(react_hook_form_1.Controller, { name: "security.rateLimit.requestsPerMinute", control: control, render: ({ field }) => ((0, jsx_runtime_1.jsxs)(react_2.NumberInput, { min: 1, max: 10000, ...field, children: [(0, jsx_runtime_1.jsx)(react_2.NumberInputField, {}), (0, jsx_runtime_1.jsxs)(react_2.NumberInputStepper, { children: [(0, jsx_runtime_1.jsx)(react_2.NumberIncrementStepper, {}), (0, jsx_runtime_1.jsx)(react_2.NumberDecrementStepper, {})] })] })) })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Burst Size" }), (0, jsx_runtime_1.jsx)(react_hook_form_1.Controller, { name: "security.rateLimit.burstSize", control: control, render: ({ field }) => ((0, jsx_runtime_1.jsxs)(react_2.NumberInput, { min: 1, max: 1000, ...field, children: [(0, jsx_runtime_1.jsx)(react_2.NumberInputField, {}), (0, jsx_runtime_1.jsxs)(react_2.NumberInputStepper, { children: [(0, jsx_runtime_1.jsx)(react_2.NumberIncrementStepper, {}), (0, jsx_runtime_1.jsx)(react_2.NumberDecrementStepper, {})] })] })) })] })] })] })] })] }) }), (0, jsx_runtime_1.jsxs)(react_2.ModalFooter, { children: [(0, jsx_runtime_1.jsx)(react_2.Button, { variant: "ghost", mr: 3, onClick: onClose, children: "Cancel" }), (0, jsx_runtime_1.jsx)(react_2.Button, { colorScheme: "blue", type: "submit", isLoading: isSubmitting, loadingText: server ? 'Updating...' : 'Creating...', children: server ? 'Update Server' : 'Create Server' })] })] }) })] }));
};
exports.ServerConfigModal = ServerConfigModal;
exports.default = exports.ServerConfigModal;
//# sourceMappingURL=ServerConfigModal.js.map