import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["value", "title", "children", "disabled"];
import { Children, isValidElement } from 'react';
import DataSet from '../data-set/DataSet';
import { DataSetSelection, FieldType } from '../data-set/enum';

function getOptionsFromChildren(elements, data, fields, textField, valueField, parentField, idField, disabledField, parent) {
  if (elements) {
    Children.forEach(elements, function (child) {
      if ( /*#__PURE__*/isValidElement(child)) {
        var type = child.type;

        if (type.__PRO_TREE_SELECT_NODE) {
          var _data$push;

          var props = child.props;

          var value = props.value,
              title = props.title,
              children = props.children,
              disabled = props.disabled,
              __treeNodeProps = _objectWithoutProperties(props, _excluded);

          data.push((_data$push = {}, _defineProperty(_data$push, textField, title), _defineProperty(_data$push, valueField, value), _defineProperty(_data$push, disabledField, disabled), _defineProperty(_data$push, parentField, parent), _defineProperty(_data$push, "__treeNodeProps", __treeNodeProps), _data$push));

          if (idField !== valueField) {
            data[idField] = props[idField] || value;
          }

          getOptionsFromChildren(children, data, fields, textField, valueField, parentField, idField, disabledField, props[idField] || value);
        }
      }
    });
  }
}

export default function normalizeTreeNodes(_ref) {
  var textField = _ref.textField,
      valueField = _ref.valueField,
      disabledField = _ref.disabledField,
      parentField = _ref.parentField,
      idField = _ref.idField,
      multiple = _ref.multiple,
      children = _ref.children,
      getConfig = _ref.getConfig;
  var data = [];
  var fields = [{
    name: textField,
    type: FieldType.reactNode
  }, {
    name: valueField
  }, {
    name: disabledField,
    type: FieldType["boolean"]
  }];

  if (children) {
    getOptionsFromChildren(children, data, fields, textField, valueField, parentField, idField, disabledField);
  }

  return new DataSet({
    data: data,
    fields: fields,
    paging: false,
    selection: multiple ? DataSetSelection.multiple : DataSetSelection.single,
    autoLocateFirst: false,
    parentField: parentField,
    idField: idField
  }, {
    getConfig: getConfig
  });
}
//# sourceMappingURL=normalizeTreeNodes.js.map
