"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Login = void 0;
var _react = _interopRequireWildcard(require("react"));
var _userContext = require("../../../context/user/userContext");
var _tokenService = require("../../../services/tokenService");
var _localstorageService = require("../../../services/localstorageService");
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 Login = _ref => {
  var _URLSearchParams$get;
  let {
    brandCode
  } = _ref;
  const {
    login,
    logout,
    removeAll,
    replaceUrl
  } = (0, _react.useContext)(_userContext.UserContext);
  const search = window.location.search;
  const queryToken = new URLSearchParams(search).get("token");
  const hasMultipleBrandsValue = new URLSearchParams(search).get("hmb");
  const isPrimaryBrandValue = new URLSearchParams(search).get("ipb");
  const isSelfValue = new URLSearchParams(search).get("islf");
  const corpBrandCodeValue = new URLSearchParams(search).get("cbc");
  const queryDashboard = (_URLSearchParams$get = new URLSearchParams(search).get("dashboard")) !== null && _URLSearchParams$get !== void 0 ? _URLSearchParams$get : "";
  (0, _react.useEffect)(() => {
    const checkIfTokenValid = () => {
      let shouldReplaceUrl = false;
      try {
        const isValidQueryToken = () => {
          return queryToken !== null && queryToken !== "" && queryToken !== undefined && queryToken !== "null";
        };
        if (isValidQueryToken()) {
          if ((0, _tokenService.isGivenTokenExpiring)(queryToken) || (0, _tokenService.isGivenTokenExpired)(queryToken)) {
            logout();
          } else {
            removeAll();
            (0, _tokenService.setStorageToken)(queryToken);
            (0, _localstorageService.setHasMultipleBrands)(hasMultipleBrandsValue);
            (0, _localstorageService.setIsPrimaryBrand)(isPrimaryBrandValue);
            (0, _localstorageService.setIsSelf)(isSelfValue);
            (0, _localstorageService.setCorpBrandCode)(corpBrandCodeValue);
            shouldReplaceUrl = true;
          }
        } else {
          logout();
        }
        if (shouldReplaceUrl) {
          console.log("querydashboard", queryDashboard);
          window.location.replace("/franchisee-mgmt/manage/roles");
          // replaceUrl(queryDashboard);
        }
      } catch (err) {
        console.log(err);
        logout();
      }
    };
    checkIfTokenValid();
  }, [queryToken, login, logout, replaceUrl, queryDashboard, hasMultipleBrandsValue, isPrimaryBrandValue, isSelfValue, removeAll, corpBrandCodeValue]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: "overlay-loading"
  });
};
exports.Login = Login;