import { useEffect } from 'react';
import {
  clearSession,
  encryptedValuesUrlFun,
  getSession,
} from '../Services/Others';

function RedirectApps() {
  const commonUrl = import.meta.env.ENV_COMMON_BASE_URL;

  useEffect(() => {
    const MobileNo = encryptedValuesUrlFun(getSession('MobileNo'));
    const userName = encryptedValuesUrlFun(getSession('userName'));
    const UserId = encryptedValuesUrlFun(getSession('UserId'));
    const UserType = encryptedValuesUrlFun(getSession('UserType'));
    const CompId = encryptedValuesUrlFun(getSession('CompId'));
    const CompName = encryptedValuesUrlFun(getSession('CompName'));
    const SessionId = encryptedValuesUrlFun(getSession('SessionId'));
    // const AuthToken = sessionStorage.getItem('auth');

    if (sessionStorage.getItem('BranchId') !== null) {
      clearSession('BranchId');
    }
    if (sessionStorage.getItem('AppId') !== null) {
      clearSession('AppId');
    }
    if (sessionStorage.getItem('hasRefreshed') !== null) {
      sessionStorage.removeItem('hasRefreshed');
    }
    const param = {
      MN: MobileNo,
      UN: userName,
      UD: UserId,
      UT: UserType,
      CD: CompId,
      CN: CompName,
      SD: SessionId,
      // "AT":AuthToken
    };
    const queryParams = new URLSearchParams(param).toString();
    window.location.replace(
      `${commonUrl}/home/landing-page/home?${queryParams}`
    );
  }, []);

  return null; // Since this is not rendering any JSX, return null
}

export default RedirectApps;
