import { useEffect, useState } from "react";
import { getSeconds, differenceInMinutes } from "date-fns";
import { SESSION_TIMEOUT_INTERVAL } from "../../../utils/constants.js";
import { getAppConfigValue } from "../../../utils/app-config/config.js";
const useTrackSessionExpiry = (warningNotifiedAt) => {
  const [timeData, setTimeData] = useState();
  useEffect(() => {
    const timeLeftForExpiryAfterWarning = () => {
      const userWarnInterval = getAppConfigValue(
        "sessionTimeoutWarnInterval"
      );
      const userTimeoutInterval = getAppConfigValue(
        "sessionTimeoutInterval"
      );
      const warnInterval = userWarnInterval ? parseFloat(userWarnInterval) : SESSION_TIMEOUT_INTERVAL.WARN;
      const sessionTimeout = userTimeoutInterval ? parseFloat(userTimeoutInterval) : SESSION_TIMEOUT_INTERVAL.EXPIRY;
      const diffTime = sessionTimeout - warnInterval;
      const sessionExpiresIn = warningNotifiedAt + diffTime;
      const seconds = getSeconds(sessionExpiresIn - Date.now());
      const minutes = differenceInMinutes(sessionExpiresIn, Date.now());
      return {
        seconds: `0${seconds}`.slice(-2),
        minutes: `0${minutes}`.slice(-2)
      };
    };
    const interval = setInterval(() => {
      const { seconds, minutes } = timeLeftForExpiryAfterWarning();
      setTimeData(`${minutes}:${seconds}`);
    }, 1e3);
    return () => {
      clearInterval(interval);
    };
  }, [warningNotifiedAt]);
  return timeData;
};
export {
  useTrackSessionExpiry
};
