"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getActiveDescendant = getActiveDescendant;
exports.getTextFieldChild = getTextFieldChild;
exports.removeUndefined = removeUndefined;
exports.renderReactStatelyItems = renderReactStatelyItems;
exports.renderStatusMessage = renderStatusMessage;
var _react = _interopRequireDefault(require("react"));
var _reactAria = require("../react-aria");
var _TextField = require("../TextField");
var _DropdownMenuOption = require("../Dropdown/DropdownMenuOption");
var _excluded = ["id", "name", "children", "isResult"]; // from react-stately
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function renderReactStatelyItems(items, itemToString) {
  return items.map(function (item) {
    var id = item.id,
      name = item.name,
      children = item.children,
      isResult = item.isResult,
      extraAttrs = _objectWithoutProperties(item, _excluded);
    return /*#__PURE__*/_react.default.createElement(_reactAria.Item, _extends({}, extraAttrs, {
      textValue: name !== null && name !== void 0 ? name : itemToString === null || itemToString === void 0 ? void 0 : itemToString(item),
      key: id
    }), children !== null && children !== void 0 ? children : name);
  });
}
function renderStatusMessage(message) {
  return /*#__PURE__*/_react.default.createElement("li", {
    "aria-selected": "false",
    className: "ds-c-autocomplete__menu-item-message",
    role: "option"
  }, message);
}

/**
 * Determine if a React component is a TextField
 */
function isTextField(child) {
  var _child$type, _child$type2;
  if (!child || ! /*#__PURE__*/_react.default.isValidElement(child)) {
    return false;
  }

  // Check child.type first and as a fallback, check child.type.displayName follow by child.type.name
  var componentName = ((_child$type = child.type) === null || _child$type === void 0 ? void 0 : _child$type.displayName) || ((_child$type2 = child.type) === null || _child$type2 === void 0 ? void 0 : _child$type2.name);
  return child.type === _TextField.TextField || componentName === 'TextField';
}
function getTextFieldChild(children) {
  var textField;
  _react.default.Children.forEach(children, function (child) {
    if (isTextField(child)) {
      textField = child;
    }
  });
  return textField;
}

/**
 * Assumes that it will find the item, so only use in cases where react-aria
 * would define an aria-activedescendent
 */
function getActiveDescendant(rootId, state, items) {
  var index = (items !== null && items !== void 0 ? items : []).findIndex(function (item) {
    return state.selectionManager.focusedKey === item.id;
  });
  return (0, _DropdownMenuOption.getOptionId)(rootId, index);
}

/**
 * Mutates the original object, deleting properties whose values are undefined.
 * Returns the object.
 */
function removeUndefined(obj) {
  Object.keys(obj).forEach(function (key) {
    return obj[key] === undefined && delete obj[key];
  });
  return obj;
}