"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeConfigModal = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
/**
 * Node Configuration Modal Component
 * Handles configuration of different workflow node types
 */
const react_1 = require("react");
const react_2 = require("@chakra-ui/react");
const useApi_1 = require("../../hooks/useApi");
const NodeConfigModal = ({ isOpen, onClose, onSave, node, readOnly = false, }) => {
    const api = (0, useApi_1.useApi)();
    const toast = (0, react_2.useToast)();
    const [formData, setFormData] = (0, react_1.useState)({});
    const [securityConfig, setSecurityConfig] = (0, react_1.useState)({
        crowdstrikePolicy: '',
        requireAuth: false,
        allowedRoles: [],
        ipWhitelist: [],
        auditLogging: true,
    });
    const [adminConfig, setAdminConfig] = (0, react_1.useState)({
        requireApproval: false,
        approvers: [],
        timeout: 3600,
        fallbackAction: 'skip',
        notifyOnComplete: true,
    });
    const bgColor = (0, react_2.useColorModeValue)('white', 'gray.800');
    const borderColor = (0, react_2.useColorModeValue)('gray.200', 'gray.700');
    (0, react_1.useEffect)(() => {
        if (node) {
            setFormData(node);
            if (node.type === 'security') {
                setSecurityConfig(node.config);
            }
            else if (node.type === 'admin') {
                setAdminConfig(node.config);
            }
        }
    }, [node]);
    const handleSave = async () => {
        try {
            const updatedNode = {
                ...formData,
                id: node?.id || `node-${Date.now()}`,
                type: formData.type || 'action',
                config: formData.type === 'security'
                    ? securityConfig
                    : formData.type === 'admin'
                        ? adminConfig
                        : formData.config,
            };
            // Validate node configuration
            if (updatedNode.type === 'security') {
                // Validate CrowdStrike policy
                const response = await api.get(`/api/security/policies/${securityConfig.crowdstrikePolicy}`);
                if (!response.data) {
                    throw new Error('Invalid CrowdStrike policy');
                }
            }
            onSave(updatedNode);
            onClose();
            toast({
                title: 'Node configuration saved',
                status: 'success',
                duration: 3000,
            });
        }
        catch (error) {
            toast({
                title: 'Failed to save node configuration',
                description: error.message,
                status: 'error',
                duration: 5000,
            });
        }
    };
    const renderBasicConfig = () => ((0, jsx_runtime_1.jsxs)(react_2.VStack, { spacing: 4, align: "stretch", children: [(0, jsx_runtime_1.jsxs)(react_2.FormControl, { isRequired: true, children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Node Label" }), (0, jsx_runtime_1.jsx)(react_2.Input, { value: formData.label || '', onChange: (e) => setFormData({ ...formData, label: e.target.value }), placeholder: "Enter node label", isReadOnly: readOnly })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Description" }), (0, jsx_runtime_1.jsx)(react_2.Textarea, { value: formData.description || '', onChange: (e) => setFormData({ ...formData, description: e.target.value }), placeholder: "Enter node description", isReadOnly: readOnly })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Status" }), (0, jsx_runtime_1.jsxs)(react_2.Select, { value: formData.status || 'active', onChange: (e) => setFormData({ ...formData, status: e.target.value }), isReadOnly: readOnly, children: [(0, jsx_runtime_1.jsx)("option", { value: "active", children: "Active" }), (0, jsx_runtime_1.jsx)("option", { value: "inactive", children: "Inactive" }), (0, jsx_runtime_1.jsx)("option", { value: "draft", children: "Draft" })] })] })] }));
    const renderSecurityConfig = () => ((0, jsx_runtime_1.jsxs)(react_2.VStack, { spacing: 4, align: "stretch", children: [(0, jsx_runtime_1.jsxs)(react_2.FormControl, { isRequired: true, children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "CrowdStrike Policy" }), (0, jsx_runtime_1.jsxs)(react_2.Select, { value: securityConfig.crowdstrikePolicy, onChange: (e) => setSecurityConfig({
                            ...securityConfig,
                            crowdstrikePolicy: e.target.value,
                        }), isReadOnly: readOnly, children: [(0, jsx_runtime_1.jsx)("option", { value: "", children: "Select a policy" }), (0, jsx_runtime_1.jsx)("option", { value: "high_security", children: "High Security" }), (0, jsx_runtime_1.jsx)("option", { value: "standard", children: "Standard" }), (0, jsx_runtime_1.jsx)("option", { value: "custom", children: "Custom" })] })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Authentication" }), (0, jsx_runtime_1.jsxs)(react_2.HStack, { children: [(0, jsx_runtime_1.jsx)(react_2.Switch, { isChecked: securityConfig.requireAuth, onChange: (e) => setSecurityConfig({
                                    ...securityConfig,
                                    requireAuth: e.target.checked,
                                }), isReadOnly: readOnly }), (0, jsx_runtime_1.jsx)(react_2.Text, { children: "Require Authentication" })] })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Allowed Roles" }), (0, jsx_runtime_1.jsxs)(react_2.Select, { value: securityConfig.allowedRoles[0] || '', onChange: (e) => setSecurityConfig({
                            ...securityConfig,
                            allowedRoles: [e.target.value],
                        }), isReadOnly: readOnly, isMulti: true, children: [(0, jsx_runtime_1.jsx)("option", { value: "admin", children: "Admin" }), (0, jsx_runtime_1.jsx)("option", { value: "security", children: "Security Team" }), (0, jsx_runtime_1.jsx)("option", { value: "user", children: "Standard User" })] })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "IP Whitelist" }), (0, jsx_runtime_1.jsx)(react_2.Textarea, { value: securityConfig.ipWhitelist.join('\n'), onChange: (e) => setSecurityConfig({
                            ...securityConfig,
                            ipWhitelist: e.target.value.split('\n').filter(Boolean),
                        }), placeholder: "Enter IP addresses (one per line)", isReadOnly: readOnly })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Audit Logging" }), (0, jsx_runtime_1.jsxs)(react_2.HStack, { children: [(0, jsx_runtime_1.jsx)(react_2.Switch, { isChecked: securityConfig.auditLogging, onChange: (e) => setSecurityConfig({
                                    ...securityConfig,
                                    auditLogging: e.target.checked,
                                }), isReadOnly: readOnly }), (0, jsx_runtime_1.jsx)(react_2.Text, { children: "Enable audit logging" })] })] })] }));
    const renderAdminConfig = () => ((0, jsx_runtime_1.jsxs)(react_2.VStack, { spacing: 4, align: "stretch", children: [(0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Approval Required" }), (0, jsx_runtime_1.jsxs)(react_2.HStack, { children: [(0, jsx_runtime_1.jsx)(react_2.Switch, { isChecked: adminConfig.requireApproval, onChange: (e) => setAdminConfig({
                                    ...adminConfig,
                                    requireApproval: e.target.checked,
                                }), isReadOnly: readOnly }), (0, jsx_runtime_1.jsx)(react_2.Text, { children: "Require approval before execution" })] })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Approvers" }), (0, jsx_runtime_1.jsxs)(react_2.Select, { value: adminConfig.approvers[0] || '', onChange: (e) => setAdminConfig({
                            ...adminConfig,
                            approvers: [e.target.value],
                        }), isReadOnly: readOnly, isMulti: true, children: [(0, jsx_runtime_1.jsx)("option", { value: "admin", children: "System Admin" }), (0, jsx_runtime_1.jsx)("option", { value: "security_admin", children: "Security Admin" }), (0, jsx_runtime_1.jsx)("option", { value: "workflow_admin", children: "Workflow Admin" })] })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Approval Timeout (seconds)" }), (0, jsx_runtime_1.jsx)(react_2.Input, { type: "number", value: adminConfig.timeout, onChange: (e) => setAdminConfig({
                            ...adminConfig,
                            timeout: parseInt(e.target.value, 10),
                        }), isReadOnly: readOnly })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Fallback Action" }), (0, jsx_runtime_1.jsxs)(react_2.Select, { value: adminConfig.fallbackAction, onChange: (e) => setAdminConfig({
                            ...adminConfig,
                            fallbackAction: e.target.value,
                        }), isReadOnly: readOnly, children: [(0, jsx_runtime_1.jsx)("option", { value: "skip", children: "Skip" }), (0, jsx_runtime_1.jsx)("option", { value: "fail", children: "Fail" }), (0, jsx_runtime_1.jsx)("option", { value: "retry", children: "Retry" })] })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Notifications" }), (0, jsx_runtime_1.jsxs)(react_2.HStack, { children: [(0, jsx_runtime_1.jsx)(react_2.Switch, { isChecked: adminConfig.notifyOnComplete, onChange: (e) => setAdminConfig({
                                    ...adminConfig,
                                    notifyOnComplete: e.target.checked,
                                }), isReadOnly: readOnly }), (0, jsx_runtime_1.jsx)(react_2.Text, { children: "Notify on completion" })] })] })] }));
    return ((0, jsx_runtime_1.jsxs)(react_2.Modal, { isOpen: isOpen, onClose: onClose, size: "xl", scrollBehavior: "inside", children: [(0, jsx_runtime_1.jsx)(react_2.ModalOverlay, {}), (0, jsx_runtime_1.jsxs)(react_2.ModalContent, { bg: bgColor, borderColor: borderColor, children: [(0, jsx_runtime_1.jsx)(react_2.ModalHeader, { children: node ? 'Edit Node' : 'Create Node' }), (0, jsx_runtime_1.jsx)(react_2.ModalCloseButton, {}), (0, jsx_runtime_1.jsx)(react_2.ModalBody, { children: (0, jsx_runtime_1.jsxs)(react_2.Tabs, { children: [(0, jsx_runtime_1.jsxs)(react_2.TabList, { children: [(0, jsx_runtime_1.jsx)(react_2.Tab, { children: "Basic" }), formData.type === 'security' && (0, jsx_runtime_1.jsx)(react_2.Tab, { children: "Security" }), formData.type === 'admin' && (0, jsx_runtime_1.jsx)(react_2.Tab, { children: "Admin" }), (0, jsx_runtime_1.jsx)(react_2.Tab, { children: "Advanced" })] }), (0, jsx_runtime_1.jsxs)(react_2.TabPanels, { children: [(0, jsx_runtime_1.jsx)(react_2.TabPanel, { children: renderBasicConfig() }), formData.type === 'security' && ((0, jsx_runtime_1.jsx)(react_2.TabPanel, { children: renderSecurityConfig() })), formData.type === 'admin' && ((0, jsx_runtime_1.jsx)(react_2.TabPanel, { children: renderAdminConfig() })), (0, jsx_runtime_1.jsx)(react_2.TabPanel, { children: (0, jsx_runtime_1.jsx)(react_2.VStack, { spacing: 4, align: "stretch", children: (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Custom Configuration" }), (0, jsx_runtime_1.jsx)(react_2.Code, { p: 4, borderRadius: "md", width: "100%", height: "200px", overflow: "auto", children: JSON.stringify(formData.type === 'security'
                                                                ? securityConfig
                                                                : formData.type === 'admin'
                                                                    ? adminConfig
                                                                    : formData.config, null, 2) })] }) }) })] })] }) }), (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" }), !readOnly && ((0, jsx_runtime_1.jsx)(react_2.Button, { colorScheme: "blue", onClick: handleSave, children: "Save" }))] })] })] }));
};
exports.NodeConfigModal = NodeConfigModal;
exports.default = exports.NodeConfigModal;
//# sourceMappingURL=NodeConfigModal.js.map