'use client';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useState } from 'react';
import { Shield, Eye, EyeOff, Loader2, CheckCircle2, AlertTriangle, XCircle } from 'lucide-react';
import { cmsApi } from '../lib/api.js';
const fieldClass = 'w-full rounded-lg border border-border bg-input-background px-3 py-2.5 text-sm text-foreground focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/50 focus-visible:outline-none';
export function ResetPassword({ onNavigate, token }) {
    const [password, setPassword] = useState('');
    const [confirmPassword, setConfirmPassword] = useState('');
    const [showPassword, setShowPassword] = useState(false);
    const [submitting, setSubmitting] = useState(false);
    const [success, setSuccess] = useState(false);
    const [error, setError] = useState('');
    if (!token) {
        return (_jsx("div", { className: "bg-muted flex min-h-screen items-center justify-center px-4", children: _jsxs("div", { className: "w-full max-w-md", children: [_jsxs("div", { className: "mb-8 text-center", children: [_jsx("div", { className: "bg-destructive/10 mx-auto mb-4 flex h-14 w-14 items-center justify-center rounded-xl", children: _jsx(XCircle, { className: "text-destructive h-7 w-7" }) }), _jsx("h1", { className: "text-foreground text-2xl font-medium", children: "Invalid Reset Link" }), _jsx("p", { className: "text-muted-foreground mt-2", children: "This password reset link is invalid or has expired." })] }), _jsxs("div", { className: "border-border bg-card space-y-4 rounded-xl border p-6 text-center shadow-sm", children: [_jsx("button", { type: "button", onClick: () => onNavigate('/forgot-password'), className: "bg-primary text-primary-foreground hover:bg-primary/90 w-full rounded-lg py-2.5 font-medium transition-colors", children: "Request New Reset Link" }), _jsx("button", { type: "button", onClick: () => onNavigate('/login'), className: "text-muted-foreground hover:text-foreground w-full text-sm transition-colors", children: "Back to Sign In" })] })] }) }));
    }
    const passwordsMatch = password === confirmPassword;
    const meetsLength = password.length >= 8;
    const canSubmit = password && confirmPassword && passwordsMatch && meetsLength && !submitting;
    const handleSubmit = async (e) => {
        e.preventDefault();
        if (!canSubmit)
            return;
        setError('');
        setSubmitting(true);
        try {
            const result = await cmsApi('/auth/reset-password', {
                method: 'POST',
                body: JSON.stringify({ token, password }),
            });
            if (result.error) {
                setError(result.error);
            }
            else {
                setSuccess(true);
            }
        }
        catch {
            setError('An unexpected error occurred. Please try again.');
        }
        finally {
            setSubmitting(false);
        }
    };
    return (_jsx("div", { className: "bg-muted flex min-h-screen items-center justify-center px-4", children: _jsxs("div", { className: "w-full max-w-md", children: [_jsxs("div", { className: "mb-8 text-center", children: [_jsx("div", { className: "bg-primary mx-auto mb-4 flex h-14 w-14 items-center justify-center rounded-xl", children: _jsx(Shield, { className: "text-primary-foreground h-7 w-7" }) }), _jsx("h1", { className: "text-foreground text-2xl font-medium", children: success ? 'Password Reset' : 'Choose New Password' }), _jsx("p", { className: "text-muted-foreground mt-2", children: success ? 'Your password has been updated' : 'Enter your new password below' })] }), _jsx("div", { className: "border-border bg-card space-y-5 rounded-xl border p-6 shadow-sm", children: success ? (_jsxs("div", { className: "space-y-4 text-center", children: [_jsx("div", { className: "bg-success/10 mx-auto flex h-12 w-12 items-center justify-center rounded-full", children: _jsx(CheckCircle2, { className: "text-success h-6 w-6" }) }), _jsx("p", { className: "text-muted-foreground text-sm", children: "Your password has been successfully reset. All existing sessions have been revoked for security." }), _jsx("button", { type: "button", onClick: () => onNavigate('/login'), className: "bg-primary text-primary-foreground hover:bg-primary/90 w-full rounded-lg py-2.5 font-medium transition-colors", children: "Sign In with New Password" })] })) : (_jsxs("form", { onSubmit: handleSubmit, className: "space-y-5", children: [error && (_jsxs("div", { className: "border-destructive/30 bg-destructive/10 flex items-start gap-3 rounded-lg border p-3", children: [_jsx(AlertTriangle, { className: "text-destructive mt-0.5 h-5 w-5 shrink-0" }), _jsx("p", { className: "text-destructive text-sm", children: error })] })), _jsxs("div", { children: [_jsx("label", { htmlFor: "reset-password", className: "text-foreground mb-1.5 block text-sm font-medium", children: "New Password" }), _jsxs("div", { className: "relative", children: [_jsx("input", { id: "reset-password", type: showPassword ? 'text' : 'password', value: password, onChange: (e) => setPassword(e.target.value), placeholder: "Enter new password", className: `${fieldClass} pr-10`, required: true, autoFocus: true, autoComplete: "new-password", minLength: 8 }), _jsx("button", { type: "button", onClick: () => setShowPassword(!showPassword), className: "text-muted-foreground hover:text-foreground absolute top-1/2 right-3 -translate-y-1/2", tabIndex: -1, children: showPassword ? _jsx(EyeOff, { className: "h-4 w-4" }) : _jsx(Eye, { className: "h-4 w-4" }) })] }), password && !meetsLength && (_jsx("p", { className: "text-destructive mt-1 text-xs", children: "Password must be at least 8 characters" }))] }), _jsxs("div", { children: [_jsx("label", { htmlFor: "reset-confirm", className: "text-foreground mb-1.5 block text-sm font-medium", children: "Confirm Password" }), _jsx("input", { id: "reset-confirm", type: showPassword ? 'text' : 'password', value: confirmPassword, onChange: (e) => setConfirmPassword(e.target.value), placeholder: "Confirm new password", className: fieldClass, required: true, autoComplete: "new-password" }), confirmPassword && !passwordsMatch && (_jsx("p", { className: "text-destructive mt-1 text-xs", children: "Passwords do not match" }))] }), _jsx("button", { type: "submit", disabled: !canSubmit, className: "bg-primary text-primary-foreground hover:bg-primary/90 flex w-full items-center justify-center gap-2 rounded-lg py-2.5 font-medium transition-colors disabled:cursor-not-allowed disabled:opacity-50", children: submitting ? (_jsxs(_Fragment, { children: [_jsx(Loader2, { className: "h-4 w-4 animate-spin" }), "Resetting..."] })) : ('Reset Password') })] })) })] }) }));
}
//# sourceMappingURL=ResetPassword.js.map