import { jsx } from "react/jsx-runtime";
import { useEffect, useState } from "react";
import { useAppDispatch } from "../../data/react.js";
import { logout } from "../../data/logout/actions.js";
import {
  initSessionMonitoring,
  stopSessionMonitoring,
  subscribeToSessionExpiryWarning,
  subscribeToResetSession,
  subscribeToSessionExpiry,
  resetUserIdleTime
} from "../../utils/session.js";
import { SessionExpiry } from "../modals/session-expiry/index.js";
import { httpClientProps } from "../../communication/http-client/props.js";
const SessionTimeout = () => {
  const dispatch = useAppDispatch();
  const [showSessionExpiryWarning, setSessionExpiryWarning] = useState(false);
  const [warningNotifiedAt, setwarningNotifiedAt] = useState();
  useEffect(() => {
    httpClientProps.resetUserIdleTime = resetUserIdleTime;
    initSessionMonitoring();
    const warningCb = subscribeToSessionExpiryWarning(
      (sessionExpiryWarningNotifiedAt) => {
        if (!sessionExpiryWarningNotifiedAt) return;
        setwarningNotifiedAt(sessionExpiryWarningNotifiedAt);
        setSessionExpiryWarning(true);
      }
    );
    const resetCb = subscribeToResetSession((resetWarningModal) => {
      if (resetWarningModal === true) {
        setSessionExpiryWarning(false);
      }
    });
    const sessionExpiredCb = subscribeToSessionExpiry(() => {
      dispatch(logout.confirm());
      setSessionExpiryWarning(false);
    });
    return () => {
      warningCb();
      resetCb();
      sessionExpiredCb();
      stopSessionMonitoring();
    };
  }, [dispatch]);
  return /* @__PURE__ */ jsx("div", { children: showSessionExpiryWarning && /* @__PURE__ */ jsx(SessionExpiry, { open: true, warningNotifiedAt }) });
};
export {
  SessionTimeout
};
