import { Dispatch, SetStateAction } from "react";
import styles from "./styles.module.scss";
import { ErrorPayload, Password, useApi } from "@/api";

interface IResetPasswordProps {
  email: string;
  setResultResetPassword: Dispatch<
    SetStateAction<Password.Response | undefined>
  >;
  onClick?: () => void;
}

function ResetYourPassword({
  email,
  setResultResetPassword,
  onClick,
}: IResetPasswordProps) {
  const api = useApi();

  const resetPassword = async () => {
    if (!email) return;
    try {
      setResultResetPassword(await api.resetPassword({ email }));
    } catch (error: unknown) {
      const response = (error as ErrorPayload<Password.Response>).responseData;
      if (response?.message && !!response?.status) {
        setResultResetPassword(response);
      }
      console.log(error);
    }
  };

  return (
    <button className={styles.text} onClick={onClick ? onClick : resetPassword}>
      Reset your password
    </button>
  );
}

export default ResetYourPassword;
