"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DropdownMenuOption = DropdownMenuOption;
exports.default = void 0;
exports.getOptionId = getOptionId;
var _compat = require("preact/compat");
var _Icons = require("../Icons");
var _reactAria = require("../react-aria");
var _classnames = _interopRequireDefault(require("classnames"));
var _jsxRuntime = require("preact/compat/jsx-runtime");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// from react-stately

// from react-aria

function getOptionId(rootId, index) {
  return `${rootId}__item--${index}`;
}
function DropdownMenuOption(_ref) {
  let {
    componentClass,
    item,
    state,
    rootId
  } = _ref;
  const ref = (0, _compat.useRef)(null);
  const {
    optionProps,
    isSelected,
    isFocused,
    isDisabled
  } = (0, _reactAria.useOption)({
    key: item.key
  }, state, ref);
  const selectedIndicator = /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
    className: `${componentClass}__menu-item-selected-indicator`,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icons.SvgIcon, {
      title: "",
      viewBox: "0 0 448 512",
      className: "ds-u-font-size--sm",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)("path", {
        d: "M443.3 100.7c6.2 6.2 6.2 16.4 0 22.6l-272 272c-6.2 6.2-16.4 6.2-22.6 0l-144-144c-6.2-6.2-6.2-16.4 0-22.6s16.4-6.2 22.6 0L160 361.4l260.7-260.7c6.2-6.2 16.4-6.2 22.6 0z"
      })
    })
  });
  const {
    textValue,
    ...extraAttrs
  } = item.props;

  // Work around [this issue](https://github.com/adobe/react-spectrum/issues/4974) by manually
  // scrolling the selected option into view. At the time of writing this, we are using
  // `@react-aria/selection` version 3.16.1
  (0, _compat.useEffect)(() => {
    if (state.isOpen && isSelected) {
      ref.current?.scrollIntoView({
        block: 'nearest'
      });
    }
  }, [isSelected, state.isOpen]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("li", {
    ...optionProps,
    id: getOptionId(rootId, item.index),
    ...extraAttrs,
    ref: ref,
    className: (0, _classnames.default)(item.props.className, `${componentClass}__menu-item`, isFocused && `${componentClass}__menu-item--highlighted`, isSelected && `${componentClass}__menu-item--selected`, isDisabled && `${componentClass}__menu-item--disabled`),
    children: [isSelected && selectedIndicator, /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      children: item.rendered
    })]
  });
}
var _default = DropdownMenuOption;
exports.default = _default;