import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import { MultiSelect } from './MultiSelect';
import { FormControl } from './FormControl';
export var TreeFilterControl =
/*#__PURE__*/
function (_FormControl) {
  _inheritsLoose(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.createElement(MultiSelect.Group, {
        key: id
      }, React.createElement(MultiSelect.MasterOption, {
        value: id,
        label: label
      }), nestedNodes);
    } else {
      return React.createElement(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 = _objectWithoutPropertiesLoose(_this$props, ["tree", "maxDepth"]);

    var options = this.renderTree(tree, 1, maxDepth);
    return React.createElement(MultiSelect, props, options);
  };

  return TreeFilterControl;
}(FormControl);

_defineProperty(TreeFilterControl, "propTypes", Object.assign({}, MultiSelect.propTypes, {
  maxDepth: PropTypes.number,
  tree: PropTypes.object.isRequired
}));

_defineProperty(TreeFilterControl, "defaultProps", Object.assign({}, MultiSelect.defaultProps, {
  block: true,
  testAutomationId: 'treeFilter',
  maxDepth: null
}));