import { LanguageTypes } from "@/validations/mutual/language/validations";

export function loginHook(
  startTransition: any,
  data: any,
  setError: any,
  setJsonText: any,
  Router: any,
  lang: LanguageTypes
) {
  try {
    const sendData = { ...data };
    startTransition(() => {
      fetch("/api/login/email", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(sendData),
      })
        .then((response) => {
          if (response.status === 200) {
            response.json().then((data) => {
              setTimeout(() => {
                Router.push(`/panel/dashboard`);
              }, 100);
            });
          } else {
            response.json().then((data) => {
              setError(data?.message);
            });
          }
        })
        .catch(() => {});
    });
  } catch (error) {
    setError("An error occurred during login");
  }
}
