'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect } from 'react';
import { ShieldAlert } from 'lucide-react';
import { SettingsSaveBar, SettingsValidationSummary } from './components.js';
import { AccessProtectionCard } from './AccessProtectionCard.js';
import { AuthMfaCard } from './AuthMfaCard.js';
import { SecretsAuditCard } from './SecretsAuditCard.js';
import { SecurityStatusCard } from './SecurityStatusCard.js';
import { SessionPolicyCard } from './SessionPolicyCard.js';
import { useSecuritySettings } from './useSecuritySettings.js';
function CardSkeleton() {
    return (_jsxs("div", { className: "border-border bg-card animate-pulse rounded-lg border p-6", children: [_jsx("div", { className: "bg-muted mb-3 h-4 w-40 rounded" }), _jsx("div", { className: "bg-muted mb-4 h-3 w-72 rounded" }), _jsxs("div", { className: "space-y-3", children: [_jsx("div", { className: "bg-muted h-3 w-full rounded" }), _jsx("div", { className: "bg-muted h-3 w-2/3 rounded" })] })] }));
}
export function SecurityTab({ role, onNavigate, }) {
    const canEdit = role === 'ADMIN';
    const { loading, loadError, data, form, setField, dirty, issues, hasErrors, saveState, save, reset, revokeState, revokeAllOtherSessions, } = useSecuritySettings(canEdit);
    // Deep-link support: /settings?tab=security&section=sessions|ip-allowlist|audit
    useEffect(() => {
        if (loading || typeof window === 'undefined')
            return;
        const section = new URLSearchParams(window.location.search).get('section');
        if (!section)
            return;
        const el = document.getElementById(`security-${section}`);
        el?.scrollIntoView({ behavior: 'smooth', block: 'start' });
    }, [loading]);
    if (loading) {
        return (_jsxs("div", { className: "space-y-4", children: [_jsx(CardSkeleton, {}), _jsx(CardSkeleton, {}), _jsx(CardSkeleton, {})] }));
    }
    if (loadError || !data) {
        const forbidden = /forbidden|permission|insufficient/i.test(loadError ?? '');
        return (_jsxs("div", { className: "border-border bg-card text-muted-foreground flex items-start gap-3 rounded-lg border p-6 text-sm", children: [_jsx(ShieldAlert, { size: 20, className: "text-warning mt-0.5 shrink-0", "aria-hidden": "true" }), _jsxs("div", { children: [_jsx("p", { className: "text-foreground font-medium", children: forbidden
                                ? 'You do not have access to security settings'
                                : 'Security settings unavailable' }), _jsx("p", { className: "mt-1", children: forbidden
                                ? 'Ask an administrator to review the Security tab.'
                                : (loadError ?? 'The security service could not be reached. Try again shortly.') })] })] }));
    }
    return (_jsxs("div", { className: "space-y-4 pb-4", children: [_jsx(SecurityStatusCard, { status: data.status, onNavigate: onNavigate }), issues.length > 0 && _jsx(SettingsValidationSummary, { issues: issues }), !canEdit && (_jsx("p", { className: "text-muted-foreground text-sm", children: "You have read-only access. Only administrators can change security settings." })), _jsx(AuthMfaCard, { form: form, setField: setField, canEdit: canEdit, viewerHasMfa: data.viewerHasMfa, withMfa: data.users.withMfa, totalAdmins: data.users.totalAdmins, onNavigate: onNavigate }), _jsx("div", { id: "security-sessions", children: _jsx(SessionPolicyCard, { form: form, setField: setField, canEdit: canEdit, sessionMaxAgeHours: data.sessionMaxAgeHours, activeSessions: data.activeSessions, revokeState: revokeState, onRevokeAll: () => void revokeAllOtherSessions() }) }), _jsx("div", { id: "security-ip-allowlist", children: _jsx(AccessProtectionCard, { ipAllowlist: data.ipAllowlist, secrets: data.secrets, csrfEnabled: data.csrfEnabled, uploadValidation: data.uploadValidation, sources: data.sources, onNavigate: onNavigate }) }), _jsx("div", { id: "security-audit", children: _jsx(SecretsAuditCard, { secrets: data.secrets, auditEnabled: data.auditEnabled, failedLogins24h: data.failedLogins24h, users: data.users, apiKeys: data.apiKeys, sources: data.sources, onNavigate: onNavigate }) }), saveState === 'error' && (_jsx("p", { className: "text-destructive text-sm", role: "alert", children: "Failed to save security settings. Please try again." })), _jsx(SettingsSaveBar, { dirty: dirty, canEdit: canEdit, hasErrors: hasErrors, saveState: saveState, onSave: () => void save(), onReset: reset })] }));
}
//# sourceMappingURL=SecurityTab.js.map