"use strict";

exports.__esModule = true;
exports.default = exports.Error = exports.Loader = exports.Wrapper = exports.useAuthenticationData = void 0;

var _react = _interopRequireDefault(require("react"));

var _styledComponents = _interopRequireWildcard(require("styled-components"));

var _utils = require("../utils");

var _Placeholder = _interopRequireDefault(require("../components/Placeholder"));

var _reactRedux = require("react-redux");

var _authentication = require("../actions/authentication");

var _request = require("../reducers/request");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(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; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();

(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();

var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};

var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};

var useAuthenticationData = function useAuthenticationData() {
  return {
    loading: (0, _reactRedux.useSelector)(function (state) {
      return state.getIn(["requests", "actives", _authentication.GET_AUTHENTICATION_PROFILE]);
    }),
    defaultScope: (0, _reactRedux.useSelector)(function (state) {
      return state.getIn(["settings", "defaultScope"]);
    }) || null,
    authedUser: (0, _reactRedux.useSelector)(function (state) {
      return state.getIn(["authentication", "name"]);
    }),
    requestError: (0, _utils.unwrapImmutable)((0, _reactRedux.useSelector)(function (state) {
      return state.getIn(["requests", _request.ERROR]) || null;
    })),
    needLogin: (0, _reactRedux.useSelector)(function (state) {
      return state.getIn(["requests", _request.LOGOUT]);
    })
  };
};

exports.useAuthenticationData = useAuthenticationData;

__signature__(useAuthenticationData, "useSelector{}\nuseSelector{}\nuseSelector{}\nuseSelector{}\nuseSelector{}", function () {
  return [_reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector];
});

__signature__(useAuthenticationData, "useSelector{}\nuseSelector{}\nuseSelector{}\nuseSelector{}\nuseSelector{}", function () {
  return [_reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector];
});

var Wrapper = _styledComponents.default.div.withConfig({
  displayName: "Authenticate__Wrapper",
  componentId: "sc-cw6rn1-0"
})(["display:flex;width:60%;height:100%;padding:0 20vw;flex-direction:column;justify-content:center;"]);

exports.Wrapper = Wrapper;
var Loader = (0, _styledComponents.withTheme)(function (props) {
  return /*#__PURE__*/_react.default.createElement(Wrapper, null, /*#__PURE__*/_react.default.createElement(_Placeholder.default, {
    icon: (0, _utils.getThemeProp)(["icons", "loading"], "loading")(props),
    animate: true
  }));
});
exports.Loader = Loader;

var Error = function Error(_ref) {
  var requestError = _ref.requestError,
      needLogin = _ref.needLogin;

  if (needLogin) {
    return /*#__PURE__*/_react.default.createElement(Wrapper, null, /*#__PURE__*/_react.default.createElement("h1", null, "Not logged in"));
  }

  return /*#__PURE__*/_react.default.createElement(Wrapper, null, /*#__PURE__*/_react.default.createElement("h1", null, (0, _utils.safeGet)(requestError, "payload", "message") || "An error occurred"), "Last failing action: ", /*#__PURE__*/_react.default.createElement("pre", null, JSON.stringify(requestError, null, 2)));
};

exports.Error = Error;

var Authenticate = function Authenticate(_ref2) {
  var children = _ref2.children;

  var _useAuthenticationDat = useAuthenticationData(),
      loading = _useAuthenticationDat.loading,
      defaultScope = _useAuthenticationDat.defaultScope,
      authedUser = _useAuthenticationDat.authedUser,
      requestError = _useAuthenticationDat.requestError,
      needLogin = _useAuthenticationDat.needLogin;

  if (loading || defaultScope === null) {
    return /*#__PURE__*/_react.default.createElement(Loader, null);
  }

  if (!authedUser) {
    return /*#__PURE__*/_react.default.createElement(Error, {
      requestError: requestError,
      needLogin: needLogin
    });
  } else {
    return _react.default.Children.only(children);
  }
};

__signature__(Authenticate, "useAuthenticationData{{ loading, defaultScope, authedUser, requestError, needLogin }}", function () {
  return [useAuthenticationData];
});

__signature__(Authenticate, "useAuthenticationData{{ loading, defaultScope, authedUser, requestError, needLogin }}", function () {
  return [useAuthenticationData];
});

var _default = Authenticate;
var _default2 = _default;
var _default3 = _default2;
exports.default = _default3;
;

(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;

  if (!reactHotLoader) {
    return;
  }

  reactHotLoader.register(useAuthenticationData, "useAuthenticationData", "/home/vsts/work/1/s/src/components/Authenticate.js");
  reactHotLoader.register(Wrapper, "Wrapper", "/home/vsts/work/1/s/src/components/Authenticate.js");
  reactHotLoader.register(Loader, "Loader", "/home/vsts/work/1/s/src/components/Authenticate.js");
  reactHotLoader.register(Error, "Error", "/home/vsts/work/1/s/src/components/Authenticate.js");
  reactHotLoader.register(Authenticate, "Authenticate", "/home/vsts/work/1/s/src/components/Authenticate.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/Authenticate.js");
})();

;

(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();

;

(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;

  if (!reactHotLoader) {
    return;
  }

  reactHotLoader.register(useAuthenticationData, "useAuthenticationData", "/home/vsts/work/1/s/src/components/Authenticate.js");
  reactHotLoader.register(Wrapper, "Wrapper", "/home/vsts/work/1/s/src/components/Authenticate.js");
  reactHotLoader.register(Loader, "Loader", "/home/vsts/work/1/s/src/components/Authenticate.js");
  reactHotLoader.register(Error, "Error", "/home/vsts/work/1/s/src/components/Authenticate.js");
  reactHotLoader.register(Authenticate, "Authenticate", "/home/vsts/work/1/s/src/components/Authenticate.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/Authenticate.js");
})();

;

(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();