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) => {
              console.log("data", data); //   setJsonText(JSON.stringify(data));
              setTimeout(() => {
                const userType =
                  data?.data?.user_type.toLowerCase() === "employee"
                    ? "employee"
                    : "occupant";
                const rediretUrl = `/auth/${lang}/select?type=${userType}`;
                console.log("rediretUrl", rediretUrl);
                Router.push(rediretUrl);
              }, 100);
            });
          } else {
            response.json().then((data) => {
              setError(data?.message);
            });
          }
        })
        .catch(() => {});
    });
  } catch (error) {
    setError("An error occurred during login");
  }
}
