import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["value", "disabled"];
import { flattenChildren, isValidElement } from '../../_util/props-util';

function convertNodeToOption(node) {
  var _ref = node,
      key = _ref.key,
      children = _ref.children,
      _ref$props = _ref.props,
      value = _ref$props.value,
      disabled = _ref$props.disabled,
      restProps = _objectWithoutProperties(_ref$props, _excluded);

  var child = children && children.default ? children.default() : undefined;
  return _objectSpread({
    key: key,
    value: value !== undefined ? value : key,
    children: child,
    disabled: disabled || disabled === ''
  }, restProps);
}

export function convertChildrenToData(nodes) {
  var optionOnly = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  var dd = flattenChildren(nodes).map(function (node, index) {
    var _children$label;

    if (!isValidElement(node) || !node.type) {
      return null;
    }

    var _ref2 = node,
        isSelectOptGroup = _ref2.type.isSelectOptGroup,
        key = _ref2.key,
        children = _ref2.children,
        props = _ref2.props;

    if (optionOnly || !isSelectOptGroup) {
      return convertNodeToOption(node);
    }

    var child = children && children.default ? children.default() : undefined;
    var label = (props === null || props === void 0 ? void 0 : props.label) || ((_children$label = children.label) === null || _children$label === void 0 ? void 0 : _children$label.call(children)) || key;
    return _objectSpread(_objectSpread({
      key: "__RC_SELECT_GRP__".concat(key === null ? index : String(key), "__")
    }, props), {}, {
      label: label,
      options: convertChildrenToData(child || [])
    });
  }).filter(function (data) {
    return data;
  });
  return dd;
}