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

function selectEmployeeHook(
  startTransition: any,
  data: any,
  setError: any,
  setJsonText: any,
  Router: any,
  lang: LanguageTypes
) {
  try {
    const sendData = { ...data };
    const urlToDirect = `/panel/${lang}/dashboard`;
    startTransition(() => {
      apiPostFetcher({
        url: "/api/selection/employee",
        isNoCache: true,
        body: sendData,
      })
        .then((response) => {
          const data = response.data;
          if (response.success) {
            setTimeout(() => {
              Router.push(urlToDirect);
            }, 100);
          } else {
            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 };
    const urlToDirect = `/auth/${lang}/panel/dashboard`;
    startTransition(() => {
      apiPostFetcher({
        url: "/api/selection/occupant",
        isNoCache: true,
        body: sendData,
      })
        .then((response) => {
          const data = response.data;
          if (response.success) {
            setTimeout(() => {
              Router.push(urlToDirect);
            }, 100);
          } else {
            setError(data?.message);
          }
        })
        .catch(() => {});
    });
  } catch (error) {
    setError("An error occurred during login");
  }
}

export { selectEmployeeHook, selectOccupantHook };
