"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.Tree = void 0;

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _TriStateCheckbox = require("../TriStateCheckbox");

require("./Tree.css");

var _ErrorBoundary = require("../../components/ErrorBoundary");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, 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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

var Tree =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Tree, _React$Component);

  function Tree() {
    _classCallCheck(this, Tree);

    return _possibleConstructorReturn(this, _getPrototypeOf(Tree).apply(this, arguments));
  }

  _createClass(Tree, [{
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps) {
      var ts = nextProps.treeStructure;
      var nodeId = this.props.nodeId;
      if (nodeId === undefined || nodeId === ts.rootId()) return true;
      var parentId = ts.parent(nodeId);
      if (parentId === undefined) return true;
      if (!nextProps.expansion[parentId]) return false;
      if (nextProps.filterOut[parentId]) return false;
      return true;
    }
  }, {
    key: "render",
    value: function render() {
      var props = this.props;

      var treeStructure = props.treeStructure,
          rootVisible = props.rootVisible,
          expansion = props.expansion,
          selection = props.selection,
          filterOut = props.filterOut,
          highlight = props.highlight,
          onSelectionToggle = props.onSelectionToggle,
          onExpansionToggle = props.onExpansionToggle,
          restProps = _objectWithoutProperties(props, ["treeStructure", "rootVisible", "expansion", "selection", "filterOut", "highlight", "onSelectionToggle", "onExpansionToggle"]);

      var _nodeId = props._nodeId || props.treeStructure.rootId();

      var labelContent = props.labelContent || treeStructure.name;

      var children = function children(id, context) {
        return treeStructure.children(id, context) || [];
      };

      var onSelectionClick = function onSelectionClick(event) {
        event.stopPropagation();
        if (onSelectionToggle) onSelectionToggle(_nodeId);
      };

      var onDetailsClick = function onDetailsClick(event) {
        if (onExpansionToggle) {
          event.preventDefault();
          event.stopPropagation();
          onExpansionToggle(_nodeId);
        }
      };

      return _react.default.createElement("div", _extends({}, restProps, {
        className: "".concat(_nodeId === props.treeStructure.rootId() ? 'vs-tree' : '', " ").concat(filterOut && filterOut[_nodeId] ? "vs-hidden ".concat(props.className) : props.className)
      }), rootVisible ? children(_nodeId).length > 0 ? _react.default.createElement("details", {
        open: expansion[_nodeId],
        onClick: onDetailsClick
      }, _react.default.createElement("summary", {
        className: 'vs-summary'
      }, _react.default.createElement("div", {
        className: 'vs-labelContent'
      }, _react.default.createElement("div", null, labelContent(_nodeId, highlight)), _react.default.createElement(_TriStateCheckbox.TriStateCheckbox, {
        className: 'vs-treecheckbox',
        value: selection[_nodeId],
        onClick: onSelectionClick
      }))), _react.default.createElement("div", {
        className: 'vs-treeChildren'
      }, children(_nodeId).map(function (_nodeId) {
        return _react.default.createElement(Tree, _extends({}, _objectSpread({}, props, {
          _nodeId: _nodeId
        }), {
          key: _nodeId,
          rootVisible: true
        }));
      }))) : _react.default.createElement("div", {
        className: 'vs-leafContent'
      }, _react.default.createElement("div", {
        onClick: onSelectionClick
      }, labelContent(_nodeId, highlight)), _react.default.createElement(_TriStateCheckbox.TriStateCheckbox, {
        className: 'vs-treecheckbox',
        value: selection[_nodeId],
        onClick: onSelectionClick
      })) : _react.default.createElement("div", null, children(_nodeId).map(function (_nodeId) {
        return _react.default.createElement(Tree, _extends({}, _objectSpread({}, props, {
          _nodeId: _nodeId
        }), {
          key: _nodeId,
          rootVisible: true
        }));
      })));
    }
  }]);

  return Tree;
}(_react.default.Component);

exports.Tree = Tree;
Tree.defaultProps = {
  rootVisible: false
};
Tree.propTypes = {
  rootVisible: _propTypes.default.bool,
  labelContent: _propTypes.default.func,
  treeStructure: _propTypes.default.shape({
    rootId: _propTypes.default.func.isRequired,
    parent: _propTypes.default.func.isRequired,
    children: _propTypes.default.func.isRequired,
    name: _propTypes.default.func
  }).isRequired,
  selection: _propTypes.default.object.isRequired,
  expansion: _propTypes.default.object.isRequired,
  filterOut: _propTypes.default.object,
  highlight: _propTypes.default.string,
  onSelectionToggle: _propTypes.default.func,
  onExpansionToggle: _propTypes.default.func
};

var _default = (0, _ErrorBoundary.withErrorBoundary)(Tree);

exports.default = _default;