import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// @ts-nocheck
import { PASSWORD_MAX_LENGTH } from '$constants';
import { graphql } from '%graphqlImports';
import { Button, Card, CardContent, CardDescription, CardHeader, CardTitle, InputFieldController, } from '%reactComponentsImports';
import { logAndFormatError } from '%reactErrorImports';
import { useMutation } from '@apollo/client/react';
import { zodResolver } from '@hookform/resolvers/zod';
import { createFileRoute, Link, redirect } from '@tanstack/react-router';
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { toast } from 'sonner';
import { z } from 'zod';
export const Route = createFileRoute('/auth_/forgot-password')({
    component: ForgotPasswordPage,
    beforeLoad: ({ context: { userId } }) => {
        if (userId) {
            throw redirect({ to: '/' });
        }
    },
});
const formSchema = z.object({
    email: z
        .email()
        .max(PASSWORD_MAX_LENGTH)
        .transform((value) => value.toLowerCase()),
});
const requestPasswordResetMutation = graphql(`
  mutation RequestPasswordReset($input: RequestPasswordResetInput!) {
    requestPasswordReset(input: $input) {
      success
    }
  }
`);
function ForgotPasswordPage() {
    const [submitted, setSubmitted] = useState(false);
    const { control, handleSubmit } = useForm({
        resolver: zodResolver(formSchema),
        reValidateMode: 'onBlur',
    });
    const [requestReset, { loading }] = useMutation(requestPasswordResetMutation);
    const onSubmit = (data) => {
        requestReset({
            variables: {
                input: { email: data.email },
            },
        })
            .then(() => {
            setSubmitted(true);
        })
            .catch((err) => {
            toast.error(logAndFormatError(err, 'Sorry, we could not process your request.'));
        });
    };
    if (submitted) {
        return (_jsxs(Card, { children: [_jsxs(CardHeader, { children: [_jsx(CardTitle, { children: "Check your email" }), _jsx(CardDescription, { children: "If an account exists with that email, we've sent you a password reset link. The link will expire in 1 hour." })] }), _jsx(CardContent, { children: _jsx(Link, { to: "/auth/login", children: _jsx(Button, { variant: "secondary", className: "w-full", children: "Back to login" }) }) })] }));
    }
    return (_jsxs(Card, { children: [_jsxs(CardHeader, { children: [_jsx(CardTitle, { children: "Forgot your password?" }), _jsx(CardDescription, { children: "Enter your email address and we'll send you a link to reset your password." })] }), _jsx(CardContent, { children: _jsxs("form", { onSubmit: handleSubmit(onSubmit), children: [_jsxs("div", { className: "flex flex-col gap-6", children: [_jsx(InputFieldController, { control: control, name: "email", type: "email", autoComplete: "email", placeholder: "user@example.com" }), _jsx(Button, { type: "submit", className: "w-full", disabled: loading, children: "Send Reset Link" })] }), _jsxs("div", { className: "mt-4 text-center text-sm", children: ["Remember your password?", ' ', _jsx(Link, { to: "/auth/login", className: "underline underline-offset-4", children: "Login" })] })] }) })] }));
}
//# sourceMappingURL=forgot-password.js.map