"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TextTypeDropdownItem = TextTypeDropdownItem;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _isEqual = _interopRequireDefault(require("lodash-es/isEqual"));
var _useListboxAttrs = require("../../../hooks/use-listbox-attrs");
var _hooks = require("../../../hooks");
var _utils = require("../../../hooks/use-tree-navigation/utils");
var _types = require("../../../dropdown-menu/types");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function TextTypeDropdownItem({
  index,
  option,
  selectedKey,
  optionsSize,
  handleSelect = () => {},
  closeDropdown = () => {}
}) {
  const {
    node,
    treeItemAttrs,
    setFocused,
    isFirstFocusable,
    ref
  } = (0, _hooks.useTreeNodeNavigation)({
    id: option.privateKey || option.key,
    getKeyActionFn: _utils.getKeyActionFnMenu,
    onCustomButtonKeyDown: () => closeDropdown(),
    onToggleSelected: isSelected => {
      if (isSelected) {
        handleSelect?.();
      }
    }
  });
  (0, _react.useEffect)(() => {
    if (selectedKey && (0, _isEqual.default)(option.key, selectedKey) || !selectedKey && isFirstFocusable) {
      setFocused(node.id);
    }
  }, [option, selectedKey, isFirstFocusable]);
  const {
    getOptionAttrs
  } = (0, _useListboxAttrs.useListboxAttrs)();
  return (
    /*#__PURE__*/
    // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
    (0, _jsxRuntime.jsx)("li", {
      ref: ref,
      "test-id": option.testId,
      className: (0, _classnames.default)('text-type-dropdown-content-item', {
        active: selectedKey === option.key,
        focused: node.isFocused
      }),
      ...getOptionAttrs({
        isSelected: selectedKey === option.key,
        onClick: handleSelect,
        position: index + 1,
        size: optionsSize
      }),
      onKeyDown: treeItemAttrs.onKeyDown,
      role: _types.MENU_ROLE.MENUITEM,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        className: option.className,
        children: option.title
      })
    }, option.key)
  );
}