"use strict";

exports.__esModule = true;
exports.default = exports.ScopeBar = exports.Scope = exports.SCOPE_SELECTOR_NAME = exports.Bar = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactRedux = require("react-redux");
var _reactRouterDom = require("react-router-dom");
var _useViewState3 = _interopRequireDefault(require("../../hooks/useViewState"));
var _useScopeData3 = _interopRequireDefault(require("./useScopeData"));
var _usePreviousModified = _interopRequireDefault(require("../../hooks/usePreviousModified"));
var _utils = require("../../utils");
var _settings = require("../../selectors/settings");
var _TooltippedTypography = _interopRequireDefault(require("./../MaterialUI/DataDisplay/TooltippedElements/TooltippedTypography"));
var _styles = require("@material-ui/core/styles");
var _Button = _interopRequireDefault(require("@material-ui/core/Button"));
var _ScopeSelector = _interopRequireDefault(require("./../MaterialUI/ScopeSelector/ScopeSelector"));
var _ScopeModificationConfirmationDialog = _interopRequireDefault(require("./ScopeModificationConfirmationDialog"));
var _useScopeConfirmationModalState = _interopRequireDefault(require("./useScopeConfirmationModalState"));
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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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 {
    scopeButton: {
      zIndex: 10,
      maxWidth: theme.spacing(25)
    },
    outlinedButton: {
      backgroundColor: theme.palette.primary.contrastText
    },
    buttonContainer: {
      position: "absolute",
      top: "50%",
      transform: "translateY(-50%)",
      right: theme.spacing(2)
    },
    scopeBar: {
      position: "relative",
      borderBottom: "1px solid ".concat(theme.palette.primary.main),
      backgroundColor: theme.palette.grey.light,
      width: "100%",
      minHeight: theme.spacing(4.9),
      userSelect: "none"
    }
  };
});
var Bar = exports.Bar = function Bar(props) {
  var classes = useStyles();
  return /*#__PURE__*/_react.default.createElement("div", {
    className: classes.scopeBar
  }, props.children);
};
__signature__(Bar, "useStyles{classes}", function () {
  return [useStyles];
});
__signature__(Bar, "useStyles{classes}", function () {
  return [useStyles];
});
var ScopeBar = exports.ScopeBar = function ScopeBar(_ref) {
  var show = _ref.show,
    name = _ref.name,
    updateViewState = _ref.updateViewState,
    disabled = _ref.disabled;
  var classes = useStyles();
  var variant = show === true ? "contained" : "outlined";

  // markup such this was made just to support correct behavior in IE
  // because it doesn't support min-width: auto for flex elements
  return /*#__PURE__*/_react.default.createElement(Bar, null, /*#__PURE__*/_react.default.createElement("div", {
    className: classes.buttonContainer
  }, /*#__PURE__*/_react.default.createElement(_Button.default, {
    disabled: disabled,
    variant: variant,
    color: "primary",
    onClick: function onClick() {
      return updateViewState("show", true);
    },
    disableElevation: true
  }, /*#__PURE__*/_react.default.createElement(_TooltippedTypography.default, {
    noWrap: true,
    children: name,
    titleValue: name
  }))));
};
__signature__(ScopeBar, "useStyles{classes}", function () {
  return [useStyles];
});
__signature__(ScopeBar, "useStyles{classes}", function () {
  return [useStyles];
});
ScopeBar.displayName = "ScopeBar";
var SCOPE_SELECTOR_NAME = exports.SCOPE_SELECTOR_NAME = "scopeSelector";
var Scope = exports.Scope = function Scope(_ref2) {
  var children = _ref2.children;
  var _useScopeData = (0, _useScopeData3.default)(),
    _useScopeData2 = _slicedToArray(_useScopeData, 3),
    currentScope = _useScopeData2[0],
    defaultNodeState = _useScopeData2[1],
    getScope = _useScopeData2[2];
  var _useViewState = (0, _useViewState3.default)(SCOPE_SELECTOR_NAME),
    _useViewState2 = _slicedToArray(_useViewState, 2),
    _useViewState2$ = _useViewState2[0],
    _useViewState2$$show = _useViewState2$.show,
    show = _useViewState2$$show === void 0 ? false : _useViewState2$$show,
    disabled = _useViewState2$.disabled,
    nodeState = _useViewState2$.nodeState,
    filter = _useViewState2$.filter,
    updateViewState = _useViewState2[1];
  var resetNodeState = (0, _react.useCallback)(function (current) {
    return current && updateViewState("nodeState", _objectSpread(_objectSpread({}, nodeState), defaultNodeState));
  }, [updateViewState, nodeState, defaultNodeState]);
  (0, _usePreviousModified.default)(show, resetNodeState);
  var reset = function reset(event) {
    updateViewState("show", false);
    event.stopPropagation();
  };
  var updateFilter = function updateFilter(event) {
    return updateViewState("filter", event);
  };
  var _useScopeConfirmation = (0, _useScopeConfirmationModalState.default)(),
    _useScopeConfirmation2 = _slicedToArray(_useScopeConfirmation, 5),
    isModalOpened = _useScopeConfirmation2[0],
    closeModal = _useScopeConfirmation2[1],
    scopeDialogType = _useScopeConfirmation2[2],
    acceptScopeChange = _useScopeConfirmation2[3],
    selectNewScope = _useScopeConfirmation2[4];
  var onScopeSelectorClose = function onScopeSelectorClose(event, newSelection) {
    reset(event);
    selectNewScope(newSelection);
  };
  var isScopeSelectable = function isScopeSelectable(scopeId) {
    var _scope$isAuthorizedSc;
    var scope = getScope(scopeId);
    return (_scope$isAuthorizedSc = scope == null ? void 0 : scope.isAuthorizedScope) != null ? _scope$isAuthorizedSc : false;
  };
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(ScopeBar, {
    name: currentScope.name,
    show: show,
    updateViewState: updateViewState,
    disabled: disabled
  }), /*#__PURE__*/_react.default.createElement(_ScopeSelector.default, {
    show: show,
    getScope: getScope,
    isScopeSelectable: isScopeSelectable,
    selectedScope: currentScope,
    closeSelector: onScopeSelectorClose,
    filter: filter,
    updateFilter: updateFilter
  }), /*#__PURE__*/_react.default.createElement(_ScopeModificationConfirmationDialog.default, {
    isModalOpened: isModalOpened,
    scopeDialogType: scopeDialogType,
    closeModalCallback: closeModal,
    okCallback: acceptScopeChange
  }), children);
};
__signature__(Scope, "useScopeData{[currentScope, defaultNodeState, getScope]}\nuseViewState{[{ show = false, disabled, nodeState, filter }, updateViewState]}\nuseCallback{resetNodeState}\nusePreviousModified{}\nuseScopeConfirmationModalState{[isModalOpened, closeModal, scopeDialogType, acceptScopeChange, selectNewScope]}", function () {
  return [_useScopeData3.default, _useViewState3.default, _usePreviousModified.default, _useScopeConfirmationModalState.default];
});
__signature__(Scope, "useScopeData{[currentScope, defaultNodeState, getScope]}\nuseViewState{[{ show = false, disabled, nodeState, filter }, updateViewState]}\nuseCallback{resetNodeState}\nusePreviousModified{}\nuseScopeConfirmationModalState{[isModalOpened, closeModal, scopeDialogType, acceptScopeChange, selectNewScope]}", function () {
  return [_useScopeData3.default, _useViewState3.default, _usePreviousModified.default, _useScopeConfirmationModalState.default];
});
Scope.displayName = "Scope";
var RoutedScope = function RoutedScope(props) {
  var defaultScope = (0, _utils.unwrapImmutable)((0, _reactRedux.useSelector)(_settings.defaultScopeSelector));
  return /*#__PURE__*/_react.default.createElement(_reactRouterDom.Switch, null, /*#__PURE__*/_react.default.createElement(_reactRouterDom.Route, {
    path: "/:scope",
    render: function render() {
      return /*#__PURE__*/_react.default.createElement(Scope, props);
    }
  }), /*#__PURE__*/_react.default.createElement(_reactRouterDom.Redirect, {
    to: "/".concat(defaultScope || "Global")
  }));
};
__signature__(RoutedScope, "useSelector{}", function () {
  return [_reactRedux.useSelector];
});
__signature__(RoutedScope, "useSelector{}", function () {
  return [_reactRedux.useSelector];
});
var _default = RoutedScope;
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/Scope/index.js");
  reactHotLoader.register(Bar, "Bar", "/home/vsts/work/1/s/src/components/Scope/index.js");
  reactHotLoader.register(ScopeBar, "ScopeBar", "/home/vsts/work/1/s/src/components/Scope/index.js");
  reactHotLoader.register(SCOPE_SELECTOR_NAME, "SCOPE_SELECTOR_NAME", "/home/vsts/work/1/s/src/components/Scope/index.js");
  reactHotLoader.register(Scope, "Scope", "/home/vsts/work/1/s/src/components/Scope/index.js");
  reactHotLoader.register(RoutedScope, "RoutedScope", "/home/vsts/work/1/s/src/components/Scope/index.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/Scope/index.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/Scope/index.js");
  reactHotLoader.register(Bar, "Bar", "/home/vsts/work/1/s/src/components/Scope/index.js");
  reactHotLoader.register(ScopeBar, "ScopeBar", "/home/vsts/work/1/s/src/components/Scope/index.js");
  reactHotLoader.register(SCOPE_SELECTOR_NAME, "SCOPE_SELECTOR_NAME", "/home/vsts/work/1/s/src/components/Scope/index.js");
  reactHotLoader.register(Scope, "Scope", "/home/vsts/work/1/s/src/components/Scope/index.js");
  reactHotLoader.register(RoutedScope, "RoutedScope", "/home/vsts/work/1/s/src/components/Scope/index.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/Scope/index.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();