"use strict";

exports.__esModule = true;
exports.stateEventUpdater = exports.default = exports.clickOutsideHandler = exports.Wrapper = exports.Preferences = exports.PREFS_NAME = void 0;
var _react = _interopRequireDefault(require("react"));
var _reactRedux = require("react-redux");
var _reactIntl = require("react-intl");
var _styles = require("@material-ui/core/styles");
var _Button = _interopRequireDefault(require("@material-ui/core/Button"));
var _utils = require("../../utils");
var _Sidepanel = _interopRequireDefault(require("../Sidepanel"));
var _useViewState3 = _interopRequireDefault(require("../../hooks/useViewState"));
var _view = require("../../actions/view");
var _locale = require("../../actions/locale");
var _applications = require("../../actions/applications");
var _applications2 = require("../../selectors/applications");
var _locale2 = require("../../selectors/locale");
var _settings = require("../../selectors/settings");
var _withClickOutside = _interopRequireDefault(require("../../hocs/withClickOutside"));
var _versionInfo = require("../../actions/versionInfo");
var _sharedMessages = _interopRequireDefault(require("./../../sharedMessages"));
var _SelectProps = _interopRequireDefault(require("../MaterialUI/Inputs/SelectProps"));
var _InformationItem = _interopRequireDefault(require("../MaterialUI/DataDisplay/PredefinedElements/InformationItem"));
var _Select = _interopRequireDefault(require("../MaterialUI/Inputs/Select"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
(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 PREFS_NAME = exports.PREFS_NAME = "__prefsDialog";
var useStyles = (0, _styles.makeStyles)(function (theme) {
  return {
    prefPanel: {
      backgroundColor: "#f7f7f7",
      borderLeft: "1px solid ".concat(theme.palette.grey.borders),
      borderTop: "1px solid ".concat(theme.palette.grey.borders),
      borderTopLeftRadius: "5px",
      top: "40px",
      boxShadow: "-3px 2px 5px 0px rgba(0, 0, 0, 0.2)",
      zIndex: 9998,
      transition: "right 400ms ease-out"
    },
    header: {
      flex: "0 0 auto",
      borderBottom: "1px solid ".concat(theme.palette.grey.borders),
      borderTopLeftRadius: "5px",
      padding: "15px 30px",
      height: "18px",
      fontSize: "15px",
      fontFamily: theme.typography.button.fontFamily,
      textTransform: "uppercase",
      color: theme.palette.primary.main,
      backgroundColor: "#ffffff"
    },
    prefForm: {
      flex: "1 1 auto",
      display: "flex",
      flexDirection: "column",
      padding: "20px 30px",
      fontSize: "12px"
    },
    footer: {
      flex: "0 0 auto",
      display: "flex",
      alignItems: "center",
      justifyContent: "flex-end",
      backgroundColor: "#ffffff",
      borderTop: "1px solid ".concat(theme.palette.grey.borders),
      paddingRight: "10px",
      height: "60px"
    },
    prefButton: {
      marginRight: "20px",
      minWidth: "110px"
    },
    wrapper: {
      display: "flex",
      flexDirection: "column",
      height: "100%"
    }
  };
});
var Wrapper = exports.Wrapper = (0, _withClickOutside.default)(_react.default.forwardRef(__signature__(__signature__(function (_ref, ref) {
  var children = _ref.children;
  var classes = useStyles();
  return /*#__PURE__*/_react.default.createElement("div", {
    ref: ref,
    className: classes.wrapper
  }, children);
}, "useStyles{classes}", function () {
  return [useStyles];
}), "useStyles{classes}", function () {
  return [useStyles];
})));
var stateEventUpdater = exports.stateEventUpdater = (0, _utils.memoize)(function (update, key) {
  return function (value) {
    return update(key, value);
  };
});
var usePreferenceSetup = function usePreferenceSetup() {
  var _viewState$language, _viewState$applicatio;
  var dispatch = (0, _reactRedux.useDispatch)();
  var _useViewState = (0, _useViewState3.default)(PREFS_NAME),
    _useViewState2 = _slicedToArray(_useViewState, 2),
    viewState = _useViewState2[0],
    updateViewState = _useViewState2[1];
  var initialLanguage = (0, _reactRedux.useSelector)(_locale2.currentLocaleOrDefault);
  var initialApplication = (0, _reactRedux.useSelector)(_settings.defaultAppId) || "";
  var languageSelectProps = new _SelectProps.default();
  languageSelectProps.set(_SelectProps.default.propNames.value, (_viewState$language = viewState.language) != null ? _viewState$language : initialLanguage);
  languageSelectProps.set(_SelectProps.default.propNames.disabled, false);
  languageSelectProps.set(_SelectProps.default.propNames.positionOverride, {
    disablePortal: true
  });
  languageSelectProps.set(_SelectProps.default.propNames.update, stateEventUpdater(updateViewState, "language"));
  var applicationSelectProps = new _SelectProps.default();
  applicationSelectProps.set(_SelectProps.default.propNames.value, (_viewState$applicatio = viewState.application) != null ? _viewState$applicatio : initialApplication);
  applicationSelectProps.set(_SelectProps.default.propNames.disabled, false);
  applicationSelectProps.set(_SelectProps.default.propNames.positionOverride, {
    disablePortal: true
  });
  applicationSelectProps.set(_SelectProps.default.propNames.update, stateEventUpdater(updateViewState, "application"));
  return {
    show: viewState.show,
    languageOptions: (0, _utils.unwrapImmutable)((0, _reactRedux.useSelector)(_locale2.cultureOptionList)),
    languageSelectProps: languageSelectProps,
    applicationOptions: (0, _utils.unwrapImmutable)((0, _reactRedux.useSelector)(_applications2.localizedAppOptionSelector)),
    applicationSelectProps: applicationSelectProps,
    clear: function clear() {
      return dispatch((0, _view.setValue)(PREFS_NAME, {
        show: false
      }));
    },
    save: function save() {
      if (viewState.language) {
        dispatch((0, _locale.changeLocale)(viewState.language));
        dispatch((0, _locale.setDefaultLanguage)(viewState.language));
        dispatch((0, _versionInfo.resetVersionInfo)());
        // TODO: reload any language dependent data?
      }
      if (viewState.application) {
        dispatch((0, _applications.setMyApplication)(viewState.application));
      }
      dispatch((0, _view.setValue)(PREFS_NAME, {
        show: false
      }));
    }
  };
};
__signature__(usePreferenceSetup, "useDispatch{dispatch}\nuseViewState{[viewState, updateViewState]}\nuseSelector{initialLanguage}\nuseSelector{}\nuseSelector{}\nuseSelector{}", function () {
  return [_reactRedux.useDispatch, _useViewState3.default, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector];
});
__signature__(usePreferenceSetup, "useDispatch{dispatch}\nuseViewState{[viewState, updateViewState]}\nuseSelector{initialLanguage}\nuseSelector{}\nuseSelector{}\nuseSelector{}", function () {
  return [_reactRedux.useDispatch, _useViewState3.default, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector, _reactRedux.useSelector];
});
var clickOutsideHandler = exports.clickOutsideHandler = function clickOutsideHandler(e) {
  e.preventDefault();
  e.stopPropagation();
};
var Preferences = exports.Preferences = function Preferences() {
  var classes = useStyles();
  var _usePreferenceSetup = usePreferenceSetup(),
    show = _usePreferenceSetup.show,
    save = _usePreferenceSetup.save,
    clear = _usePreferenceSetup.clear,
    languageOptions = _usePreferenceSetup.languageOptions,
    languageSelectProps = _usePreferenceSetup.languageSelectProps,
    applicationOptions = _usePreferenceSetup.applicationOptions,
    applicationSelectProps = _usePreferenceSetup.applicationSelectProps;
  return /*#__PURE__*/_react.default.createElement(_Sidepanel.default, {
    in: show,
    width: "380px",
    timeout: 400,
    className: classes.prefPanel
  }, /*#__PURE__*/_react.default.createElement(Wrapper, {
    onClickOutside: clickOutsideHandler
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: classes.header
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _sharedMessages.default.preferences)), /*#__PURE__*/_react.default.createElement("div", {
    className: classes.prefForm
  }, /*#__PURE__*/_react.default.createElement(_InformationItem.default, {
    label: _sharedMessages.default.displayLanguage
  }, /*#__PURE__*/_react.default.createElement(_Select.default, {
    options: languageOptions,
    selectProps: languageSelectProps
  })), /*#__PURE__*/_react.default.createElement(_InformationItem.default, {
    label: _sharedMessages.default.defaultApp
  }, /*#__PURE__*/_react.default.createElement(_Select.default, {
    options: applicationOptions,
    selectProps: applicationSelectProps
  }))), /*#__PURE__*/_react.default.createElement("div", {
    className: classes.footer
  }, /*#__PURE__*/_react.default.createElement(_Button.default, {
    id: "cancelPrefs",
    onClick: clear,
    className: classes.prefButton,
    variant: "outlined"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _sharedMessages.default.cancel)), /*#__PURE__*/_react.default.createElement(_Button.default, {
    id: "savePrefs",
    onClick: save,
    className: classes.prefButton,
    variant: "outlined",
    color: "primary"
  }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _sharedMessages.default.save)))));
};
__signature__(Preferences, "useStyles{classes}\nusePreferenceSetup{{ show, save, clear, languageOptions, languageSelectProps, applicationOptions, applicationSelectProps }}", function () {
  return [useStyles, usePreferenceSetup];
});
__signature__(Preferences, "useStyles{classes}\nusePreferenceSetup{{ show, save, clear, languageOptions, languageSelectProps, applicationOptions, applicationSelectProps }}", function () {
  return [useStyles, usePreferenceSetup];
});
var _default = Preferences;
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(PREFS_NAME, "PREFS_NAME", "/home/vsts/work/1/s/src/components/AppFrame/Preferences.js");
  reactHotLoader.register(useStyles, "useStyles", "/home/vsts/work/1/s/src/components/AppFrame/Preferences.js");
  reactHotLoader.register(Wrapper, "Wrapper", "/home/vsts/work/1/s/src/components/AppFrame/Preferences.js");
  reactHotLoader.register(stateEventUpdater, "stateEventUpdater", "/home/vsts/work/1/s/src/components/AppFrame/Preferences.js");
  reactHotLoader.register(usePreferenceSetup, "usePreferenceSetup", "/home/vsts/work/1/s/src/components/AppFrame/Preferences.js");
  reactHotLoader.register(clickOutsideHandler, "clickOutsideHandler", "/home/vsts/work/1/s/src/components/AppFrame/Preferences.js");
  reactHotLoader.register(Preferences, "Preferences", "/home/vsts/work/1/s/src/components/AppFrame/Preferences.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/AppFrame/Preferences.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(PREFS_NAME, "PREFS_NAME", "/home/vsts/work/1/s/src/components/AppFrame/Preferences.js");
  reactHotLoader.register(useStyles, "useStyles", "/home/vsts/work/1/s/src/components/AppFrame/Preferences.js");
  reactHotLoader.register(Wrapper, "Wrapper", "/home/vsts/work/1/s/src/components/AppFrame/Preferences.js");
  reactHotLoader.register(stateEventUpdater, "stateEventUpdater", "/home/vsts/work/1/s/src/components/AppFrame/Preferences.js");
  reactHotLoader.register(usePreferenceSetup, "usePreferenceSetup", "/home/vsts/work/1/s/src/components/AppFrame/Preferences.js");
  reactHotLoader.register(clickOutsideHandler, "clickOutsideHandler", "/home/vsts/work/1/s/src/components/AppFrame/Preferences.js");
  reactHotLoader.register(Preferences, "Preferences", "/home/vsts/work/1/s/src/components/AppFrame/Preferences.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/AppFrame/Preferences.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();