import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { graphql, readFragment } from '%graphqlImports';
import { Button, Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, InputFieldController, } from '%reactComponentsImports';
import { useMutation } from '@apollo/client/react';
import { zodResolver } from '@hookform/resolvers/zod';
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { toast } from 'sonner';
import { z } from 'zod';
const PASSWORD_MIN_LENGTH = 8;
const passwordResetSchema = z
    .object({
    newPassword: z
        .string()
        .min(PASSWORD_MIN_LENGTH, `Password must be at least ${PASSWORD_MIN_LENGTH} characters`)
        .max(255, 'Password is too long'),
    confirmPassword: z.string(),
})
    .refine((data) => data.newPassword === data.confirmPassword, {
    message: "Passwords don't match",
    path: ['confirmPassword'],
});
export const passwordResetDialogUserFragment = graphql(`
  fragment PasswordResetDialog_user on User {
    id
    name
    email
  }
`);
const resetUserPasswordMutation = graphql(`
    mutation ResetUserPassword($input: ResetUserPasswordInput!) {
      resetUserPassword(input: $input) {
        user {
          ...PasswordResetDialog_user
        }
      }
    }
  `, [passwordResetDialogUserFragment]);
/**
 * Dialog for resetting a user's password.
 * Allows admins to set a new password for any user without requiring the current password.
 * Self-contained component that handles its own mutation logic.
 *
 * @param props - The component props
 * @param props.user - The user whose password is being reset
 * @param props.open - Whether the dialog is open
 * @param props.onOpenChange - Callback when dialog open state changes
 * @returns The password reset dialog component
 */
export function PasswordResetDialog({ user, open, onOpenChange, }) {
    const [isSaving, setIsSaving] = useState(false);
    // Unmask the fragment data
    const userData = readFragment(passwordResetDialogUserFragment, user);
    const [resetUserPassword] = useMutation(resetUserPasswordMutation);
    const form = useForm({
        resolver: zodResolver(passwordResetSchema),
        defaultValues: {
            newPassword: '',
            confirmPassword: '',
        },
    });
    const handleSubmit = async (data) => {
        setIsSaving(true);
        try {
            await resetUserPassword({
                variables: {
                    input: {
                        userId: userData.id,
                        newPassword: data.newPassword,
                    },
                },
            });
            toast.success('Password reset successfully');
            onOpenChange(false);
            form.reset();
        }
        catch {
            toast.error('Failed to reset password');
        }
        finally {
            setIsSaving(false);
        }
    };
    return (_jsx(Dialog, { open: open, onOpenChange: onOpenChange, children: _jsxs(DialogContent, { className: "sm:max-w-[425px]", children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { children: "Reset Password" }), _jsxs(DialogDescription, { children: ["Set a new password for ", userData.name ?? userData.email] })] }), _jsxs("form", { onSubmit: form.handleSubmit(handleSubmit), children: [_jsxs("div", { className: "space-y-4 py-4", children: [_jsx(InputFieldController, { control: form.control, name: "newPassword", label: "New Password", type: "password", placeholder: "Enter new password", description: `Must be at least ${PASSWORD_MIN_LENGTH} characters`, autoComplete: "off" }), _jsx(InputFieldController, { control: form.control, name: "confirmPassword", label: "Confirm Password", type: "password", placeholder: "Confirm new password", autoComplete: "off" })] }), _jsxs(DialogFooter, { children: [_jsx(Button, { type: "button", variant: "outline", onClick: () => {
                                        onOpenChange(false);
                                        form.reset();
                                    }, children: "Cancel" }), _jsx(Button, { type: "submit", disabled: isSaving, children: "Reset Password" })] })] })] }) }));
}
//# sourceMappingURL=password-reset-dialog.js.map