"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.OTHER_OPTION_PROPS = void 0;
exports["default"] = normalizeOptions;

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

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

var _react = require("react");

var _DataSet = _interopRequireDefault(require("../data-set/DataSet"));

var _enum = require("../data-set/enum");

var _isFragment = _interopRequireDefault(require("../_util/isFragment"));

var _excluded = ["value", "children", "disabled"];
var OTHER_OPTION_PROPS = '__OTHER_OPTION_PROPS__';
exports.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 ((0, _isFragment["default"])(child)) {
        var children = child.props.children;

        if (children) {
          _react.Children.forEach(children, getOption);
        }
      } else if ( /*#__PURE__*/(0, _react.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 = (0, _objectWithoutProperties2["default"])(_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: _enum.FieldType.reactNode,
                group: groups.length - 1
              });
            }

            return obj;
          }, (_groups$reduce = {}, (0, _defineProperty2["default"])(_groups$reduce, OTHER_OPTION_PROPS, rest), (0, _defineProperty2["default"])(_groups$reduce, textField, _children), (0, _defineProperty2["default"])(_groups$reduce, valueField, value === undefined && /*#__PURE__*/(0, _react.isValidElement)(_children) ? _children : value), (0, _defineProperty2["default"])(_groups$reduce, disabledField, disabled), _groups$reduce)));
        }
      }
    };

    _react.Children.forEach(elements, getOption);
  }
}

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: _enum.FieldType.reactNode
  }, {
    name: valueField
  }, {
    name: disabledField,
    type: _enum.FieldType["boolean"]
  }];

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

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