"use strict";

exports.__esModule = true;
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactIntl = require("react-intl");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactRedux = require("react-redux");
var _styles = require("@material-ui/core/styles");
var _utils = require("../../utils");
var _applications = require("../../actions/applications");
var _useToggle3 = _interopRequireDefault(require("../../hooks/useToggle"));
var _useLoader = _interopRequireDefault(require("../../hooks/useLoader"));
var _applications2 = require("../../selectors/applications");
var _Text = require("../Text");
var _Scope = _interopRequireWildcard(require("../Scope"));
var _Topbar = _interopRequireDefault(require("./Topbar"));
var _Sidebar = _interopRequireDefault(require("./Sidebar"));
var _About = _interopRequireDefault(require("./About"));
var _Preferences = _interopRequireWildcard(require("./Preferences"));
var _ConnectedToastList = _interopRequireDefault(require("./ConnectedToastList"));
var _useApplicationHelpUrl = _interopRequireDefault(require("./useApplicationHelpUrl"));
var _useViewState3 = _interopRequireDefault(require("../../hooks/useViewState"));
var _versionInfo = require("../../actions/versionInfo");
var _locale = require("../../selectors/locale");
var _navigation = require("../../selectors/navigation");
var _loadingScreen = _interopRequireDefault(require("../MaterialUI/Feedback/loadingScreen"));
var _sharedMessages = _interopRequireDefault(require("../../sharedMessages"));
var _ActionModal = _interopRequireDefault(require("../MaterialUI/DataDisplay/PredefinedElements/ActionModal"));
var _requests = require("../../selectors/requests");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
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 useStyles = (0, _styles.makeStyles)(function (theme) {
  return {
    base: function base(props) {
      return {
        backgroundColor: theme.palette.grey.dark,
        height: "100%",
        overflow: "hidden",
        pointerEvents: props.preferencesOpen ? "none" : undefined
      };
    },
    viewPort: function viewPort(props) {
      return {
        overflow: "hidden",
        backgroundColor: "white",
        borderTopLeftRadius: "10px",
        height: "calc(100% - 40px)",
        position: "absolute",
        bottom: 0,
        right: 0,
        display: "flex",
        flexDirection: "column",
        transition: "width 0.3s ease-out",
        width: props.toggleOpen ? "calc(100% - 200px)" : "calc(100% - 50px)"
      };
    }
  };
});
var getApp = function getApp(apps, id) {
  return apps.filter(function (app) {
    return app.name === id;
  })[0];
};
var AppFrame = function AppFrame(_ref) {
  var initOpen = _ref.initOpen,
    applicationId = _ref.applicationId,
    modules = _ref.modules,
    activeModules = _ref.activeModules,
    children = _ref.children,
    noScope = _ref.noScope,
    _ref$forceShowScope = _ref.forceShowScope,
    forceShowScope = _ref$forceShowScope === void 0 ? [] : _ref$forceShowScope;
  var _useIntl = (0, _reactIntl.useIntl)(),
    formatMessage = _useIntl.formatMessage;
  var _useViewState = (0, _useViewState3.default)(_Preferences.PREFS_NAME),
    _useViewState2 = _slicedToArray(_useViewState, 1),
    prefViewState = _useViewState2[0];
  var _useToggle = (0, _useToggle3.default)(initOpen),
    _useToggle2 = _slicedToArray(_useToggle, 3),
    open = _useToggle2[0],
    toggle = _useToggle2[1],
    reset = _useToggle2[2];
  var classes = useStyles({
    preferencesOpen: prefViewState.show,
    toggleOpen: open
  });
  var locale = (0, _reactRedux.useSelector)(_locale.currentLocale);
  var needLogin = (0, _reactRedux.useSelector)(_requests.logoutSelector);
  var applications = (0, _utils.unwrapImmutable)((0, _reactRedux.useSelector)(_applications2.localizedAppSelector));
  var moduleName = (0, _reactRedux.useSelector)(_navigation.selectCurrentModuleName);
  var _useApplicationHelpUr = (0, _useApplicationHelpUrl.default)(applicationId),
    _useApplicationHelpUr2 = _slicedToArray(_useApplicationHelpUr, 1),
    helpUrl = _useApplicationHelpUr2[0];
  (0, _useLoader.default)((0, _applications.getApplications)(), function (state) {
    return (0, _applications2.localizedAppSelector)(state).size;
  });
  var currentApplication = getApp(applications, applicationId);
  (0, _useLoader.default)((0, _versionInfo.getVersionInfo)(locale), function () {
    return locale === null || helpUrl !== null;
  });
  var _React$useState = _react.default.useState(false),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    doesApplicationNeedRefresh = _React$useState2[0],
    setDoesApplicationNeedRefresh = _React$useState2[1];
  var reloadApplication = function reloadApplication() {
    window.location.reload();
    setDoesApplicationNeedRefresh(false);
  };
  (0, _react.useEffect)(function () {
    if (needLogin) {
      setDoesApplicationNeedRefresh(true);
    }
  }, [needLogin]);
  (0, _react.useEffect)(function () {
    document.title = (currentApplication == null ? void 0 : currentApplication.displayName) || applicationId;
  }, [currentApplication, applicationId]);

  // Extract only what's needed for the SideBar and its menuItems
  var menuItemFromModules = (0, _react.useMemo)(function () {
    return (modules != null ? modules : []).map(function (item) {
      return {
        id: item.id,
        label: item.label,
        icon: item.icon
      };
    });
  }, [modules]);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: classes.base
  }, /*#__PURE__*/_react.default.createElement(_ConnectedToastList.default, null), /*#__PURE__*/_react.default.createElement(_ActionModal.default, {
    title: formatMessage(_sharedMessages.default.error),
    message: formatMessage(_sharedMessages.default.needToRefresh),
    open: doesApplicationNeedRefresh,
    actions: [{
      label: _sharedMessages.default.refresh,
      handler: reloadApplication,
      isPrimary: true
    }]
  }), /*#__PURE__*/_react.default.createElement(_Topbar.default, {
    applications: applications,
    applicationId: applicationId,
    currentApplication: currentApplication,
    helpUrl: helpUrl,
    onClick: reset
  }), /*#__PURE__*/_react.default.createElement(_Sidebar.default, {
    open: open,
    toggle: toggle,
    modules: menuItemFromModules,
    activeModules: activeModules
  }), /*#__PURE__*/_react.default.createElement("div", {
    onClick: reset,
    className: classes.viewPort,
    "data-test-id": "viewport"
  }, noScope && !forceShowScope.includes(moduleName) ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_Scope.Bar, null), children) : /*#__PURE__*/_react.default.createElement(_Scope.default, null, children)), /*#__PURE__*/_react.default.createElement(_About.default, {
    currentApplication: currentApplication
  }), /*#__PURE__*/_react.default.createElement(_loadingScreen.default, null), /*#__PURE__*/_react.default.createElement(_Preferences.default, null));
};
__signature__(AppFrame, "useIntl{{ formatMessage }}\nuseViewState{[prefViewState]}\nuseToggle{[open, toggle, reset]}\nuseStyles{classes}\nuseSelector{locale}\nuseSelector{needLogin}\nuseSelector{}\nuseSelector{moduleName}\nuseApplicationHelpUrl{[helpUrl]}\nuseLoader{}\nuseLoader{}\nuseState{[doesApplicationNeedRefresh, setDoesApplicationNeedRefresh](false)}\nuseEffect{}\nuseEffect{}\nuseMemo{menuItemFromModules}", function () {
  return [_reactIntl.useIntl, _useViewState3.default, _useToggle3.default, useStyles, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector, _useApplicationHelpUrl.default, _useLoader.default, _useLoader.default];
});
__signature__(AppFrame, "useIntl{{ formatMessage }}\nuseViewState{[prefViewState]}\nuseToggle{[open, toggle, reset]}\nuseStyles{classes}\nuseSelector{locale}\nuseSelector{needLogin}\nuseSelector{}\nuseSelector{moduleName}\nuseApplicationHelpUrl{[helpUrl]}\nuseLoader{}\nuseLoader{}\nuseState{[doesApplicationNeedRefresh, setDoesApplicationNeedRefresh](false)}\nuseEffect{}\nuseEffect{}\nuseMemo{menuItemFromModules}", function () {
  return [_reactIntl.useIntl, _useViewState3.default, _useToggle3.default, useStyles, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector, _useApplicationHelpUrl.default, _useLoader.default, _useLoader.default];
});
AppFrame.displayName = "AppFrame";
AppFrame.propTypes = {
  applicationId: _propTypes.default.string.isRequired,
  modules: _propTypes.default.array.isRequired,
  activeModules: _propTypes.default.objectOf(_propTypes.default.oneOfType([_propTypes.default.bool, _propTypes.default.shape({
    type: _propTypes.default.string,
    message: _Text.ptLabel
  })])),
  noScope: _propTypes.default.bool
};
var _default = AppFrame;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(useStyles, "useStyles", "/home/vsts/work/1/s/src/components/AppFrame/AppFrame.js");
  reactHotLoader.register(getApp, "getApp", "/home/vsts/work/1/s/src/components/AppFrame/AppFrame.js");
  reactHotLoader.register(AppFrame, "AppFrame", "/home/vsts/work/1/s/src/components/AppFrame/AppFrame.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/AppFrame/AppFrame.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(useStyles, "useStyles", "/home/vsts/work/1/s/src/components/AppFrame/AppFrame.js");
  reactHotLoader.register(getApp, "getApp", "/home/vsts/work/1/s/src/components/AppFrame/AppFrame.js");
  reactHotLoader.register(AppFrame, "AppFrame", "/home/vsts/work/1/s/src/components/AppFrame/AppFrame.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/AppFrame/AppFrame.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();