'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';
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: "flex min-h-screen items-center justify-center bg-gray-50 px-4", children: _jsxs("div", { className: "w-full max-w-md", children: [_jsxs("div", { className: "mb-8 text-center", children: [_jsx("div", { className: "mx-auto mb-4 flex h-14 w-14 items-center justify-center rounded-xl bg-red-100", children: _jsx(XCircle, { className: "h-7 w-7 text-red-600" }) }), _jsx("h1", { className: "text-2xl font-bold text-gray-900", children: "Invalid Reset Link" }), _jsx("p", { className: "mt-2 text-gray-600", children: "This password reset link is invalid or has expired." })] }), _jsxs("div", { className: "space-y-4 rounded-xl border border-gray-200 bg-white p-6 text-center shadow-sm", children: [_jsx("button", { type: "button", onClick: () => onNavigate('/forgot-password'), className: "w-full rounded-lg bg-blue-600 py-2.5 font-medium text-white transition-colors hover:bg-blue-700", children: "Request New Reset Link" }), _jsx("button", { type: "button", onClick: () => onNavigate('/login'), className: "w-full text-sm text-gray-600 transition-colors hover:text-gray-800", 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: "flex min-h-screen items-center justify-center bg-gray-50 px-4", children: _jsxs("div", { className: "w-full max-w-md", children: [_jsxs("div", { className: "mb-8 text-center", children: [_jsx("div", { className: "mx-auto mb-4 flex h-14 w-14 items-center justify-center rounded-xl bg-blue-600", children: _jsx(Shield, { className: "h-7 w-7 text-white" }) }), _jsx("h1", { className: "text-2xl font-bold text-gray-900", children: success ? 'Password Reset' : 'Choose New Password' }), _jsx("p", { className: "mt-2 text-gray-600", children: success ? 'Your password has been updated' : 'Enter your new password below' })] }), _jsx("div", { className: "space-y-5 rounded-xl border border-gray-200 bg-white p-6 shadow-sm", children: success ? (_jsxs("div", { className: "space-y-4 text-center", children: [_jsx("div", { className: "mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-green-100", children: _jsx(CheckCircle2, { className: "h-6 w-6 text-green-600" }) }), _jsx("p", { className: "text-sm text-gray-600", children: "Your password has been successfully reset. All existing sessions have been revoked for security." }), _jsx("button", { type: "button", onClick: () => onNavigate('/login'), className: "w-full rounded-lg bg-blue-600 py-2.5 font-medium text-white transition-colors hover:bg-blue-700", children: "Sign In with New Password" })] })) : (_jsxs("form", { onSubmit: handleSubmit, className: "space-y-5", children: [error && (_jsxs("div", { className: "flex items-start gap-3 rounded-lg border border-red-200 bg-red-50 p-3", children: [_jsx(AlertTriangle, { className: "mt-0.5 h-5 w-5 shrink-0 text-red-600" }), _jsx("p", { className: "text-sm text-red-800", children: error })] })), _jsxs("div", { children: [_jsx("label", { htmlFor: "reset-password", className: "mb-1.5 block text-sm font-medium text-gray-700", 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: "w-full rounded-lg border border-gray-300 px-3 py-2.5 pr-10 text-sm focus:border-blue-500 focus:ring-2 focus:ring-blue-500 focus:outline-none", required: true, autoFocus: true, autoComplete: "new-password", minLength: 8 }), _jsx("button", { type: "button", onClick: () => setShowPassword(!showPassword), className: "absolute top-1/2 right-3 -translate-y-1/2 text-gray-400 hover:text-gray-600", tabIndex: -1, children: showPassword ? _jsx(EyeOff, { className: "h-4 w-4" }) : _jsx(Eye, { className: "h-4 w-4" }) })] }), password && !meetsLength && (_jsx("p", { className: "mt-1 text-xs text-red-600", children: "Password must be at least 8 characters" }))] }), _jsxs("div", { children: [_jsx("label", { htmlFor: "reset-confirm", className: "mb-1.5 block text-sm font-medium text-gray-700", 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: "w-full rounded-lg border border-gray-300 px-3 py-2.5 text-sm focus:border-blue-500 focus:ring-2 focus:ring-blue-500 focus:outline-none", required: true, autoComplete: "new-password" }), confirmPassword && !passwordsMatch && (_jsx("p", { className: "mt-1 text-xs text-red-600", children: "Passwords do not match" }))] }), _jsx("button", { type: "submit", disabled: !canSubmit, className: "flex w-full items-center justify-center gap-2 rounded-lg bg-blue-600 py-2.5 font-medium text-white transition-colors hover:bg-blue-700 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