"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.TreeFilterControl = void 0;

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

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

var _MultiSelect = require("./MultiSelect");

var _FormControl2 = require("./FormControl");

var TreeFilterControl =
/*#__PURE__*/
function (_FormControl) {
  (0, _inheritsLoose2.default)(TreeFilterControl, _FormControl);

  function TreeFilterControl() {
    return _FormControl.apply(this, arguments) || this;
  }

  var _proto = TreeFilterControl.prototype;

  _proto.renderTree = function renderTree(treeNode, depth, maxDepth) {
    var _this = this;

    var id = treeNode.id,
        label = treeNode.label,
        items = treeNode.items;

    if (items && items.length > 0 && (maxDepth === null || depth < maxDepth)) {
      var nestedNodes = items.map(function (childNode) {
        return _this.renderTree(childNode, depth + 1, maxDepth);
      });
      return _react.default.createElement(_MultiSelect.MultiSelect.Group, {
        key: id
      }, _react.default.createElement(_MultiSelect.MultiSelect.MasterOption, {
        value: id,
        label: label
      }), nestedNodes);
    } else {
      return _react.default.createElement(_MultiSelect.MultiSelect.Option, {
        key: id,
        value: id,
        label: label
      });
    }
  };

  _proto.render = function render() {
    var _this$props = this.props,
        tree = _this$props.tree,
        maxDepth = _this$props.maxDepth,
        props = (0, _objectWithoutPropertiesLoose2.default)(_this$props, ["tree", "maxDepth"]);
    var options = this.renderTree(tree, 1, maxDepth);
    return _react.default.createElement(_MultiSelect.MultiSelect, props, options);
  };

  return TreeFilterControl;
}(_FormControl2.FormControl);

exports.TreeFilterControl = TreeFilterControl;
(0, _defineProperty2.default)(TreeFilterControl, "propTypes", Object.assign({}, _MultiSelect.MultiSelect.propTypes, {
  maxDepth: _propTypes.default.number,
  tree: _propTypes.default.object.isRequired
}));
(0, _defineProperty2.default)(TreeFilterControl, "defaultProps", Object.assign({}, _MultiSelect.MultiSelect.defaultProps, {
  block: true,
  testAutomationId: 'treeFilter',
  maxDepth: null
}));