'use client';
import { jsx as _jsx } from "react/jsx-runtime";
import { useEffect } from 'react';
import { useAdminRouter } from './router/index.js';
import { ThemeProvider } from './components/ThemeProvider.js';
import { ISOLATION_STYLE, useRobotsNoIndex } from './lib/root-chrome.js';
import { Login } from './views/Login.js';
import { SetupWizard } from './views/SetupWizard.js';
import { ForgotPassword } from './views/ForgotPassword.js';
import { ResetPassword } from './views/ResetPassword.js';
/**
 * Standalone mount for the unauthenticated auth screens on split-login
 * installs: login (+ MFA), setup wizard, forgot/reset password. Pair it with
 * `<AdminRoot loginPath>` so the admin mount itself stops rendering a login
 * form for signed-out visitors.
 *
 * Routes under `basePath`:
 * - `/` — login (or the setup wizard while `setupRequired`)
 * - `/setup` — setup wizard (first-run; falls back to login once setup is done)
 * - `/forgot-password`, `/reset-password?token=…` — recovery flows; emailed
 *   reset/invite links point here when `config.admin.loginPath` is set.
 */
export function LoginRoot({ config, session, basePath = '/securelogin', adminPath = '/admin', setupRequired, onSetupComplete, onLogin, onVerifyTotp, captchaConfig, onAuthenticated, }) {
    const defaultDarkMode = config?.admin?.branding?.darkMode;
    useRobotsNoIndex();
    return (_jsx("div", { style: ISOLATION_STYLE, className: "actuate-admin", children: _jsx(ThemeProvider, { defaultDarkMode: defaultDarkMode, children: _jsx(LoginShell, { config: config, session: session, basePath: basePath, adminPath: adminPath, setupRequired: setupRequired, onSetupComplete: onSetupComplete, onLogin: onLogin, onVerifyTotp: onVerifyTotp, captchaConfig: captchaConfig, onAuthenticated: onAuthenticated }) }) }));
}
function LoginShell({ config, session, basePath = '/securelogin', adminPath = '/admin', setupRequired, onSetupComplete, onLogin, onVerifyTotp, captchaConfig, onAuthenticated, }) {
    const { navigate, matchRoute } = useAdminRouter(basePath, '/');
    // Already signed in (or just signed in): hand off to the admin app. The
    // admin is a separate route mount, so this must be a full-page navigation.
    const authenticated = Boolean(session) && !setupRequired;
    useEffect(() => {
        if (!authenticated)
            return;
        if (onAuthenticated) {
            onAuthenticated(adminPath);
            return;
        }
        if (typeof window !== 'undefined') {
            window.location.replace(adminPath);
        }
    }, [authenticated, adminPath, onAuthenticated]);
    if (authenticated) {
        return (_jsx("div", { className: "bg-background flex min-h-screen items-center justify-center", children: _jsx("p", { className: "text-muted-foreground text-sm", role: "status", children: "Signed in \u2014 taking you to the admin\u2026" }) }));
    }
    if (setupRequired && onSetupComplete) {
        return (_jsx(SetupWizard, { onComplete: onSetupComplete, siteName: config?.admin?.branding?.name ?? 'Actuate CMS' }));
    }
    if (matchRoute('/forgot-password')) {
        return _jsx(ForgotPassword, { onNavigate: navigate });
    }
    if (matchRoute('/reset-password')) {
        const params = new URLSearchParams(typeof window !== 'undefined' ? window.location.search : '');
        return _jsx(ResetPassword, { onNavigate: navigate, token: params.get('token') });
    }
    return (_jsx(Login, { onLogin: onLogin, onVerifyTotp: onVerifyTotp, onNavigate: navigate, captchaConfig: captchaConfig, branding: config?.admin?.branding }));
}
//# sourceMappingURL=LoginRoot.js.map