"use strict";

exports.__esModule = true;
exports.default = exports.Scope = exports.SCOPE_SELECTOR_NAME = exports.ScopeBar = 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(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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 () {
  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(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

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 = function Bar(props) {
  var classes = useStyles();
  return /*#__PURE__*/_react.default.createElement("div", {
    className: classes.scopeBar
  }, props.children);
};

exports.Bar = Bar;

__signature__(Bar, "useStyles{classes}", function () {
  return [useStyles];
});

__signature__(Bar, "useStyles{classes}", function () {
  return [useStyles];
});

var 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
  }))));
};

exports.ScopeBar = ScopeBar;

__signature__(ScopeBar, "useStyles{classes}", function () {
  return [useStyles];
});

__signature__(ScopeBar, "useStyles{classes}", function () {
  return [useStyles];
});

ScopeBar.displayName = "ScopeBar";
var SCOPE_SELECTOR_NAME = "scopeSelector";
exports.SCOPE_SELECTOR_NAME = SCOPE_SELECTOR_NAME;

var 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);
};

exports.Scope = Scope;

__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 = _default2;
exports.default = _default3;
;

(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);
})();