"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.findItemById = void 0;
exports.getActiveDescendant = getActiveDescendant;
exports.getTextFieldChild = getTextFieldChild;
exports.removeUndefined = removeUndefined;
exports.renderReactStatelyItems = renderReactStatelyItems;
exports.renderStatusMessage = renderStatusMessage;
var _react = require("react");
var _reactAria = require("../react-aria");
var _TextField = require("../TextField");
var _DropdownMenuOption = require("../Dropdown/DropdownMenuOption");
var _jsxRuntime = require("react/jsx-runtime");
// from react-stately

function renderReactStatelyItems(items, itemToString) {
  const renderItem = item => {
    const {
      id,
      name,
      children,
      ...extraAttrs
    } = item;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactAria.Item, {
      textValue: name ?? itemToString?.(item),
      ...extraAttrs,
      children: children ?? name
    }, id);
  };
  return items.map((item, index) => {
    if ('label' in item && 'items' in item) {
      const {
        id,
        label,
        items,
        ...extraAttrs
      } = item;
      const groupKey = `group-${label.replace(/\s+/g, '-').toLowerCase()}-${index}`;
      return /*#__PURE__*/(0, _react.createElement)(_reactAria.Section, {
        ...extraAttrs,
        key: groupKey,
        title: label
      }, items.map(renderItem));
    } else {
      return renderItem(item);
    }
  });
}
function renderStatusMessage(message) {
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("li", {
    className: "ds-c-autocomplete__menu-item-message",
    role: "option",
    children: message
  });
}

/**
 * Determine if a React component is a TextField
 */
function isTextField(child) {
  if (!child || ! /*#__PURE__*/(0, _react.isValidElement)(child)) {
    return false;
  }

  // Check child.type first and as a fallback, check child.type.displayName follow by child.type.name
  const componentName = child.type?.displayName || child.type?.name;
  return child.type === _TextField.TextField || componentName === 'TextField';
}
function getTextFieldChild(children) {
  let textField;
  _react.Children.forEach(children, 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) {
  const index = (items ?? []).findIndex(item => 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(key => obj[key] === undefined && delete obj[key]);
  return obj;
}
const findItemById = (id, items) => {
  for (const item of items) {
    if ('items' in item) {
      const match = item.items.find(child => child.id === id);
      if (match) return match;
    } else {
      if (item.id === id) return item;
    }
  }
};
exports.findItemById = findItemById;