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

function getOptionsFromChildren(elements, data, fields, textField, valueField, disabledField) {
  var groups = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : [];

  if (elements) {
    var getOption = function getOption(child) {
      if (isFragment(child)) {
        var children = child.props.children;

        if (children) {
          Children.forEach(children, getOption);
        }
      } else if ( /*#__PURE__*/isValidElement(child)) {
        var type = child.type;

        if (type.__PRO_OPT_GROUP) {
          var props = child.props;
          getOptionsFromChildren(props.children, data, fields, textField, valueField, disabledField, groups.concat(props.label || ''));
        } else if (type.__PRO_OPTION) {
          var _groups$reduce;

          var _child$props = child.props,
              value = _child$props.value,
              _children = _child$props.children,
              disabled = _child$props.disabled,
              rest = _objectWithoutProperties(_child$props, _excluded);

          data.push(groups.reduce(function (obj, group, index) {
            var name = "group-".concat(index);
            obj[name] = group;

            if (!fields.find(function (field) {
              return field.name === name;
            })) {
              fields.push({
                name: name,
                type: FieldType.reactNode,
                group: groups.length - 1
              });
            }

            return obj;
          }, (_groups$reduce = {}, _defineProperty(_groups$reduce, OTHER_OPTION_PROPS, rest), _defineProperty(_groups$reduce, textField, _children), _defineProperty(_groups$reduce, valueField, value === undefined && /*#__PURE__*/isValidElement(_children) ? _children : value), _defineProperty(_groups$reduce, disabledField, disabled), _groups$reduce)));
        }
      }
    };

    Children.forEach(elements, getOption);
  }
}

export default function normalizeOptions(_ref) {
  var textField = _ref.textField,
      valueField = _ref.valueField,
      disabledField = _ref.disabledField,
      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, disabledField);
  }

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