"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ComplianceSettings = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const react_2 = require("@chakra-ui/react");
const fi_1 = require("react-icons/fi");
const ComplianceSettings = ({ drupalSettings, experienceBuilder, }) => {
    const [settings, setSettings] = (0, react_1.useState)(drupalSettings.defaultSettings);
    const [isSaving, setIsSaving] = (0, react_1.useState)(false);
    const [error, setError] = (0, react_1.useState)(null);
    const toast = (0, react_2.useToast)();
    const bgColor = (0, react_2.useColorModeValue)('white', 'gray.800');
    const borderColor = (0, react_2.useColorModeValue)('gray.200', 'gray.700');
    (0, react_1.useEffect)(() => {
        fetchSettings();
    }, [drupalSettings.userId]);
    const fetchSettings = async () => {
        try {
            const response = await fetch(`${drupalSettings.baseUrl}/api/compliance/settings`, {
                headers: {
                    'X-CSRF-Token': drupalSettings.csrfToken,
                    'Accept': 'application/json',
                },
            });
            if (!response.ok)
                throw new Error('Failed to fetch settings');
            const data = await response.json();
            setSettings(data.settings);
            setError(null);
            // Notify Experience Builder
            experienceBuilder?.onAction?.('settings_loaded', data);
        }
        catch (err) {
            console.error('Error fetching settings:', err);
            setError('Failed to load compliance settings');
        }
    };
    const saveSettings = async () => {
        setIsSaving(true);
        setError(null);
        try {
            const response = await fetch(`${drupalSettings.baseUrl}/api/compliance/settings`, {
                method: 'POST',
                headers: {
                    'X-CSRF-Token': drupalSettings.csrfToken,
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ settings }),
            });
            if (!response.ok)
                throw new Error('Failed to save settings');
            toast({
                title: 'Settings saved',
                description: 'Your compliance settings have been updated successfully.',
                status: 'success',
                duration: 5000,
                isClosable: true,
            });
            // Notify Experience Builder
            experienceBuilder?.onAction?.('settings_saved', { settings });
        }
        catch (err) {
            console.error('Error saving settings:', err);
            setError('Failed to save compliance settings');
            toast({
                title: 'Error',
                description: 'Failed to save compliance settings. Please try again.',
                status: 'error',
                duration: 5000,
                isClosable: true,
            });
        }
        finally {
            setIsSaving(false);
        }
    };
    const handleSettingChange = (category, setting, value) => {
        setSettings(prev => ({
            ...prev,
            [category]: {
                ...prev[category],
                [setting]: value,
            },
        }));
    };
    // If in Experience Builder edit mode, show a simplified view
    if (experienceBuilder?.isEditing) {
        return ((0, jsx_runtime_1.jsxs)(react_2.Box, { p: 4, borderWidth: "2px", borderStyle: "dashed", borderRadius: "lg", children: [(0, jsx_runtime_1.jsx)(react_2.Text, { children: "Compliance Settings" }), (0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "sm", color: "gray.500", children: "This component will display compliance settings when placed on a page." })] }));
    }
    return ((0, jsx_runtime_1.jsxs)(react_2.Box, { p: 6, bg: bgColor, borderRadius: "lg", borderWidth: "1px", borderColor: borderColor, "data-eb-block-id": experienceBuilder?.blockId, children: [error && ((0, jsx_runtime_1.jsxs)(react_2.Alert, { status: "error", mb: 4, children: [(0, jsx_runtime_1.jsx)(react_2.AlertIcon, {}), (0, jsx_runtime_1.jsx)(react_2.AlertTitle, { children: "Error" }), (0, jsx_runtime_1.jsx)(react_2.AlertDescription, { children: error })] })), (0, jsx_runtime_1.jsxs)(react_2.Tabs, { variant: "enclosed", colorScheme: "blue", children: [(0, jsx_runtime_1.jsxs)(react_2.TabList, { children: [(0, jsx_runtime_1.jsx)(react_2.Tab, { children: (0, jsx_runtime_1.jsxs)(react_2.HStack, { children: [(0, jsx_runtime_1.jsx)(fi_1.FiBell, {}), (0, jsx_runtime_1.jsx)(react_2.Text, { children: "Notifications" })] }) }), (0, jsx_runtime_1.jsx)(react_2.Tab, { children: (0, jsx_runtime_1.jsxs)(react_2.HStack, { children: [(0, jsx_runtime_1.jsx)(fi_1.FiShield, {}), (0, jsx_runtime_1.jsx)(react_2.Text, { children: "Thresholds" })] }) }), (0, jsx_runtime_1.jsx)(react_2.Tab, { children: (0, jsx_runtime_1.jsxs)(react_2.HStack, { children: [(0, jsx_runtime_1.jsx)(fi_1.FiActivity, {}), (0, jsx_runtime_1.jsx)(react_2.Text, { children: "Monitoring" })] }) }), (0, jsx_runtime_1.jsx)(react_2.Tab, { children: (0, jsx_runtime_1.jsxs)(react_2.HStack, { children: [(0, jsx_runtime_1.jsx)(fi_1.FiAlertCircle, {}), (0, jsx_runtime_1.jsx)(react_2.Text, { children: "Reporting" })] }) })] }), (0, jsx_runtime_1.jsxs)(react_2.TabPanels, { children: [(0, jsx_runtime_1.jsx)(react_2.TabPanel, { children: (0, jsx_runtime_1.jsxs)(react_2.VStack, { spacing: 4, align: "stretch", children: [(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 Notifications" }), (0, jsx_runtime_1.jsx)(react_2.Switch, { isChecked: settings.notifications.enableNotifications, onChange: (e) => handleSettingChange('notifications', 'enableNotifications', e.target.checked) })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { display: "flex", alignItems: "center", children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { mb: "0", children: "Critical Alerts" }), (0, jsx_runtime_1.jsx)(react_2.Switch, { isChecked: settings.notifications.notifyOnCritical, onChange: (e) => handleSettingChange('notifications', 'notifyOnCritical', e.target.checked), isDisabled: !settings.notifications.enableNotifications })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { display: "flex", alignItems: "center", children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { mb: "0", children: "High Severity Alerts" }), (0, jsx_runtime_1.jsx)(react_2.Switch, { isChecked: settings.notifications.notifyOnHigh, onChange: (e) => handleSettingChange('notifications', 'notifyOnHigh', e.target.checked), isDisabled: !settings.notifications.enableNotifications })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { display: "flex", alignItems: "center", children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { mb: "0", children: "Score Changes" }), (0, jsx_runtime_1.jsx)(react_2.Switch, { isChecked: settings.notifications.notifyOnScoreChange, onChange: (e) => handleSettingChange('notifications', 'notifyOnScoreChange', e.target.checked), isDisabled: !settings.notifications.enableNotifications })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Refresh Interval (seconds)" }), (0, jsx_runtime_1.jsxs)(react_2.NumberInput, { value: settings.notifications.refreshInterval, onChange: (_, value) => handleSettingChange('notifications', 'refreshInterval', value), min: 30, max: 300, step: 30, 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.jsx)(react_2.TabPanel, { children: (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: "Critical Threshold" }), (0, jsx_runtime_1.jsxs)(react_2.NumberInput, { value: settings.thresholds.criticalThreshold, onChange: (_, value) => handleSettingChange('thresholds', 'criticalThreshold', value), min: 0, max: 100, 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: "High Threshold" }), (0, jsx_runtime_1.jsxs)(react_2.NumberInput, { value: settings.thresholds.highThreshold, onChange: (_, value) => handleSettingChange('thresholds', 'highThreshold', value), min: 0, max: 100, 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: "Medium Threshold" }), (0, jsx_runtime_1.jsxs)(react_2.NumberInput, { value: settings.thresholds.mediumThreshold, onChange: (_, value) => handleSettingChange('thresholds', 'mediumThreshold', value), min: 0, max: 100, 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: "Low Threshold" }), (0, jsx_runtime_1.jsxs)(react_2.NumberInput, { value: settings.thresholds.lowThreshold, onChange: (_, value) => handleSettingChange('thresholds', 'lowThreshold', value), min: 0, max: 100, 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.jsx)(react_2.TabPanel, { children: (0, jsx_runtime_1.jsxs)(react_2.VStack, { spacing: 4, align: "stretch", children: [(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 Real-time Monitoring" }), (0, jsx_runtime_1.jsx)(react_2.Switch, { isChecked: settings.monitoring.enableRealTimeMonitoring, onChange: (e) => handleSettingChange('monitoring', 'enableRealTimeMonitoring', e.target.checked) })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Monitoring Interval (seconds)" }), (0, jsx_runtime_1.jsxs)(react_2.NumberInput, { value: settings.monitoring.monitoringInterval, onChange: (_, value) => handleSettingChange('monitoring', 'monitoringInterval', value), min: 30, max: 300, step: 30, isDisabled: !settings.monitoring.enableRealTimeMonitoring, 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, { display: "flex", alignItems: "center", children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { mb: "0", children: "Store Historical Data" }), (0, jsx_runtime_1.jsx)(react_2.Switch, { isChecked: settings.monitoring.storeHistoricalData, onChange: (e) => handleSettingChange('monitoring', 'storeHistoricalData', e.target.checked) })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Data Retention Period (days)" }), (0, jsx_runtime_1.jsxs)(react_2.NumberInput, { value: settings.monitoring.retentionPeriod, onChange: (_, value) => handleSettingChange('monitoring', 'retentionPeriod', value), min: 30, max: 365, step: 30, isDisabled: !settings.monitoring.storeHistoricalData, 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.jsx)(react_2.TabPanel, { children: (0, jsx_runtime_1.jsxs)(react_2.VStack, { spacing: 4, align: "stretch", children: [(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 Automatic Reports" }), (0, jsx_runtime_1.jsx)(react_2.Switch, { isChecked: settings.reporting.enableAutomaticReports, onChange: (e) => handleSettingChange('reporting', 'enableAutomaticReports', e.target.checked) })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Report Frequency" }), (0, jsx_runtime_1.jsxs)(react_2.Select, { value: settings.reporting.reportFrequency, onChange: (e) => handleSettingChange('reporting', 'reportFrequency', e.target.value), isDisabled: !settings.reporting.enableAutomaticReports, children: [(0, jsx_runtime_1.jsx)("option", { value: "daily", children: "Daily" }), (0, jsx_runtime_1.jsx)("option", { value: "weekly", children: "Weekly" }), (0, jsx_runtime_1.jsx)("option", { value: "monthly", children: "Monthly" })] })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Report Format" }), (0, jsx_runtime_1.jsxs)(react_2.Select, { value: settings.reporting.reportFormat, onChange: (e) => handleSettingChange('reporting', 'reportFormat', e.target.value), isDisabled: !settings.reporting.enableAutomaticReports, children: [(0, jsx_runtime_1.jsx)("option", { value: "pdf", children: "PDF" }), (0, jsx_runtime_1.jsx)("option", { value: "csv", children: "CSV" }), (0, jsx_runtime_1.jsx)("option", { value: "json", children: "JSON" })] })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { display: "flex", alignItems: "center", children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { mb: "0", children: "Include Trends" }), (0, jsx_runtime_1.jsx)(react_2.Switch, { isChecked: settings.reporting.includeTrends, onChange: (e) => handleSettingChange('reporting', 'includeTrends', e.target.checked), isDisabled: !settings.reporting.enableAutomaticReports })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { display: "flex", alignItems: "center", children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { mb: "0", children: "Include Recommendations" }), (0, jsx_runtime_1.jsx)(react_2.Switch, { isChecked: settings.reporting.includeRecommendations, onChange: (e) => handleSettingChange('reporting', 'includeRecommendations', e.target.checked), isDisabled: !settings.reporting.enableAutomaticReports })] })] }) })] })] }), (0, jsx_runtime_1.jsx)(react_2.Divider, { my: 6 }), (0, jsx_runtime_1.jsx)(react_2.HStack, { justify: "flex-end", spacing: 4, children: (0, jsx_runtime_1.jsx)(react_2.Button, { onClick: saveSettings, isLoading: isSaving, loadingText: "Saving...", leftIcon: (0, jsx_runtime_1.jsx)(fi_1.FiSave, {}), colorScheme: "blue", children: "Save Settings" }) })] }));
};
exports.ComplianceSettings = ComplianceSettings;
//# sourceMappingURL=ComplianceSettings.js.map