"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("@testing-library/react");
const react_2 = require("@chakra-ui/react");
const ComplianceSettings_1 = require("../ComplianceSettings");
const vitest_1 = require("vitest");
// Mock fetch
const mockFetch = vitest_1.vi.fn();
global.fetch = mockFetch;
// Mock toast
const mockToast = vitest_1.vi.fn();
vitest_1.vi.mock('@chakra-ui/react', async () => {
    const actual = await vitest_1.vi.importActual('@chakra-ui/react');
    return {
        ...actual,
        useToast: () => mockToast,
    };
});
const defaultSettings = {
    notifications: {
        enableNotifications: true,
        notifyOnCritical: true,
        notifyOnHigh: true,
        notifyOnScoreChange: true,
        refreshInterval: 60,
    },
    thresholds: {
        criticalThreshold: 90,
        highThreshold: 75,
        mediumThreshold: 50,
        lowThreshold: 25,
    },
    monitoring: {
        enableRealTimeMonitoring: true,
        monitoringInterval: 60,
        storeHistoricalData: true,
        retentionPeriod: 90,
    },
    reporting: {
        enableAutomaticReports: true,
        reportFrequency: 'weekly',
        reportFormat: 'pdf',
        includeTrends: true,
        includeRecommendations: true,
    },
    regions: {
        enabledRegions: ['us', 'eu'],
        defaultRegion: 'us',
        regionSpecificRules: true,
    },
};
const mockDrupalSettings = {
    baseUrl: 'http://example.com',
    csrfToken: 'test-token',
    userId: 'test-user',
    defaultSettings,
};
const renderComponent = (props = {}) => {
    return (0, react_1.render)((0, jsx_runtime_1.jsx)(react_2.ChakraProvider, { children: (0, jsx_runtime_1.jsx)(ComplianceSettings_1.ComplianceSettings, { drupalSettings: mockDrupalSettings, ...props }) }));
};
(0, vitest_1.describe)('ComplianceSettings', () => {
    (0, vitest_1.beforeEach)(() => {
        vitest_1.vi.clearAllMocks();
        mockFetch.mockReset();
    });
    (0, vitest_1.it)('renders in edit mode', () => {
        renderComponent({ experienceBuilder: { isEditing: true } });
        (0, vitest_1.expect)(react_1.screen.getByText('Compliance Settings')).toBeInTheDocument();
        (0, vitest_1.expect)(react_1.screen.getByText(/This component will display compliance settings/)).toBeInTheDocument();
    });
    (0, vitest_1.it)('loads settings on mount', async () => {
        mockFetch.mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ settings: defaultSettings }),
        });
        renderComponent();
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('http://example.com/api/compliance/settings', vitest_1.expect.any(Object));
        });
    });
    (0, vitest_1.it)('handles settings load error', async () => {
        mockFetch.mockRejectedValueOnce(new Error('Failed to fetch'));
        renderComponent();
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(react_1.screen.getByText('Failed to load compliance settings')).toBeInTheDocument();
        });
    });
    (0, vitest_1.it)('saves settings successfully', async () => {
        mockFetch
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ settings: defaultSettings }),
        })
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ success: true }),
        });
        renderComponent();
        // Wait for initial load
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(mockFetch).toHaveBeenCalledTimes(1);
        });
        // Change a setting
        const enableNotificationsSwitch = react_1.screen.getByLabelText('Enable Notifications');
        react_1.fireEvent.click(enableNotificationsSwitch);
        // Save settings
        const saveButton = react_1.screen.getByText('Save Settings');
        react_1.fireEvent.click(saveButton);
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('http://example.com/api/compliance/settings', vitest_1.expect.objectContaining({
                method: 'POST',
                body: JSON.stringify({
                    settings: vitest_1.expect.objectContaining({
                        notifications: vitest_1.expect.objectContaining({
                            enableNotifications: false,
                        }),
                    }),
                }),
            }));
        });
        (0, vitest_1.expect)(mockToast).toHaveBeenCalledWith(vitest_1.expect.objectContaining({
            title: 'Settings saved',
            status: 'success',
        }));
    });
    (0, vitest_1.it)('handles save error', async () => {
        mockFetch
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ settings: defaultSettings }),
        })
            .mockRejectedValueOnce(new Error('Failed to save'));
        renderComponent();
        // Wait for initial load
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(mockFetch).toHaveBeenCalledTimes(1);
        });
        // Save settings
        const saveButton = react_1.screen.getByText('Save Settings');
        react_1.fireEvent.click(saveButton);
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(react_1.screen.getByText('Failed to save compliance settings')).toBeInTheDocument();
        });
        (0, vitest_1.expect)(mockToast).toHaveBeenCalledWith(vitest_1.expect.objectContaining({
            title: 'Error',
            status: 'error',
        }));
    });
    (0, vitest_1.it)('updates threshold values', async () => {
        mockFetch.mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ settings: defaultSettings }),
        });
        renderComponent();
        // Wait for initial load
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(mockFetch).toHaveBeenCalledTimes(1);
        });
        // Switch to thresholds tab
        const thresholdsTab = react_1.screen.getByText('Thresholds');
        react_1.fireEvent.click(thresholdsTab);
        // Update critical threshold
        const criticalThresholdInput = react_1.screen.getByLabelText('Critical Threshold');
        react_1.fireEvent.change(criticalThresholdInput, { target: { value: '95' } });
        // Save settings
        const saveButton = react_1.screen.getByText('Save Settings');
        react_1.fireEvent.click(saveButton);
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('http://example.com/api/compliance/settings', vitest_1.expect.objectContaining({
                method: 'POST',
                body: JSON.stringify({
                    settings: vitest_1.expect.objectContaining({
                        thresholds: vitest_1.expect.objectContaining({
                            criticalThreshold: 95,
                        }),
                    }),
                }),
            }));
        });
    });
    (0, vitest_1.it)('updates monitoring settings', async () => {
        mockFetch.mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ settings: defaultSettings }),
        });
        renderComponent();
        // Wait for initial load
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(mockFetch).toHaveBeenCalledTimes(1);
        });
        // Switch to monitoring tab
        const monitoringTab = react_1.screen.getByText('Monitoring');
        react_1.fireEvent.click(monitoringTab);
        // Toggle real-time monitoring
        const realTimeMonitoringSwitch = react_1.screen.getByLabelText('Enable Real-time Monitoring');
        react_1.fireEvent.click(realTimeMonitoringSwitch);
        // Update monitoring interval
        const monitoringIntervalInput = react_1.screen.getByLabelText('Monitoring Interval (seconds)');
        react_1.fireEvent.change(monitoringIntervalInput, { target: { value: '120' } });
        // Save settings
        const saveButton = react_1.screen.getByText('Save Settings');
        react_1.fireEvent.click(saveButton);
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('http://example.com/api/compliance/settings', vitest_1.expect.objectContaining({
                method: 'POST',
                body: JSON.stringify({
                    settings: vitest_1.expect.objectContaining({
                        monitoring: vitest_1.expect.objectContaining({
                            enableRealTimeMonitoring: false,
                            monitoringInterval: 120,
                        }),
                    }),
                }),
            }));
        });
    });
    (0, vitest_1.it)('updates reporting settings', async () => {
        mockFetch.mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ settings: defaultSettings }),
        });
        renderComponent();
        // Wait for initial load
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(mockFetch).toHaveBeenCalledTimes(1);
        });
        // Switch to reporting tab
        const reportingTab = react_1.screen.getByText('Reporting');
        react_1.fireEvent.click(reportingTab);
        // Update report frequency
        const reportFrequencySelect = react_1.screen.getByLabelText('Report Frequency');
        react_1.fireEvent.change(reportFrequencySelect, { target: { value: 'daily' } });
        // Update report format
        const reportFormatSelect = react_1.screen.getByLabelText('Report Format');
        react_1.fireEvent.change(reportFormatSelect, { target: { value: 'csv' } });
        // Save settings
        const saveButton = react_1.screen.getByText('Save Settings');
        react_1.fireEvent.click(saveButton);
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('http://example.com/api/compliance/settings', vitest_1.expect.objectContaining({
                method: 'POST',
                body: JSON.stringify({
                    settings: vitest_1.expect.objectContaining({
                        reporting: vitest_1.expect.objectContaining({
                            reportFrequency: 'daily',
                            reportFormat: 'csv',
                        }),
                    }),
                }),
            }));
        });
    });
    (0, vitest_1.it)('notifies Experience Builder of actions', async () => {
        const onAction = vitest_1.vi.fn();
        mockFetch.mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ settings: defaultSettings }),
        });
        renderComponent({
            experienceBuilder: {
                onAction,
                isEditing: false,
                blockId: 'test-block',
            },
        });
        // Wait for initial load
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(onAction).toHaveBeenCalledWith('settings_loaded', { settings: defaultSettings });
        });
        // Change a setting
        const enableNotificationsSwitch = react_1.screen.getByLabelText('Enable Notifications');
        react_1.fireEvent.click(enableNotificationsSwitch);
        // Save settings
        const saveButton = react_1.screen.getByText('Save Settings');
        react_1.fireEvent.click(saveButton);
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(onAction).toHaveBeenCalledWith('settings_saved', {
                settings: vitest_1.expect.objectContaining({
                    notifications: vitest_1.expect.objectContaining({
                        enableNotifications: false,
                    }),
                }),
            });
        });
    });
});
//# sourceMappingURL=ComplianceSettings.test.js.map