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

const afterLoginDirectUrl = (lang: LanguageTypes) => {
  return `/panel/${lang}/main/pages/user/dashboard`;
};

function selectEmployeeHook(
  startTransition: any,
  data: any,
  setError: any,
  setJsonText: any,
  Router: any,
  lang: LanguageTypes
) {
  try {
    const sendData = { ...data };
    startTransition(() => {
      fetch("/api/selection/employee", {
        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);
              setTimeout(() => {
                Router.push(afterLoginDirectUrl(lang));
              }, 100);
            });
          } else {
            response.json().then((data) => {
              setError(data?.message);
            });
          }
        })
        .catch(() => {});
    });
  } catch (error) {
    setError("An error occurred during login");
  }
}

function selectOccupantHook(
  startTransition: any,
  data: any,
  setError: any,
  setJsonText: any,
  Router: any,
  lang: LanguageTypes
) {
  try {
    const sendData = { ...data };
    startTransition(() => {
      fetch("/api/selection/occupant", {
        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);
              setTimeout(() => {
                Router.push(afterLoginDirectUrl(lang));
              }, 100);
            });
          } else {
            response.json().then((data) => {
              setError(data?.message);
            });
          }
        })
        .catch(() => {});
    });
  } catch (error) {
    setError("An error occurred during login");
  }
}

export { selectEmployeeHook, selectOccupantHook };
