"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AutoLogout = void 0;
var _react = require("react");
var _userContext = require("../context/user/userContext");
const AutoLogout = _ref => {
  let {
    brandCode,
    children
  } = _ref;
  const {
    logout
  } = (0, _react.useContext)(_userContext.UserContext);
  let timer;
  const handleLogoutTimer = events => {
    timer = setTimeout(() => {
      resetTimer();
      Object.values(events).forEach(item => {
        window.removeEventListener(item, resetTimer);
      });
      logoutAction();
    }, 900000);
  };
  const resetTimer = () => {
    if (timer) clearTimeout(timer);
  };
  (0, _react.useEffect)(() => {
    const events = ["load", "mousemove", "mousedown", "click", "scroll", "keypress"];
    const fn = () => {
      Object.values(events).forEach(item => {
        window.addEventListener(item, () => {
          resetTimer();
          handleLogoutTimer(events);
        });
      });
    };
    fn();
  });
  const logoutAction = () => {
    logout();
  };
  return children;
};
exports.AutoLogout = AutoLogout;