"use strict";

exports.__esModule = true;
exports.default = exports.BaseScopeTreeView = void 0;

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

var _TreeView = _interopRequireDefault(require("@material-ui/lab/TreeView"));

var _TreeItem = _interopRequireDefault(require("./TreeItem"));

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

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 BaseScopeTreeView = function BaseScopeTreeView(_ref) {
  var rootId = _ref.rootId,
      getScope = _ref.getScope,
      onSelected = _ref.onSelected,
      _ref$initExpanded = _ref.initExpanded,
      initExpanded = _ref$initExpanded === void 0 ? [] : _ref$initExpanded,
      selected = _ref.selected,
      setSelected = _ref.setSelected,
      multipleSelect = _ref.multipleSelect,
      isScopeSelectable = _ref.isScopeSelectable;

  var _React$useState = _react.default.useState(initExpanded),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      expanded = _React$useState2[0],
      setExpanded = _React$useState2[1];

  var handleToggle = function handleToggle(_, nodeIds) {
    setExpanded(nodeIds);
  };

  var onScopeSelect = function onScopeSelect(event, nodeId) {
    var newSelected = !multipleSelect ? nodeId : selected.includes(nodeId) ? selected.filter(function (id) {
      return id !== nodeId;
    }) : [nodeId].concat(selected);
    setSelected(newSelected);
    onSelected(event, newSelected);
  };

  var renderTree = function renderTree(scopeId) {
    var currentScope = getScope(scopeId);
    if (currentScope == null) return null;
    return /*#__PURE__*/_react.default.createElement(_TreeItem.default, {
      key: "ScopeSelector-".concat(currentScope.id),
      scope: currentScope,
      rootId: rootId,
      onScopeSelect: onScopeSelect,
      isScopeSelectable: isScopeSelectable
    }, currentScope.children.map(function (child) {
      return renderTree(child);
    }));
  };

  return /*#__PURE__*/_react.default.createElement(_TreeView.default, {
    expanded: expanded,
    selected: selected,
    onNodeToggle: handleToggle
  }, renderTree(rootId));
};

exports.BaseScopeTreeView = BaseScopeTreeView;

__signature__(BaseScopeTreeView, "useState{[expanded, setExpanded](initExpanded)}");

__signature__(BaseScopeTreeView, "useState{[expanded, setExpanded](initExpanded)}");

var ScopeTreeView = function ScopeTreeView(_ref2) {
  var rootId = _ref2.rootId,
      getScope = _ref2.getScope,
      selected = _ref2.selected,
      expanded = _ref2.expanded,
      onSelected = _ref2.onSelected,
      multipleSelect = _ref2.multipleSelect,
      isScopeSelectable = _ref2.isScopeSelectable;
  var initialSelected = Array.isArray(selected) ? selected : selected ? [selected] : [];

  var _React$useState3 = _react.default.useState(initialSelected),
      _React$useState4 = _slicedToArray(_React$useState3, 2),
      currentSelected = _React$useState4[0],
      setCurrentSelected = _React$useState4[1];

  return /*#__PURE__*/_react.default.createElement(BaseScopeTreeView, {
    rootId: rootId,
    getScope: getScope,
    onSelected: onSelected,
    initExpanded: expanded,
    selected: currentSelected,
    setSelected: setCurrentSelected,
    multipleSelect: multipleSelect,
    isScopeSelectable: isScopeSelectable
  });
};

__signature__(ScopeTreeView, "useState{[currentSelected, setCurrentSelected](initialSelected)}");

__signature__(ScopeTreeView, "useState{[currentSelected, setCurrentSelected](initialSelected)}");

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

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

  if (!reactHotLoader) {
    return;
  }

  reactHotLoader.register(BaseScopeTreeView, "BaseScopeTreeView", "/home/vsts/work/1/s/src/components/MaterialUI/ScopeSelector/ScopeTreeView.js");
  reactHotLoader.register(ScopeTreeView, "ScopeTreeView", "/home/vsts/work/1/s/src/components/MaterialUI/ScopeSelector/ScopeTreeView.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/MaterialUI/ScopeSelector/ScopeTreeView.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(BaseScopeTreeView, "BaseScopeTreeView", "/home/vsts/work/1/s/src/components/MaterialUI/ScopeSelector/ScopeTreeView.js");
  reactHotLoader.register(ScopeTreeView, "ScopeTreeView", "/home/vsts/work/1/s/src/components/MaterialUI/ScopeSelector/ScopeTreeView.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/MaterialUI/ScopeSelector/ScopeTreeView.js");
})();

;

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