import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import { toJS } from 'mobx';
export function getKey(record, idField) {
  return String(idField ? record.get(idField) : record.key);
}

function getTreeNode(record, children, idField, text, treeNodeProps, async, filterText) {
  var key = getKey(record, idField);
  var nodeProps = record.get('__treeNodeProps') || {};
  return _objectSpread(_objectSpread({
    title: text,
    isLeaf: async ? undefined : filterText ? !children || !children.length : !record.children || !record.children.length,
    children: children,
    record: record
  }, nodeProps), {}, {
    selectable: nodeProps.selectable !== false && (record.dataSet.selection ? record.selectable : false),
    eventKey: key,
    key: key
  }, treeNodeProps);
}

export function getTreeNodes(dataSet, records, renderer, onTreeNode, async, titleField, optionsFilter, searchMatcher, filterText) {
  var idField = dataSet.props.idField;

  if (records) {
    return records.reduce(function (array, record, index) {
      if (record.status !== 'delete') {
        var children = record.children ? getTreeNodes(dataSet, record.children, renderer, onTreeNode, async, titleField, optionsFilter, searchMatcher, filterText) : null;

        if (!searchMatcher || !filterText || children && children.length || searchMatcher(record, filterText)) {
          if (!optionsFilter || optionsFilter(record, index, records)) {
            var node = getTreeNode(record, children, idField, renderer({
              dataSet: dataSet,
              record: record,
              text: toJS(record.get(titleField))
            }), onTreeNode({
              dataSet: dataSet,
              record: record
            }), async, filterText);

            if (node) {
              array.push(node);
            }
          } else if (children) {
            array.push.apply(array, _toConsumableArray(children));
          }
        }
      }

      return array;
    }, []);
  }

  return null;
}
//# sourceMappingURL=util.js.map
