"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.RequireAuth = void 0;
var _react = _interopRequireWildcard(require("react"));
var _dist = require("react-router-dom/dist");
var _userContext = require("../../../context/user/userContext");
var _LoaderContext = require("../../../context/loader/LoaderContext");
var _authService = require("../../../services/authService");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const RequireAuth = _ref => {
  let {
    children
  } = _ref;
  //const search = window.location.search;
  const location = (0, _dist.useLocation)();
  const [waiting, setWaiting] = (0, _react.useState)(true);
  const [processing, setProcessing] = (0, _react.useState)(-1);
  const {
    state,
    logout,
    getTokenDetails,
    getBrandSpecificOptions,
    getProfileAndPrivileges
  } = (0, _react.useContext)(_userContext.UserContext);
  const [renderChildren, shouldRenderChildren] = (0, _react.useState)(false);
  const {
    updateLoaderStatus
  } = (0, _react.useContext)(_LoaderContext.LoaderContext);
  const [isGettingNewToken, setIsGettingNewToken] = (0, _react.useState)(false);
  const {
    hasMultipleBrands,
    isPrimaryBrand,
    isSelf,
    corpBrandCode
  } = getBrandSpecificOptions();
  (0, _react.useEffect)(() => {
    const isAuthenticated = () => {
      const {
        token,
        isInValid,
        isExpired,
        isExpiring
      } = getTokenDetails();
      if (isExpired || isInValid) {
        logout();
      } else if (isExpiring) {
        if (isGettingNewToken === false) {
          setIsGettingNewToken(true);
          (0, _authService.refreshToken)(token).then(res => {
            setIsGettingNewToken(false);
          });
        }
      } else {
        var _state$user$isAuthent, _state$user;
        shouldRenderChildren(true);
        updateLoaderStatus(false);
        const isAuthenticated = (_state$user$isAuthent = state === null || state === void 0 || (_state$user = state.user) === null || _state$user === void 0 ? void 0 : _state$user.isAuthenticated) !== null && _state$user$isAuthent !== void 0 ? _state$user$isAuthent : false;
        if (isAuthenticated === false) {
          if (processing === -1) {
            setProcessing(0);
            getProfileAndPrivileges(token, hasMultipleBrands, isPrimaryBrand, isSelf, corpBrandCode, () => {
              setWaiting(false);
              setProcessing(1);
            });
          }
        } else {
          updateLoaderStatus(false);
          setWaiting(false);
        }
      }
    };
    isAuthenticated();
  }, [state, getTokenDetails, getProfileAndPrivileges, updateLoaderStatus, renderChildren, processing, logout, isGettingNewToken, hasMultipleBrands, isPrimaryBrand, corpBrandCode, isSelf]);
  if (waiting) return /*#__PURE__*/(0, _jsxRuntime.jsx)(_react.default.Fragment, {});
  if (renderChildren) {
    return children;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_dist.Navigate, {
    to: "/login",
    state: {
      from: location
    }
  });
};
exports.RequireAuth = RequireAuth;